def induce_correlations(data, corrmat):
<EOL>data_rank = np.vstack([rankdata(datai) for datai in data.T]).T<EOL>data_rank_score = data_rank / (data_rank.shape[<NUM_LIT:0>] + <NUM_LIT:1.0>)<EOL>data_rank_score = norm(<NUM_LIT:0>, <NUM_LIT:1>).ppf(data_rank_score)<EOL>p = chol(corrmat)<EOL>t = np.corrcoef(data_rank_score, rowvar=<NUM_LIT:0>)<EOL>q = chol(t)<EOL>s = np.dot(p, np.linalg.inv(q))<EOL>new_data = np.dot(data_rank_score, s.T)<EOL>new_data_rank = np.vstack([rankdata(datai) for datai in new_data.T]).T<EOL>for i in range(data.shape[<NUM_LIT:1>]):<EOL><INDENT>vals, order = np.unique(<EOL>np.hstack((data_rank[:, i], new_data_rank[:, i])), return_inverse=True<EOL>)<EOL>old_order = order[: new_data_rank.shape[<NUM_LIT:0>]]<EOL>new_order = order[-new_data_rank.shape[<NUM_LIT:0>] :]<EOL>tmp = data[np.argsort(old_order), i][new_order]<EOL>data[:, i] = tmp[:]<EOL><DEDENT>return data<EOL>
Induce a set of correlations on a column-wise dataset

Parameters
----------
data : 2d-array
    An m-by-n array where m is the number of samples and n is the
    number of independent variables, each column of the array corresponding
    to each variable
corrmat : 2d-array
    An n-by-n array that defines the desired correlation coefficients
    (between -1 and 1). Note: the matrix must be symmetric and
    positive-definite in order to induce.

Returns
-------
new_data : 2d-array
    An m-by-n array that has the desired correlations.