相关正态随机变量与任意rho(corrcoef)

时间:2018-10-11 06:33:31

标签: c++ math statistics

我有一个关于生成相关随机变量的问题...是否有一种方法可以生成通常为rho = 0的x1(0,1),x2(0,1);或产生 x3(0,1),x4(0,1)的rho = 0.75或其他?

到目前为止,我已经尝试过

1-独立法线发生器:

vector<double> uncorr_normal(double m, double s, int n)
{
random_device seed;
mt19937 gen{ seed() };

normal_distribution<> dist{ m, s };

vector<double> samples;
for (int i = 0; i < n; i++)
{
    samples.push_back(dist(gen));
}
return samples;
}

2个依赖的普通生成器:

pair<vector<double>, vector<double>> 
corr_normal(double m1, double s1, double m2, double s2, double rho, int n)
{
vector<double> X;
vector<double> Y;

random_device seed;
mt19937 gen{ seed() };

normal_distribution<> dist1{ m1, s1 };
normal_distribution<> dist2{ m2, s2 };

for (int i = 0; i < n; i++)
{
    double x = dist1(gen);
    X.push_back(x);
    double y = rho * x + sqrt(1 - rho * rho) * dist2(gen);
    Y.push_back(y);
}
pair<vector<double>, vector<double>> pair(X, Y);
return pair;

}

我通过以下实现的函数来测量相关系数:

double rho(vector<double>& X, vector<double>& Y)
{
double sum_X = 0, sum_Y = 0, sum_XY = 0;
double squareSum_X = 0, squareSum_Y = 0;
//------------------------------------------
size_t n = max(X.size(), Y.size());
//------------------------------------------
for (int i = 0; i < n; i++)
{
    // sum of elements of array X.
    sum_X = sum_X + X[i];

    // sum of elements of array Y.
    sum_Y = sum_Y + Y[i];

    // sum of X[i] * Y[i].
    sum_XY = sum_XY + X[i] * Y[i];

    // sum of square of array elements.
    squareSum_X = squareSum_X + X[i] * X[i];
    squareSum_Y = squareSum_Y + Y[i] * Y[i];
}

// use formula for calculating correlation coefficient.
double corr = (double)(n * sum_XY - sum_X * sum_Y)
    / (double)(sqrt((n * squareSum_X - sum_X * sum_X)
        * (n * squareSum_Y - sum_Y * sum_Y)));

//------------------------------------------
return corr;

}

但是,如果我生成两个不相关的随机变量并使用rho函数对其进行测试,则我不会得到rho = 0;

在相关情况下,如果我插入随机相关向量,我也不会得到指定的rho。

您能帮我吗?

最好的问候

1 个答案:

答案 0 :(得分:0)

在关联的情况下,您必须创建标准的正常样本,然后对其进行转换和关联:

pair<vector<double>, vector<double>>
corr_normal(double m1, double s1, double m2, double s2, double rho, int n)
{
    vector<double> X;
    vector<double> Y;

    random_device seed;
    mt19937 gen{ seed() };

    normal_distribution<> dist1{ 0.0, 1.0 };
    normal_distribution<> dist2{ 0.0, 1.0 };

    for (int i = 0; i < n; i++)
    {
            double x = dist1(gen);
            X.push_back(m1 + x * s1);
            double y = m2 + s2*(rho * x + sqrt(1 - rho * rho) * dist2(gen));
            Y.push_back(y);
    }
    pair<vector<double>, vector<double>> pair(X, Y);
    return pair;
}

请参见http://www.statisticalengineering.com/bivariate_normal.htm