Hello
Consider this example in MathWorks website:
n=1000;
sigma=0.5;
Sigmalnd=sigma.^2.*[1 0; 0 1];
rng(default);
ZInd=mvnrnd([0 0],Sigmalnd,n);
If i get the mean of the first pair of generated random variables , it would be:
K>> mean(ZInd(1,:))
ans =
0.3028
What i want is that the mean of the generated random variables in each pair of 1000 should be equal to 0 as is the mean for each random variable. How should i do this? Any help/idea is highly appreciated.
Bests

답변 (1개)

the cyclist
the cyclist 2021년 5월 20일
편집: the cyclist 2021년 5월 20일

0 개 추천

Let's take this small example in which we generate just 3 pairs of correlated random numbers:
n=3; % Generate 3 pairs, instead of 1000
sigma=0.5;
Sigmalnd=sigma.^2.*[1 0; 0 1];
rng default % I corrected your syntax here
ZInd=mvnrnd([0 0],Sigmalnd,n)
ZInd = 3×2
0.2688 0.4311 0.9169 0.1594 -1.1294 -0.6538
The covariance matrix specifies that the first column and the second column will be uncorrelated. (Specifically, the parent distribution variables will be uncorrelated. The sample distribution will have some correlation, due to sampling error.)
Now let's look at the mean of that first row, as you suggested:
mean(ZInd(1,:))
ans = 0.3500
You are saying that you want the mean of the first row to be zero. This implies that you want the sum of the first row to be zero. This further implies that the second column value is always the exact negative of the first column value. So, these values are not uncorrelated, but rather are perfectly (and negatively) correlated.
So, you just need to change Sigmalnd to sigma.^2.*[1 -1; -1 1]:
Sigmalnd=sigma.^2.*[1 -1; -1 1];
rng default
ZInd=mvnrnd([0 0],Sigmalnd,n)
ZInd = 3×2
0.2688 -0.2688 0.9169 -0.9169 -1.1294 1.1294
Or, more simply, just generate the first column, and then negate it to get the second column.
rng default
ZInd=sigma*randn(n,1);
ZInd(:,2) = -ZInd(:,1)
ZInd = 3×2
0.2688 -0.2688 0.9169 -0.9169 -1.1294 1.1294

댓글 수: 6

Thanks for your response, however my code is different to this simple example and i just wanted to ask my question in a short format..let me give you an explanation of my own code
Nmc=1000;
covC=0.5;
muC = 8500; % Pa %parameters of lognormal distribution
sigmalnC=abs(sqrt(log(1+(covC^2))));
mulnC = (log(muC))-(0.5*sigmalnC^2);
uu=3;
ro=0.36;
CM=eye(uu+1,uu+1);
CM(uu,uu+1)=ro; %correlation matrix(should be positive definite; det>0)
CM(uu+1,uu)=ro;
rng('default'); %fixing the seed value
i=1:uu+1;
mulnCC(i)=mulnC;
RC=mvnrnd(mulnCC,(sigmalnC^2)*CM,Nmc);
C=exp(RC);
As you see if i take mean from first row, the result is 1.0183e+04 not 8500 which i want.
K>> mean(C(1,:))
ans =
1.0183e+04
the cyclist
the cyclist 2021년 5월 21일
I would not expect the mean of the first row to be 8500. I'll explain why, using an analogy.
Suppose I flip 4 fair coins, where I consider heads = 1 and tails = 0. Suppose I do that 1,000 times, just as you have generated 1000 observations of 4 random variables.
Suppose the first flips of 4 coins gives H-H-H-T. So, I consider that to be 1-1-1-0, which has a mean of 0.75.
Should I be concerned that the mean is not 0.5? No!
In the same way, you should not be concerned that the mean of your first row is not 8500. You are just seeing the random variation.
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2021년 5월 22일
편집: Pooneh Shah Malekpoor 2021년 5월 22일
Nice explanation! However, I have a reason to do this. Imagine that in each of 1000 flips(each Monte Carlo for example), you have to increase the number of fair coins according to some relationships. i am coding a problem where the number of samples of random variables generated for each Monte Carlo iteration changes during each iteration according to some principles. some lines of this principle (for producng samples of different size for each iteration) are as follows:
1. Generate a normally distributed global average Z1(0) (I know the mean and variance for this)
2. Subdivide the field into two equal parts.
3. Generate two normally distributed values, Z1(1) and Z2(1)
whose means and variances are selected so as to satisfy three criteria:
(a) they show the correct variance according to local averaging theory
(b) they are properly correlated with one another
(c) they average to the parent value:Z1(0)=(Z1(1)+ Z2(1))/2
meaning:the distribution of Z1(1) and Z2(1) are conditioned on the value of Z1(0)
4.Subdivide each cell in stage 1 into two equal parts.
5. Generate two normally distributed values, Z1(2) and Z2(2) whose means and variances are selected so as to satisfy four criteria:
(a) they show the correct variance according to local averaging theory
(b) they are properly correlated with one another
(c) they average to the parent value, Z1(1)=(Z1(2)+ Z2(2))/2
(d) they are properly correlated with Z3(2) and Z4(2)
and so forth.
Any help/idea is highly appreciated.
Paul
Paul 2021년 5월 22일
Sounds like an interesting problem, but I want to be clear that I understand it For sake of discussion, I'm going to assume that all of these random variables are normal.
Considering Stage 0. We have a normal, random variable Z10 with expected value E(Z10) = mu10 and variance Var(Z10) = s10^2.
Considering Stage 1. We have two additional random variables, Z11 and Z21. According to steps 3a and 3b these variables have specified variances and correlation coefficient, so we know that these variables have a joint, normal distribution with covariance
Sigma1 = [s11^2 rho*s11*s12; rho*s11*s12 s21^2]
However the expected values mu11 and mu21 are not specified. Presumably, these are supposed to come from step 3c. I'm not sure how to interpret step 3c. I think it's meant to be interpreted as statement on the conditional, expected values of Z11 and Z21, i.e., E(Z11 | Z10 = z10) + E(Z21 | Z10 = z10) = 2*z10. Note the distinction between the random variable Z10 and a realized value z10 (upper case vs. lower case). Is this the correct interpretation of step 3c?
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2021년 5월 22일
sorry, what do you mean by step 3c?
Paul
Paul 2021년 5월 23일
(c) they average to the parent value:Z1(0)=(Z1(1)+ Z2(1))/2

댓글을 달려면 로그인하십시오.

질문:

2021년 5월 20일

댓글:

2021년 5월 23일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by