I checked the questions and answers but I couldn't find the way of solution. I need your help. I just want to create correlated variables due to my correlation matrix. I tried these codes but it didn't work.
import pandas as pd
import numpy as np
from numpy.random import multivariate_normal
correlation = np.array([
1.00, 0.75, 0.85, 0.90,
0.75, 1.00, 0.30, 0.40,
0.85, 0.30, 1.00, 0.50,
0.90, 0.40, 0.50, 1.00]).reshape((4,4))
xy = multivariate_normal((100, 105, 110, 120), correlation, size = 100)
xy = pd.DataFrame(xy, columns = ["x1", "x2", "x3", "x4"])
import scipy.stats
scipy.stats.pearsonr(xy["x1"], xy["x2"])
I check it by Pearson Correlation coefficient I must be 0.75 as correlation matrix but is not. Is there a way to generate correlated variables as you can see in the matrix "correlation"?
Aucun commentaire:
Enregistrer un commentaire