use mvnrnd with some conditions

3 visualizaciones (últimos 30 días)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor el 20 de Mayo de 2021
Comentada: Paul el 23 de Mayo de 2021
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

Respuestas (1)

the cyclist
the cyclist el 20 de Mayo de 2021
Editada: the cyclist el 20 de Mayo de 2021
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 comentarios
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor el 22 de Mayo de 2021
sorry, what do you mean by step 3c?
Paul
Paul el 23 de Mayo de 2021
(c) they average to the parent value:Z1(0)=(Z1(1)+ Z2(1))/2

Iniciar sesión para comentar.

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by