SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Stat405                  Simulation


                               Hadley Wickham
Wednesday, 23 September 2009
1. Simulation
               2. Mathematical approach
               3. Homework
               4. Feedback




Wednesday, 23 September 2009
Symposium?



Wednesday, 23 September 2009
Bootstrap estimate
                   Casino claims that slot machines have prize
                   payout of 92%. Is this claim true?
                   Payoff for 345 attempts is 0.67.
                   What does that tell us about true
                   distribution? Can now generate payoffs
                   distribution for that many pulls. Is 0.92
                   likely or unlikely?


Wednesday, 23 September 2009
calculate_prize <- function(windows) {
       payoffs <- c("DD" = 800, "7" = 80, "BBB" = 40,
         "BB" = 25, "B" = 10, "C" = 10, "0" = 0)

         same <- length(unique(windows)) == 1
         allbars <- all(windows %in% c("B", "BB", "BBB"))

         if (same) {
           prize <- payoffs[windows[1]]
         } else if (allbars) {
           prize <- 5
         } else {
           cherries <- sum(windows == "C")
           diamonds <- sum(windows == "DD")

             prize <- c(0, 2, 5)[cherries + 1] *
               c(1, 2, 4)[diamonds + 1]
         }
         prize
     }

Wednesday, 23 September 2009
Your turn
                   Now want to generate a new draw from the
                   same distribution (a bootstrap sample).
                   Write a function that returns the prize from
                   a randomly generated new draw. Call the
                   function random_prize
                   Hint: sample(slot$w1, 1) will draw a single
                   sample randomly from the original data


Wednesday, 23 September 2009
slots <- read.csv("slots.csv", stringsAsFactors = F)

     w1 <- sample(slots$w1, 1)
     w2 <- sample(slots$w2, 1)
     w3 <- sample(slots$w3, 1)

     calculate_prize(c(w1, w2, w3))




Wednesday, 23 September 2009
random_prize <- function() {
       w1 <- sample(slots$w1, 1)
       w2 <- sample(slots$w2, 1)
       w3 <- sample(slots$w3, 1)

           calculate_prize(c(w1, w2, w3))
     }

     # What is the implicit assumption here?
     # How could we test that assumption?



Wednesday, 23 September 2009
Your turn

                   Write a function to do this n times
                   Use a for loop: create an empty vector,
                   and then ïŹll with values
                   Draw a histogram of the results




Wednesday, 23 September 2009
n <- 100
     prizes <- rep(NA, n)

     for(i in seq_along(prizes)) {
       prizes[i] <- random_prize()
     }




Wednesday, 23 September 2009
random_prizes <- function(n) {
       prizes <- rep(NA, n)

           for(i in seq_along(prizes)) {
             prizes[i] <- random_prize()
           }
           prizes
     }

     library(ggplot2)
     qplot(random_prizes(100))




Wednesday, 23 September 2009
Your turn
                   Create a function that returns the payoff
                   (mean prize) for n draws (payoff)
                   Then create a function that draws from
                   the distribution of mean payoffs m times
                   (payoff_distr)
                   Explore the distribution. What happens
                   as m increases? What about n?


Wednesday, 23 September 2009
payoff <- function(n) {
       mean(random_prizes(n))
     }

     m <- 100
     n <- 10

     payoffs <- rep(NA, m)
       for(i in seq_along(payoffs)) {
       payoffs[i] <- payoff(n)
     }

     qplot(payoffs, binwidth = 0.1)


Wednesday, 23 September 2009
payoff_distr <- function(m, n) {
       payoffs <- rep(NA, m)
       for(i in seq_along(payoffs)) {
         payoffs[i] <- payoff(n)
       }
       payoffs
     }

     dist <- payoff_distr(100, 1000)
     qplot(dist, binwidth = 0.05)




Wednesday, 23 September 2009
# Speeding things up a bit
     system.time(payoff_distr(200, 100))

     payoff <- function(n) {
       w1 <- sample(slots$w1, n, rep = T)
       w2 <- sample(slots$w2, n, rep = T)
       w3 <- sample(slots$w2, n, rep = T)

          prizes <- rep(NA, n)
          for(i in seq_along(prizes)) {
            prizes[i] <- calculate_prize(c(w1[i], w2[i], w3[i]))
          }
          mean(prizes)
     }
     system.time(payoff_distr(200, 100))

Wednesday, 23 September 2009
# Me - to save time
     payoffs <- payoff_distr(10000, 345)
     save(payoffs, file = "payoffs.rdata")

     # You
     load("payoffs.rdata")
     str(payoffs)




Wednesday, 23 September 2009
qplot(payoffs, binwidth = 0.02)

     mean(payoffs)
     sd(payoffs)
     mean(payoffs < mean(slots$prize))
     quantile(payoffs, c(0.025, 0.975))
     mean(payoffs) + c(-2, 2) * sd(payoffs)

     t.test(slots$prize, mu = 0.92)
     sd(slots$prize) / sqrt(nrow(slots))




