Search code examples
rsimulationmontecarlo

R Proble with Monte Carlo Simulation


How I could solve this exercise:

Let U come from a uniform (0,1) distribution and Z come from a standard normal distribution. Let X = pZ + (1-p)U. Estimate p when X has a variance of 0.4.

Maybe I should install.packages('polynom')?

p is equal to 0,62 because I´ve already calculated it analitically, but I don´t know how to solve it with R using random random numbers generators


Solution

  • Here is an example

    fobj <- function(p)  abs(var(p*rnorm(1e5) + (1-p)*runif(1e5))-0.4)
    pval <- optimise(fobj,c(0,1))$minimum
    

    where

    • first define your objective function fobj, i.e., distance between variance of X and 0.4
    • then apply optimise minimize the fobj

    Result

    > pval
    [1] 0.6223968