简体   繁体   English

如何在 Python 中生成相关随机数?

[英]How do I generate correlated random numbers in Python?

How do I create a set of n vectors of dimensionality d such that elements have correlation c (ie, if a vector has one large element, the other elements are likely to be large)?如何创建一组 n 个维度为 d 的向量,使元素具有相关性 c (即,如果向量具有一个大元素,则其他元素可能很大)?

For demonstration, let's say n=5, d=3, and c=0.7.为了演示,假设 n=5、d=3 和 c=0.7。

Is there some way to set up conv here: https://numpy.org/doc/stable/reference/random/generated/numpy.random.multivariate_normal.html有没有办法在这里设置转换: https://numpy.org/doc/stable/reference/random/generated/numpy.random.multivariate_normal.html

This may be too much to ask, but what if I want the numbers drawn from a normal distribution?这可能问得太多了,但是如果我想要从正态分布中提取的数字怎么办?

Thanks!谢谢!

Edit: Basically I'm trying to create a synthetic population whose individuals differ in some latent variable, and ideally this latent variable would follow a normal distribution.编辑:基本上我正在尝试创建一个合成群体,其个体在某些潜在变量中有所不同,理想情况下,这个潜在变量将遵循正态分布。 For instance, the psychometric g factor summarizes performance on multiple tests, and explains a certain amount of variance between people on a given test.例如,心理测量 g 因素总结了多项测试的表现,并解释了给定测试中人与人之间的一定量的差异。 So I'd like to create n vectors (population size) of dimensionality d (number of tasks), but maybe c needs to be a vector of d numbers?所以我想创建维度为 d(任务数)的 n 个向量(人口规模),但也许 c 需要是 d 个数字的向量? And I might need to specify a vector of d numbers for the latent variable scores (eg, g), or maybe that emerges from how the vectors for the individuals are created?而且我可能需要为潜在变量分数(例如,g)指定一个由 d 个数字组成的向量,或者这可能来自于如何创建个体向量?

Might this be what you are looking for?这可能是您正在寻找的东西吗?

import numpy as np


def gen_random(n: int, d: int, covar: float) -> np.ndarray:
    """
    Paramters
    ---------
    n : int
        number of samples generated
    d : int
        dimensionality of samples
    covar : float
        uniform covariance for samples
    
    Returns
    -------
    samples : np.ndarray
        samples in as (n, d)-matrix
    """
    cov_mat = np.ones((d, d)) * covar; np.fill_diagonal(cov_mat, 1)
    offset = np.zeros(d)

    return np.random.multivariate_normal(offset, cov_mat, size=n)


v = gen_random(n=10_000, d=3, covar=0.7)
print(v)
# [[ 0.03031736  0.18227023 -0.1302022 ]
#  [-0.17770689  0.70979971 -0.74631702]
#  [-0.78485455 -0.73942846 -0.04819704]
#  ...
#  [ 2.5928135   2.43727782  1.59459156]
#  [ 0.33443158 -0.74126937 -0.7542286 ]
#  [ 0.11238505 -0.1940429   0.7397402 ]]

# sanity check
print(np.corrcoef(v, rowvar=False))
# [[1.         0.6985444  0.69802535]
#  [0.6985444  1.         0.70168241]
#  [0.69802535 0.70168241 1.        ]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM