Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Simulation - Generating Continuous Random Variables

5,378 views

Published on

  • Be the first to comment

Simulation - Generating Continuous Random Variables

  1. 1. Chair of Information Systems IV (ERIS)Institute for Enterprise Systems (InES)16 April 2013, 10.15 am – 11.15 amMartin KretzerPhone: +49 621 181 3276E-Mail: kretzer@es.uni-mannheim.deGenerating Continuous Random Variables(IS 802 “Simulation”, Section 3)
  2. 2. Agenda 2Agenda1 Introduction2 Selected Methods3 Generating Important Distributions4 Summary5 Exercises (Simulations using R)Generating Continuous Random Variables
  3. 3. Why “Generation of Continuous Random Variables”? 3 Simulations are able to address magnifold questions, e.g.: How many customers will we have? How long does it take to handle arriving customers? Will queues develop? Problem: Simulations require random variables Uniformly distributed random variables are often not enough, but can be used for generation of further distributions Uniformly distributed random variables can be generated, e.g., modulo function (section 2) Example: Customers in a store arrive randomly, between 10 and 20 minutes apart, normally / exponentiallydistributed on this interval. Goal: Generate random variables Discrete random variables (section 2) Continuous random variables (univariate) (section 3) Each method for generating a discrete random variable has its analogue in the continuous case! Multivariate normal distributed variables (section 4)Generating Continuous Random VariablesDiscrete Variables Continuous Variables Finite set of values Example: population 0, 1, 2, 3, …
  4. 4. Learning Outcomes 4Generating Continuous Random Variables Generate random variables using theInverse-Transform and Acceptance-RejectionMethod Develop algorithms for simulatingExponential, Normal, Poisson andNonhomogeneous Poisson distributions Perform simulations using R
  5. 5. Agenda 5Agenda1 Introduction2 Selected Methods2.1 Inverse Transform Method2.2 Acceptance Rejection Method3 Generating Important Distributions4 Summary5 Exercises (Simulations using R)Generating Continuous Random Variables
  6. 6. Inverse Transform Method: Proof 6Generating Continuous Random Variables
  7. 7. Inverse Transform Method: Example 7Generating Continuous Random Variables3xxF31uuF3n
  8. 8. Acceptance-Rejection Method 8Generating Continuous Random Variables
  9. 9. Acceptance-Rejection Method: Example 9Generating Continuous Random Variables3120 xxxf
  10. 10. Agenda 10Agenda1 Introduction2 Selected Methods3 Generating Important Distributions3.1 Exponential Distribution3.2 Normal Distribution3.3 Poisson Distribution3.4 Nonhomogeneous Poisson Distribution4 Summary5 Exercises (Simulations using R)Generating Continuous Random Variables
  11. 11. Exponential Distribution (1/2) 11Generating Continuous Random Variables
  12. 12. Exponential Distribution (2/2) 12Generating Continuous Random VariablesxexFuuF ln111
  13. 13. Normal Distribution (1/4) 13Generating Continuous Random Variables
  14. 14. Normal Distribution (2/4) 14Generating Continuous Random Variables5.0;5.0;*2222UYUYXi
  15. 15. 5.0;5.0;*11UYUYXiNormal Distribution (3/4) 15Generating Continuous Random Variables
  16. 16. Normal Distribution (4/4) 16 Graphical illustrationGenerating Continuous Random Variables)()(xgxfxh2222xexfxexg
  17. 17. Poisson Process 17Generating Continuous Random Variables
  18. 18. Comparison: Homogeneous and Nonhomogeneous Poisson Process 18Generating Continuous Random VariablesNonhomogeneous Poisson Process(Homogeneous) Poisson ProcessPreviously: Now:
  19. 19. Nonhomogeneous Poisson Process (1/2) 19Generating Continuous Random Variablessimulation of aPoisson processrandomlycounting events
  20. 20. Nonhomogeneous Poisson Process (2/2) 20Generating Continuous Random Variables
  21. 21. Agenda 21Agenda1 Introduction2 Selected Methods3 Generating Important Distributions4 Summary5 Exercises (Simulations using R)Generating Continuous Random Variables
  22. 22. Summary 22Generating Continuous Random VariablesDistributionInverse TransformMethodAcceptance-RejectionMethodExponential XNormal X(Homogeneous)PoissonXNonhomogeneousPoissonX
  23. 23. Agenda 23Agenda1 Introduction2 Selected Methods3 Generating Important Distributions4 Summary5 Exercises (Simulations using R)5.1 Exercise 1 – Inverse Transform Method (Exponential Distr.)5.2 Exercise 2 – Acceptance Rejection Method (Normal Distr.)Generating Continuous Random Variables
  24. 24. Exercise 1 – Task 24Generating Continuous Random Variables
  25. 25. Exercise 1 – Live Demonstration 25 R Version: 3.0.0 (Windows 32-bit) http://cran.rstudio.com/ RStudio Version: 0.97.336 http://www.rstudio.com/ide/download/Generating Continuous Random Variables
  26. 26. Exercise 1 – Code 26####################################### Author: Martin Kretzer### Date: 11 April 2012### Descr: Simulation of an exponential distribution + inverse transform method####################################### 0. Functions:### Performs simulations and returns a list of all resulting valuesgetSimulationsVector<- function( numberOfSimulations, numberOfCustomers, mean,probability ){lambda<- 1/meansimulationsVector<- c()for( i in 1:numberOfSimulations ){claimsPerSimulationVector<- c()#uniformVars<- runif( numberOfCustomers )for( j in 1:numberOfCustomers ){uniformVarProbability <- runif(1)if( uniformVarProbability <= probability ){uniformVarAlgorithm <- runif(1)claim<- -(1/lambda)*log( uniformVarAlgorithm )claimsPerSimulationVector<- c( claimsPerSimulationVector, claim )}}simulationsVector<- c(simulationsVector, sum(claimsPerSimulationVector) )}return(simulationsVector)};Generating Continuous Random Variables### Probability that all values are above the variable limitgetProbability<- function( simulationsVector, limit ){eventcount<- 0for( i in 1:length( simulationsVector ) ){if( simulationsVector[[i]] >= limit ){eventcount<- eventcount + 1}}probability<- eventcount / length( simulationsVector )return(probability)};####################################### 1. Set constant variables:numberOfSimulations<- 100000;numberOfCustomers<- 1000mean<- 800;limit<- 50000;probability<- 0.05;### 2. Execute and output variables:simulationsVector <- getSimulationsVector( numberOfSimulations,numberOfCustomers, mean, probability );simulationsVector; #R maximum outputs 10000 values### 3. Execute and output variables:hist(simulationsVector, 250, xlab="Sum of allclaims",ylab="Simulations",main="Exercise: Transform Method and Exp.Distribution");### 4. Calculate and output probability:probability<- getProbability( simulationsVector, limit );probability;
  27. 27. Exercise 1 – Solution 27Generating Continuous Random Variables
  28. 28. 5.0;5.0;*11UYUYXiExercise 2 – Task 28Generating Continuous Random Variables
  29. 29. Exercise 2 – Live Demonstration 29 R Version: 3.0.0 (Windows 32-bit) http://cran.rstudio.com/ RStudio Version: 0.97.336 http://www.rstudio.com/ide/download/Generating Continuous Random Variables
  30. 30. Exercise 2 – Code 30####################################### Author: Martin Kretzer### Date: 12 April 2012### Descr: Simulation of a half-normal distribution + accept/reject####################################### 0. Function:### Performs simulations and returns a vector of all simulated valuesgetSimulationsVector<- function( numberOfSimulations, mean, stdDev ){# Define variablessimulations.vector<- rep(0, numberOfSimulations)# Generate the exponential variable y1y1<- runif(1)y1<- -log(y1)for( i in 1:numberOfSimulations ){# Generate the independent exp. variable y2y2<- runif(1)y2<- -log(y2)# Acceptance-Rejection procedurewhile( y2 < (y1-1)^2/2 ){# if rejected, generate two new independent exp. variables y1 and# y2 and repeat procedurey1<- runif(1)y1<- -log(y1)y2<- runif(1)y2<- -log(y2)}Generating Continuous Random Variablesy3<- y2-(y1-1)^2/2# Generate a random number U and store the simulated variable y1# in simulations.vectoru<- runif(1)if( u <= 0.5 ){simulations.vector[i]<- mean + (y1*stdDev)}else{simulations.vector[i]<- mean + (-y1*stdDev)}y1<- y3}return(simulations.vector)};####################################### Function End####################################simulations.vector<- getSimulationsVector( 100000, 10, 3 )summary( simulations.vector )sd( simulations.vector )hist(simulations.vector, 250,xlab="",ylab="Simulations",main="Exercise: Acceptance-Rejection Methodand Normal Distribution");
  31. 31. Exercise 2 – Solution 31 Executed simulations: 100,000 Estimated mean: 10.010 Estimated standard deviation: 2.993299Generating Continuous Random Variables
  32. 32.  Thank you for your attention.32Generating Continuous Random Variables
  33. 33. Appendix 33Appendix1 Backup Slides2 BibliographyGenerating Continuous Random Variables
  34. 34. Poisson Process Introduction (1/2) 34Generating Continuous Random Variables
  35. 35. Poisson Process Introduction (2/2) 35Generating Continuous Random Variables
  36. 36. Nonhomogeneous Poisson Process: Option 1 Improvement 36Generating Continuous Random Variables
  37. 37. Nonhomogeneous Poisson Process: Option 2 37Generating Continuous Random Variables
  38. 38. Appendix 38Appendix1 Backup Slides2 BibliographyGenerating Continuous Random Variables
  39. 39. Bibliography 39 ETH Zürich. 2013. The Uniform Distribution, ETH Zürich, Zürich, CH (available online athttp://stat.ethz.ch/R-manual/R-devel/library/stats/html/Uniform.html ). Haugh, M. 2010. “Generating Random Variables and Stochastic Processes,” ColumbiaUniversity, US (available online at http://www.columbia.edu/~mh2078/MCS_ Generate_RVars .pdf ). The R Core Team 2013. “R: A Language and Environment for Statistical Computing.Reference Index. Version 3.0.0 (2013-04-03),” (part of the R download). R-Project 2013. “Probability Distributions,” (available online at http://cran.r-project.org/web/views/Distributions.html ). Ross, S. M. 2013. Simulation, 5th ed., San Diego, CA: Academic Press. Sigman, K. 2009. “Inverse Transform Method,” Columbia University, US (available online athttp://www.columbia.edu/~ks20/4404-13-Spring/4404-Notes-ITM.pdf ). Valdez, E. A. 2008. “Generating Continuous Random Variables,” University of Connecticut,US (available at http://www.math.uconn.edu/~valdez/math276s08/Math276-Week45.pdf ). Venables, W. N., Smith, D. M., and the R Core Team. 2013. “An Introduction to R. Notes onR: A Programming Environment for Data Analysis and Graphics Version 3.0.0 (2013-04-03),” (part of the R download).Generating Continuous Random Variables

×