Question

In: Statistics and Probability

Hello, i have this excersice in R studio Write a function in R that generates simulations...

Hello, i have this excersice in R studio

Write a function in R that generates simulations of a Poisson random variable as follows: define I = [λ], and use p_i + 1 = λp_i / (i + 1) to determine F recursively. Generate a random number U, determine if X≤I by comparing if U≤F (I). If X≤I searches downwards starting at I, otherwise it searches upwards starting from I + 1. Compare the time it takes for the two algorithms in 5000 simulations of a Poisson random variable with parameter λ = 10,200,500.

example

# Poisson usando Inversión
rpoisI <- function(lambda = 1){
  U <- runif(1)
  i <- 0
  p <- exp(-lambda)
  P <- p
  while(U >= P){
    p <- lambda * p / (i + 1)
    P <- P + p
    i <- i + 1
  }
  i
}
sims_pois <- rerun(2000, rpoisI()) %>% flatten_dbl()

ggplot() +
    geom_histogram(aes(x = sims_pois, y = ..density..), binwidth = 1)

Solutions

Expert Solution

# Poisson Random generator

### Compare the time it takes for the two algorithms in 5000 simulations of
#   a Poisson random variable with parameter λ = 10,200,500.
poisn.gen=function(lambda,size){
x=NULL
for (j in 1:size) {
    u=runif(1)
    p=exp(-lambda)
    f=p
    i=0
    s=0
    while(s==0){
      if(u<f){
        x=append(x,i,after = length(x))
        s=1
      }
      else{
        p=(p*lambda)/(i+1)
        f=f+p
        i=i+1
      }
    }
}
return(x)
}
poisn.gen(10,5000)

poisn.gen(200,5000)

poisn.gen(500,5000)

hist(poisn.gen(500,5000))


Related Solutions

In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
I have an assignment that must be done on R/R studio R/RStudio - Chapter 4- Correlation...
I have an assignment that must be done on R/R studio R/RStudio - Chapter 4- Correlation and Regression Step 1 – Download and Install the R/RStudio software on your computer. The link and instructions to do that are on Blackboard under Course Information. Step 2 – Access the Data Sets in MyLab through StatCrunch. In chapter 4, a data set called “Used BMW prices 2017” is what we’ll use. Step 3 – When you open the data set in StatCrunch,...
How is this done in R-studio? I have been trying to figure it out but I...
How is this done in R-studio? I have been trying to figure it out but I am getting more and more confused. While imprisoned by the Germans during World War II, the English mathematician John Kerrich tossed a coin 10,000 times and obtained 5067 heads. Let p be the probability of a head on a single toss. We wish to check if the data are consistent with the hypothesis that the coin was fair. a) Set up the hypotheses. Why...
I want this to be solved using R studio or R software, please. Here is the...
I want this to be solved using R studio or R software, please. Here is the example: The data in stat4_prob5 present the performance of a chemical process as a function of sever controllable process variables. (a) Fit a multiple regression modelrelating CO2product (y) to total solvent (x1) and hydrogen consumption (x2) and report the fitted regression line. (b) Find a point estimatefor the variance term σ2. (c) Construct the ANOVA tableand test for the significance of the regression using...
Hello, I want to write my math solutions in LATEX but I have no idea how...
Hello, I want to write my math solutions in LATEX but I have no idea how to do it. I want an example code written in LATEX for the solution of math problem "If x and y are rational, then x+y is also rational." I have the solution but I need the LATEX code so that I can understand how to write in LATEX. Thank You!
In R/ R Studio, what code would I enter to find the answers to these questions?...
In R/ R Studio, what code would I enter to find the answers to these questions? What is the code to find the descriptive/ summary statistics of all variables in a data set and how do i find the mean values? What is the code to measure the skewness measure of delta time for all of all the values in the data set? What is the code to draw a histogram and q-q plot of the natural log of a...
Hello, please answer with either formula or preferably with TI-84 ONLY. (No r studio, etc) thanks!...
Hello, please answer with either formula or preferably with TI-84 ONLY. (No r studio, etc) thanks! For problems #13 to #18, test the given claim. Identify the null hypothesis, alternative hypothesis, test statistic, P-value, then state the conclusion about the null hypothesis, and finalconclusion that addresses the original claim.If you are using graphing calculators, show all commands. 15. In a random sample of males, it was found that 23 write with their left hands and 217 do not. In a...
Hello, please answer with either formula or preferably with TI-84 ONLY. (No r studio, etc) thanks!...
Hello, please answer with either formula or preferably with TI-84 ONLY. (No r studio, etc) thanks! 19. The data below are annual high values of Dow Jones Industrial Average and annual mean sunspot numbers for eight recent years. A sunspot number is a measure of sunspots or groups of sunspots on the surface of the sun. The Dow Jones Industrial Average is a commonly used index that is a weighted mean calculated from different stock values. (a) Use a 0.05...
Perform the following tasks on R Studio/R Construct a function called conv3 which inputs a measurement...
Perform the following tasks on R Studio/R Construct a function called conv3 which inputs a measurement in centimeters and outputs the corresponding measurement in inches. However, if a negative value is entered as an input, no conversion of unit is done and an error message is printed instead.
Hello , I have to write a thesis on international trade of agricultural products can anyone...
Hello , I have to write a thesis on international trade of agricultural products can anyone help me find a good topic to talk about please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT