# Generating correlated data using numpy while controlling multicollinearity

I am using the following code (adopted from the code in this post). I have no problems with the code. My question is that if with this code I can create or prevent multicollinearity among the variables.

For example, in the following code, a (considered the dependent variable) is set to be correlated with b, c, and d (considered independent variables); b is set to be not correlated with c and d; and c is set to be correlated with d.

I wonder in this scenario, do I create a possible multicollinearity between c and d, and prevent it between b and c&d?

I appreciate any insights!

a = np.array([30, 70])
b = np.array([5, 30])
c = np.array([3, 7])
d = np.array([0, 4])

means = [a.mean(), b.mean(), c.mean(), d.mean()]
stds = [a.std(), b.std(), c.std(), d.std()]

a1_1=stds[0]**2; a2_2=stds[1]**2; a3_3=stds[2]**2; a4_4=stds[3]**2;

#a vs others
a1_2 = stds[0] * stds[1] * 0.7     #b
a1_3 = stds[0] * stds[2] * 0.7     #c
a1_4 = stds[0] * stds[3] * 0.7     #d

#b vs others
a2_3 = stds[1] * stds[2] * 0.1     #c
a2_4 = stds[1] * stds[3] * 0.1     #d

#c vs others
a3_4 = stds[2] * stds[3] * 0.7     #d

covs = [
[a1_1, a1_2, a1_3, a1_4],
[a1_2, a2_2, a2_3, a2_4],
[a1_3, a2_3, a3_3, a3_4],
[a1_4, a2_4, a3_4, a4_4],
]

m = np.random.multivariate_normal(means, covs, 800).T
data = pd.DataFrame(m).transpose()
data.corr()

-----------------------------------------------------------
OUTPUT
a           b           c           d
a   1.000000    0.722361    0.691691    0.724141
b   0.722361    1.000000    0.125139    0.157968
c   0.691691    0.125139    1.000000    0.704796
d   0.724141    0.157968    0.704796    1.000000

• There are many ways to accomplish this. You might therefore refocus your question on the issues of (a) how to specify the degree of "multicollinearity" among the regressors and (b) how to generate regressors with a specified amount of collinearity. – whuber Feb 25 '19 at 14:05