Wednesday, 23 September 2009
Mathematical
                                approach

                   Why are we doing this simulation? Could
                   work out the expected value and variance
                   mathematically. So let’s do it!
                   Simplifying assumption: slots are iid.




Wednesday, 23 September 2009
# Calculate empirical distribution
     dist <- table(c(slots$w1, slots$w2, slots$w3))
     dist <- dist / sum(dist)

     distdf <- as.data.frame(dist)
     names(distdf) <- c("slot", "probability")
     distdf$slot <- as.character(distdf$slot)




Wednesday, 23 September 2009
poss <- with(distdf, expand.grid(
        w1 = slot, w2 = slot, w3 = slot,
        stringsAsFactors = FALSE
     ))

     poss$prize <- NA
     for(i in seq_len(nrow(poss))) {
       window <- as.character(poss[i, 1:3])
       poss$prize[i] <- calculate_prize(window)
     }




Wednesday, 23 September 2009
Your turn
                   How can you calculate the probability of each
                   combination?
                   (Hint: think about subsetting. More hint:
                   think about the table and character
                   subsetting. Final hint: you can do this in one
                   line of code)
                   Then work out the expected value and
                   variance.


Wednesday, 23 September 2009
poss$prob <- with(poss,
       dist[w1] * dist[w2] * dist[w3])

     (poss_mean <- with(poss, sum(prob * prize)))

     # How do we determine the variance of this
     # estimator?




Wednesday, 23 September 2009

Weitere Àhnliche Inhalte

Was ist angesagt?

Artdm170 week12 user_interaction
Artdm170 week12 user_interactionArtdm170 week12 user_interaction
Artdm170 week12 user_interaction
Gilbert Guerrero
 
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
Kyuseok Hwang(allosha)
 

Was ist angesagt? (20)

The Web map stack on Django
The Web map stack on DjangoThe Web map stack on Django
The Web map stack on Django
 
Maximizing performance of 3 d user generated assets in unity
Maximizing performance of 3 d user generated assets in unityMaximizing performance of 3 d user generated assets in unity
Maximizing performance of 3 d user generated assets in unity
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
 
Integration of Google-map in Rails Application
Integration of Google-map in Rails ApplicationIntegration of Google-map in Rails Application
Integration of Google-map in Rails Application
 
Next-Gen shaders (2008)
Next-Gen shaders (2008)Next-Gen shaders (2008)
Next-Gen shaders (2008)
 
Artdm170 week12 user_interaction
Artdm170 week12 user_interactionArtdm170 week12 user_interaction
Artdm170 week12 user_interaction
 
08 Functions
08 Functions08 Functions
08 Functions
 
CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction
CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction
CDAT - cdms2, maskes, cdscan, cdutil, genutil - Introduction
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Sharbani bhattacharya sacta 2014
Sharbani bhattacharya sacta 2014Sharbani bhattacharya sacta 2014
Sharbani bhattacharya sacta 2014
 
Trident International Graphics Workshop 2014 5/5
Trident International Graphics Workshop 2014 5/5Trident International Graphics Workshop 2014 5/5
Trident International Graphics Workshop 2014 5/5
 
Epsrcws08 campbell isvm_01
Epsrcws08 campbell isvm_01Epsrcws08 campbell isvm_01
Epsrcws08 campbell isvm_01
 
Softmax
SoftmaxSoftmax
Softmax
 
NCCU CPDA Lecture 12 Attribute Based Encryption
NCCU CPDA Lecture 12 Attribute Based EncryptionNCCU CPDA Lecture 12 Attribute Based Encryption
NCCU CPDA Lecture 12 Attribute Based Encryption
 
[112] 모버ᄋᅔᆯ ᄒá…Șᆫ겨ᆌᄋᅊᄉᅄ ᄉᅔᆯᄉᅔ건 Portrait Segmentation ᄀᅟ현허ᄀᅔ
[112] 모버ᄋᅔᆯ ᄒá…Șᆫ겨ᆌᄋᅊᄉᅄ ᄉᅔᆯᄉᅔ건 Portrait Segmentation ᄀᅟ현허ᄀᅔ[112] 모버ᄋᅔᆯ ᄒá…Șᆫ겨ᆌᄋᅊᄉᅄ ᄉᅔᆯᄉᅔ건 Portrait Segmentation ᄀᅟ현허ᄀᅔ
[112] 모버ᄋᅔᆯ ᄒá…Șᆫ겨ᆌᄋᅊᄉᅄ ᄉᅔᆯᄉᅔ건 Portrait Segmentation ᄀᅟ현허ᄀᅔ
 
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
 
