我对生成相关的随机变量有疑问...是否有一种生成x1(0,1),x2(0,1)的方法,正常情况下rho = 0;或产生
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;
对于相关情况,如果我插入随机相关 vector ,我也不会得到指定的rho。
你能帮我吗?
最好的祝福
最佳答案
在关联的情况下,您必须创建标准的正常样本,然后对其进行转换和关联:
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
关于c++ - 相关正态随机变量与任意rho(corrcoef),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52753680/