蒙特卡洛正态分布java,R:蒙特卡洛模拟和正态分布问题

I am trying to solve the following exercise:

Let Z_n be maximum of n standard normal observations. Estimate what n should be so that P(Z_n>4)=0.25

I have tried following code and I know the answer is about n=9000 because it returns aproximately 0.25.

I should change my code so that n is the output and not the input.

n=9000

x1

length(x1[x1>4])/length(x1)

How can I do that?

Thanks for helping!

解决方案

Well, you could select appropriate range and then just do binary search. Just remember, result will depend on number of samples and RNG seed.

Zn

max(rnorm(n))

}

Sample

set.seed(312345) # sample same sequence of numbers

x

sum( x > 4.0 )/N

}

P

BinarySearch

lo

hi

s_lo

s_hi

if (s_lo > P)

return(list(-1, 0.0, 0.0)) # wrong low end of interval

if (s_hi < P)

return(list(-2, 0.0, 0.0)) # wrong high end of interval

while (hi-lo > 1) {

me

s_me

if (s_me >= P)

hi

else

lo

cat("hi = ", hi, "lo = ", lo, "S = ", s_me, "\n")

}

list(hi, Sample(N, hi-1), Sample(N, hi))

}

q

print(q[1]) # n at which we have P(Zn(n)>4)>=0.25

print(q[2]) # P(Zn(n-1)>4)

print(q[3]) # P(Zn(n)>4)

As a result, I've got

9089

0.24984

0.25015

which looks reasonable. It is quite slow though...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值