Support Vector Machine Derivative
Support Vector Machine DerivativeSupport Vector Machine Derivative
Support Vector Machine Derivative
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics Math
 
Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 

Ähnlich wie 09 Simulation

09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
Hadley Wickham
 
07 Problem Solving
07 Problem Solving07 Problem Solving
07 Problem Solving
Hadley Wickham
 
11 Data Structures
11 Data Structures11 Data Structures
11 Data Structures
Hadley Wickham
 
Presenting Your Code
Presenting Your CodePresenting Your Code
Presenting Your Code
moto
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
asguna
 
Trading with opensource tools, two years later
Trading with opensource tools, two years laterTrading with opensource tools, two years later
Trading with opensource tools, two years later
clkao
 

Ähnlich wie 09 Simulation (19)

10 simulation
10 simulation10 simulation
10 simulation
 
10 simulation
10 simulation10 simulation
10 simulation
 
09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
 
07 Problem Solving
07 Problem Solving07 Problem Solving
07 Problem Solving
 
Svm map reduce_slides
Svm map reduce_slidesSvm map reduce_slides
Svm map reduce_slides
 
Visualising Big Data
Visualising Big DataVisualising Big Data
Visualising Big Data
 
R Workshop for Beginners
R Workshop for BeginnersR Workshop for Beginners
R Workshop for Beginners
 
집닚지성 í”„ëĄœê·žëž˜ë° 08-가êČ©ëȘšëžë§
집닚지성 í”„ëĄœê·žëž˜ë° 08-가êČ©ëȘšëžë§ì§‘닚지성 í”„ëĄœê·žëž˜ë° 08-가êČ©ëȘšëžë§
집닚지성 í”„ëĄœê·žëž˜ë° 08-가êČ©ëȘšëžë§
 
Assignment 5.2.pdf
Assignment 5.2.pdfAssignment 5.2.pdf
Assignment 5.2.pdf
 
Stop Guessing and Start Measuring - Benchmarking Practice (Poly Version)
 Stop Guessing and Start Measuring - Benchmarking Practice (Poly Version) Stop Guessing and Start Measuring - Benchmarking Practice (Poly Version)
Stop Guessing and Start Measuring - Benchmarking Practice (Poly Version)
 
11 Data Structures
11 Data Structures11 Data Structures
11 Data Structures
 
alexnet.pdf
alexnet.pdfalexnet.pdf
alexnet.pdf
 
Test s velocity_15_5_4
Test s velocity_15_5_4Test s velocity_15_5_4
Test s velocity_15_5_4
 
Presenting Your Code
Presenting Your CodePresenting Your Code
Presenting Your Code
 
BUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfBUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdf
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
 
Apache Sparkℱ Applications the Easy Way - Pierre Borckmans
Apache Sparkℱ Applications the Easy Way - Pierre BorckmansApache Sparkℱ Applications the Easy Way - Pierre Borckmans
Apache Sparkℱ Applications the Easy Way - Pierre Borckmans
 
Trading with opensource tools, two years later
Trading with opensource tools, two years laterTrading with opensource tools, two years later
Trading with opensource tools, two years later
 
Machine Learning with Go
Machine Learning with GoMachine Learning with Go
Machine Learning with Go
 

Mehr von Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
24 modelling
24 modelling24 modelling
24 modelling
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
R packages
R packagesR packages
R packages
 
22 spam
22 spam22 spam
22 spam
 
21 spam
21 spam21 spam
21 spam
 
20 date-times
20 date-times20 date-times
20 date-times
 
19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
17 polishing
17 polishing17 polishing
17 polishing
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
14 case-study
14 case-study14 case-study
14 case-study
 
13 case-study
13 case-study13 case-study
13 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
07 problem-solving
07 problem-solving07 problem-solving
07 problem-solving
 

KĂŒrzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

