Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Monte Carlo Simulation Problem

Subject: Monte Carlo Simulation Problem

From: Tony WANG

Date: 21 Jul, 2009 18:20:03

Message: 1 of 5

Hi,

A random variable X is normally distributed with mean mu=4 and standard deviation sigma=0.4, i.e., X ~ N(4, 0.4^2).

Another random variable Y is X's function: Y=Pr(X<=4.2).

Now, I want to calculate the variance of Y. Is the following Monte Carlo procedure correct?

================
X 3.9 4.3 4.0 ... 4.4 (10000 points)
Y 1 0 1 0

Then estimate the variance of Y: Var(Y)=0.2136.

The Matlab Code:
Mu=4;
Sigma=0.4;
for i=1:10000
    SimuX(i)=random('norm',Mu,Sigma);
    if SimuX(i)<=4.2
        SimuY(i)=1;
    else
        SimuY(i)=0;
    end
end
hist(SimuX,20);
MEAN=mean(SimuY)
VAR=var(SimuY)
================

Thank you, folks.

Subject: Monte Carlo Simulation Problem

From: Simon Preston

Date: 21 Jul, 2009 21:26:01

Message: 2 of 5

"Tony WANG" <tonywang86@gmail.com> wrote in message <h450sj$t4m$1@fred.mathworks.com>...
> Hi,
>
> A random variable X is normally distributed with mean mu=4 and standard deviation sigma=0.4, i.e., X ~ N(4, 0.4^2).
>
> Another random variable Y is X's function: Y=Pr(X<=4.2).
>
> Now, I want to calculate the variance of Y. Is the following Monte Carlo procedure correct?
> ...

Looks good to me. Y is Bernoulli with mean=normcdf((4.2-4)/.4) and variance=mean*(1-mean)

Best wishes, S

Subject: Monte Carlo Simulation Problem

From: Tony WANG

Date: 22 Jul, 2009 01:24:03

Message: 3 of 5

Thanks a lot, Simon. I think that's the answer I wanted.

"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message <h45bp9$hr6$1@fred.mathworks.com>...
> Looks good to me. Y is Bernoulli with mean=normcdf((4.2-4)/.4) and variance=mean*(1-mean)
>
> Best wishes, S

Subject: Monte Carlo Simulation Problem

From: Simon Preston

Date: 22 Jul, 2009 18:04:02

Message: 4 of 5

"Tony WANG" <tonywang86@gmail.com> wrote in message <h45pnj$rlt$1@fred.mathworks.com>...
> Thanks a lot, Simon. I think that's the answer I wanted.

Actually, on second reading it's not exactly right. The code is correct if you define Y to be I(X<=4.2) where I is the indicator function. Then mean(Y) = Pr(X<=4.2). (But in the way you define Y in the OP it is not a random variable)

Hope that helps, S

Subject: Monte Carlo Simulation Problem

From: Tony WANG

Date: 23 Jul, 2009 01:22:01

Message: 5 of 5

Thanks for pointing out my problem. See, that's where I got confused about the original simulation code.

If I insist Y=Pr(X<=4.2), then Y should be a constant (with variance zero). I think the following modified code is the one that is consistent with the original problem.

clear;
Mu=4;
Sigma=0.4;
for i=1:1000
    Counter=0;
    for j=1:1000
        SimuX=random('norm',Mu,Sigma);
        if SimuX<=4.2
            Counter=Counter+1;
        end
    end
    SimuY(i)=Counter/1000;
end
MEAN=mean(SimuY)
VAR=var(SimuY)

Result:
MEAN=0.6512 (True value should be normcdf((4.2-4)/0.4)=0.6515)
VAR=2.0528e-04 (True value should be 0)

Tony

"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message <h47kai$db7$1@fred.mathworks.com>...
> Actually, on second reading it's not exactly right. The code is correct if you define Y to be I(X<=4.2) where I is the indicator function. Then mean(Y) = Pr(X<=4.2). (But in the way you define Y in the OP it is not a random variable)
>
> Hope that helps, S

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us