Download presentation

Presentation is loading. Please wait.

Published byMarkus Denning Modified over 2 years ago

1
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 1 CS1512 Foundations of Computing Science 2 Lecture 24 Probability and statistics (5) Random number generators © J R W Hunter, 2006

2
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 2 After Easter Lectures Dr Kees van Deemter will take over Logic and HCI Same times and places Tutorials Logic and HCI Same times and places Practicals Java programming simulation – Robocode ‘take-home asessment’ Same times and places

3
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 3 Continuous Assessment Week 9 test week 9 = week after Easter vacation; worth 10% of the marks of the course; as for week 5 – test under practical exam conditions; will test your knowledge of inheritance. ‘Practical exam’ completed in your own time; worth 30% of the marks of the course; handed out in week 10; hand in by the end of week 12. Both conditional on AUT dispute being resolved; safest course is to assume that they will go ahead; if you are worried about the possible effects of the AUT action, write to the Principal and express those concerns.

4
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 4 Remember: Continuous data Divide range of observations into non-overlapping intervals (bins) Count number of observations in each bin Enzyme concentration data: 121 25 83 110 60 101 95 81 123 67 113 78 85 145 100 70 93 118 119 57 64 151 48 92 62 104 139201 68 95 Range: 25 to 201 10 bins of width 20

5
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 5 Remember: Enzyme concentrations Concentration Freq. Rel.Freq. 19.5 ≤ c < 39.5 1 0.033 39.5 ≤ c < 59.5 2 0.067 59.5 ≤ c < 79.5 7 0.233 79.5 ≤ c < 99.5 7 0.233 99.5 ≤ c < 119.5 70.233 119.5 ≤ c < 139.5 3 0.100 139.5 ≤ c < 159.5 2 0.067 159.5 ≤ c < 179.5 0 0.000 179.5 ≤ c < 199.5 0 0.000 199.5 ≤ c < 219.5 1 0.033 Totals 30 1.000

6
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 6 Relative Frequency Histogram height of the bar gives the relative frequency relative frequency

7
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 7 Density Histograms Plot relative frequency / width of the column (bin width) so that the area of the bar now gives the relative frequency relative frequency = relative frequency density bin width = 0.0165 20 = 0.233 relative frequency density

8
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 8 Addition of areas relative frequencies of values between here and here = this area

9
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 9 Increase the number of samples... and decrease the width of the bin...

10
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 10 Relative frequency as area under the curve relative frequency of values between a and b = area

11
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 11 Continuous random variable Consider a large population of individuals e.g. all males in the UK over 16 Consider a continuous attribute e.g. Height: X Select an individual at random so that any individual is as likely to be selected as any other X is said to be a continuous random variable

12
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 12 Probability density function The probability distribution of X is said to be its probability density function defined such that: P(a ≥ x > b) = area under the curve between a and b NB total area under curve must be 1.0

13
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 13 The ‘normal’ distribution Very common distribution: often called a Gaussian distribution variable measured for large number of nominally identical objects; variation assumed to be caused by a large number of factors; each factor exerts a small random positive or negative influence; e.g. height: age diet bone structure genetic influences etc. Symmetric about mean Unimodal

14
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 14 Mean Mean determines the centre of the curve: Mean = 10 Mean = 30

15
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 15 Remember: Variance Measure of spread: variance

16
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 16 Remember: Variance sample variance = s 2 sample standard deviation = s = √ variance

17
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 17 Standard deviation Standard deviation determines the ‘width’ of the curve: Std. Devn. = 2 Std. Devn. = 1

18
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 18 Remember: Cumulative frequencies Number of piglets in a litter: (discrete data) Litter size Frequency Cum. Freq 5 1 1 6 0 1 7 2 3 8 3 6 9 3 9 10 9 18 11 8 26 12 5 31 13 3 34 14 2 36 Total 36 c K = n

19
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 19 Remember: Plotting frequency cumulative frequency

20
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 20 Cumulative normal distribution For good demo, go to: http://www.vertex42.com/ExcelArticles/mc/NormalDistribution-Excel.html and download the Excel file

21
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 21 Relationship between the two distributions area under curve = 0.84

22
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 22 Probability of sample lying within mean ± 2 standard deviations x Prob Dist Cum Prob Dist 2.00.0001338300.003% 2.40.00029194700.007% 2.80.00061190200.016% 3.20.00123221900.034% 3.60.00238408800.069% 4.00.00443184800.135% 4.40.00791545200.256% 4.80.01358296900.466% 5.20.0223945300.820% 5.60.03547459301.390% 6.00.05399096702.275% 6.40.07895015803.593% 6.80.11092083505.480% 7.20.14972746608.076% 7.60.19418605511.507% 8.00.24197072515.866% 8.40.28969155321.186% 8.80.33322460327.425% 9.20.3682701434.458% 9.60.39104269442.074% 10.00.3989422850.000% Mean ( μ )= 10.0 Std. devn ( σ ) = 2.0 P ( X < μ – σ ) = 15.866% P ( X < μ – 2 σ ) = 2.275% P ( μ – 2 σ < X < μ + 2 σ ) = = (100 – 2 * 2.275)% = 94.5%

23
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 23 Probability of sample lying within mean ± 2 standard deviations 2.275% 94.5% μ – 2σμ + 2σμ

24
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 24 Uniform probability distribution Also called rectangular distribution 0.0 1.0 x P(X) 0.0 1.0 P(X < y) = 1.0 y = y y

25
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 25 Uniform probability distribution 0.0 x P(X) 0.0 P(X < a) = a P(X < b) = b P(a ≤ X < b) = b - a 1.0 a b

26
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 26 Sampling from a distribution Suppose we want a stream of numbers sampled from a given distribution. Previously we had the sample data and wanted the distribution. Now we have the distribution and want sample data. Simplest to sample from the uniform distribution between 0.0 and 1.0: 0.6282666143546787 0.18748364500938420.1345094277951323 0.0720166704579284 0.58921615443103590.9375335692470778 0.6377396244822982 0.68320290569568630.8196076287840244 0.3689553414430091 0.65972335552189590.9969146442986877 0.0867381632942044 0.42621980063130590.3064954363270612 0.7706191731891433 0.73273641267315440.6184114671445469 0.4400410508617185 0.7270704022602184... Use a random number generator

27
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 27 Random Number Generators From a given starting number (the seed) there are algorithms which will generate a series of pseudo-random numbers which are uniformly distributed: linear congruential pseudorandom number generator but you didn’t want to know this! Computers are deterministic: from a given starting point they always do the same thing; how do we get different series? start from different seeds: choose the seed yourself derive it from the computer clock (date and time of day)

28
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 28 Java support – class Math static double random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 (0.0 ≤ x < 1.0); when this method is first called, it creates a single new pseudo-random- number generator (seed derived automatically) which is used thereafter for all calls to this method and is used nowhere else. public void randGen() //demo of Math.random() { for (int i=0; i<20; i++) System.out.println(Math.random()); }

29
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 29 Java support – class java.util.Random Construct a random number generator: public Random() Creates a new random number generator; this constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor. public Random(long seed) Creates a new random number generator using a single long seed

30
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 30 Java support – class java.util.Random Get the (next) random number: public double nextDouble() just like Math.random() public int nextInt(int n) returns a pseudo-random, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive) public double nextGaussian() returns the next pseudo-random, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

31
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 31 Testing the uniformity public void testUniform(int numberOfSamples, int numberOfBins){ double[] hist = new double[numberOfBins]; double sample; int binNumber; for (int i = 0; i < numberOfSamples; i++){ sample = Math.random(); binNumber = (int) (sample * numberOfBins); hist[binNumber]++; } double relativeFrequency; for (int k = 0; k < numberOfBins; k++){ relativeFrequency = hist[k]/numberOfSamples; System.out.println(relativeFrequency); } System.out.println(); }

32
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 32 Simulating coin toss public String coinToss(int n){ String s = ""; Random random = new Random(); for (int i = 0; i < n; i++) { int t = random.nextInt(2); // i.e. t = 0 or 1 if (t == 0) // we want this to happen // with probability 0.5 s = s + "T "; else s = s + "H "; } return s; }

33
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 33 Simulating dice throw public String diceThrow(int n){ String s = ""; Random random = new Random(); for (int i = 0; i < n; i++) s = s + random.nextInt(6) + " "; return s; }

34
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 34 Simulating picking balls public String pickABall(int n){ String s = ""; Random random = new Random(); for (int i = 0; i < n; i++) { double b = random.nextDouble(); if (b < 0.3) // we want this to happen with probability 0.3 s = s + "R "; else s = s + "W "; } return s; }

35
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 35 ‘Foxes and rabbits’ simulation Rabbits and foxes in an enclosed field; example of a “predator-prey” simulation; see Barnes and Kölling, Objects first with Java, Chapter 10. The field: has a fixed number of square cells arranged in a square grid; each cell can be occupied by only one animal; animals can’t leave the field.

36
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 36 Animals All animals have: a state (alive or dead!) an age a location in the field All animals do: get older breed try to move to a new location die of old age die of overcrowding Foxes: die of hunger Rabbits: die from being eaten by a fox

37
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 37 Breeding Rabbits: BREEDING_PROBABILITY = 0.15; MAX_LITTER_SIZE = 5; private int breed() // returns size of litter (if any) { int births = 0; if (rand.nextDouble() <= BREEDING_PROBABILITY) { births = rand.nextInt(MAX_LITTER_SIZE) + 1; } return births; } Foxes: BREEDING_PROBABILITY = 0.09; MAX_LITTER_SIZE = 3;

38
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 38 Breeding probability 0.0 1.0 x P(X) 0.0 1.0 if (rand.nextDouble() <= BREEDING_PROBABILITY)...

39
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 39 Number of births with each litter size equally likely 0 MAX_LITTER_SIZE x P(X) 0.0 1 / MAX_LITTER_SIZE births = rand.nextInt(MAX_LITTER_SIZE) + 1 123...

40
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 40 Number of births with different probabilities of litter sizes 0 MAX_LITTER_SIZE x P(X) 0.0 123... p3p3 p…p… p2p2 p1p1 p MAX Given p 1, p 2,... p MAX, how do you use a random number generator to generate a litter size?

41
CS1512 www.csd.abdn.ac.uk/~jhunter/teaching/CS1512/lectures/ 41 Have a good Easter!

Similar presentations

Presentation is loading. Please wait....

OK

The basics for simulations

The basics for simulations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on resources and development Ppt on credit default swaps news Ppt on trans-siberian railway Lecture ppt on computer graphics Ppt on bluetooth broadcasting system Ppt on basic proportionality theorem for class 10 Ppt on eisenmenger syndrome and pulmonary Ppt on conceptual art emphasizes Ppt on operation research game theory Free ppt on save girl child