KĂŒrzlich hochgeladen (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

09 Simulation

  • 1. Stat405 Simulation Hadley Wickham Wednesday, 23 September 2009
  • 2. 1. Simulation 2. Mathematical approach 3. Homework 4. Feedback Wednesday, 23 September 2009
  • 4. Bootstrap estimate Casino claims that slot machines have prize payout of 92%. Is this claim true? Payoff for 345 attempts is 0.67. What does that tell us about true distribution? Can now generate payoffs distribution for that many pulls. Is 0.92 likely or unlikely? Wednesday, 23 September 2009
  • 5. calculate_prize <- function(windows) { payoffs <- c("DD" = 800, "7" = 80, "BBB" = 40, "BB" = 25, "B" = 10, "C" = 10, "0" = 0) same <- length(unique(windows)) == 1 allbars <- all(windows %in% c("B", "BB", "BBB")) if (same) { prize <- payoffs[windows[1]] } else if (allbars) { prize <- 5 } else { cherries <- sum(windows == "C") diamonds <- sum(windows == "DD") prize <- c(0, 2, 5)[cherries + 1] * c(1, 2, 4)[diamonds + 1] } prize } Wednesday, 23 September 2009
  • 6. Your turn Now want to generate a new draw from the same distribution (a bootstrap sample). Write a function that returns the prize from a randomly generated new draw. Call the function random_prize Hint: sample(slot$w1, 1) will draw a single sample randomly from the original data Wednesday, 23 September 2009
  • 7. slots <- read.csv("slots.csv", stringsAsFactors = F) w1 <- sample(slots$w1, 1) w2 <- sample(slots$w2, 1) w3 <- sample(slots$w3, 1) calculate_prize(c(w1, w2, w3)) Wednesday, 23 September 2009
  • 8. random_prize <- function() { w1 <- sample(slots$w1, 1) w2 <- sample(slots$w2, 1) w3 <- sample(slots$w3, 1) calculate_prize(c(w1, w2, w3)) } # What is the implicit assumption here? # How could we test that assumption? Wednesday, 23 September 2009
  • 9. Your turn Write a function to do this n times Use a for loop: create an empty vector, and then ïŹll with values Draw a histogram of the results Wednesday, 23 September 2009
  • 10. n <- 100 prizes <- rep(NA, n) for(i in seq_along(prizes)) { prizes[i] <- random_prize() } Wednesday, 23 September 2009
  • 11. random_prizes <- function(n) { prizes <- rep(NA, n) for(i in seq_along(prizes)) { prizes[i] <- random_prize() } prizes } library(ggplot2) qplot(random_prizes(100)) Wednesday, 23 September 2009
  • 12. Your turn Create a function that returns the payoff (mean prize) for n draws (payoff) Then create a function that draws from the distribution of mean payoffs m times (payoff_distr) Explore the distribution. What happens as m increases? What about n? Wednesday, 23 September 2009
  • 13. payoff <- function(n) { mean(random_prizes(n)) } m <- 100 n <- 10 payoffs <- rep(NA, m) for(i in seq_along(payoffs)) { payoffs[i] <- payoff(n) } qplot(payoffs, binwidth = 0.1) Wednesday, 23 September 2009
  • 14. payoff_distr <- function(m, n) { payoffs <- rep(NA, m) for(i in seq_along(payoffs)) { payoffs[i] <- payoff(n) } payoffs } dist <- payoff_distr(100, 1000) qplot(dist, binwidth = 0.05) Wednesday, 23 September 2009
  • 15. # Speeding things up a bit system.time(payoff_distr(200, 100)) payoff <- function(n) { w1 <- sample(slots$w1, n, rep = T) w2 <- sample(slots$w2, n, rep = T) w3 <- sample(slots$w2, n, rep = T) prizes <- rep(NA, n) for(i in seq_along(prizes)) { prizes[i] <- calculate_prize(c(w1[i], w2[i], w3[i])) } mean(prizes) } system.time(payoff_distr(200, 100)) Wednesday, 23 September 2009
  • 16. # Me - to save time payoffs <- payoff_distr(10000, 345) save(payoffs, file = "payoffs.rdata") # You load("payoffs.rdata") str(payoffs) Wednesday, 23 September 2009
  • 17. qplot(payoffs, binwidth = 0.02) mean(payoffs) sd(payoffs) mean(payoffs < mean(slots$prize)) quantile(payoffs, c(0.025, 0.975)) mean(payoffs) + c(-2, 2) * sd(payoffs) t.test(slots$prize, mu = 0.92) sd(slots$prize) / sqrt(nrow(slots)) Wednesday, 23 September 2009
  • 18. Mathematical approach Why are we doing this simulation? Could work out the expected value and variance mathematically. So let’s do it! Simplifying assumption: slots are iid. Wednesday, 23 September 2009
  • 19. # Calculate empirical distribution dist <- table(c(slots$w1, slots$w2, slots$w3)) dist <- dist / sum(dist) distdf <- as.data.frame(dist) names(distdf) <- c("slot", "probability") distdf$slot <- as.character(distdf$slot) Wednesday, 23 September 2009
  • 20. poss <- with(distdf, expand.grid( w1 = slot, w2 = slot, w3 = slot, stringsAsFactors = FALSE )) poss$prize <- NA for(i in seq_len(nrow(poss))) { window <- as.character(poss[i, 1:3]) poss$prize[i] <- calculate_prize(window) } Wednesday, 23 September 2009
  • 21. Your turn How can you calculate the probability of each combination? (Hint: think about subsetting. More hint: think about the table and character subsetting. Final hint: you can do this in one line of code) Then work out the expected value and variance. Wednesday, 23 September 2009
  • 22. poss$prob <- with(poss, dist[w1] * dist[w2] * dist[w3]) (poss_mean <- with(poss, sum(prob * prize))) # How do we determine the variance of this # estimator? Wednesday, 23 September 2009