简体   繁体   中英

Parameter Estimation in R Simulation

I am fairly new to R and is exploring simulation to estimate the parameter n (integer)

1) Z is a vector of n draws from N(0,1)

2) Probability of max(Z)>4 equals 0.25

What is the best way in R to estimate the parameter n to satisfy these two conditions? I got stuck when trying to avoid looping or exhaustive search in the code. Thanks!

Edit: assuming a totally simulation based result, with no attempts to do this analytically,

I'd create a function like this:

prob <- function(n) {
  sum(replicate(10000, max(rnorm(n))) > 4)/10000
}

To explain that a bit, max(rnorm(n))) > 4 will return a TRUE or FALSE . The call to replicate performs that operation 10000 times. Then I average to get a probability estimate.

Then I would check out the ?optimise function, to try and get an estimate for n . You'd need to create another function which has a minima when prob(n) = 0.25 , so something like:

result <- function(n) abs(prob(n) - 0.25) .

Note, depending on how you pick your parameters this could take a long time to run. Test things out first to see what values for n might be reasonable.

Here is another (related) way, that takes advantage of pnorm which gives you the CDF for N(0,1). So pnorm(4) tells you the probability that a draw from N(0,1) <= 4 and in consequence 1 - pnorm(4) will tell us the probability that a draw is greater than 4. If any draw is greater than 4, than obviously the max is greater than 4, so we just need to concentrate on the probability that some observation is greater than 4.

Since draws are independent we can take products, so the probability of a draw greater than 4 in n draws is 1 - (pnorm(4)^n) . Based on this we can create on objective function and solve:

# Minimize squared deviations
fopt <- function(n){(1 - pnorm(4)^n - .25)^2} 
# or .75 - pnorm(4)^n, but this is clearer

# I specify start and end points. We guess really wide
optimise(fopt, interval = c(100, 100000))
#> $minimum
#> [1] 9083.241
#> 
#> $objective
#> [1] 2.262374e-20

# Now check the result
(1 - pnorm(4)^9083.241)
#> [1] 0.25

We see we get a result of 9083.241 which evaluates to exactly .25. If we only take integer results (9083) it evaluates to .2499943

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM