Download presentation

Presentation is loading. Please wait.

Published byMarkus Denning Modified over 2 years ago

1
CS CS1512 Foundations of Computing Science 2 Lecture 24 Probability and statistics (5) Random number generators © J R W Hunter, 2006

2
CS 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
CS 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
CS Remember: Continuous data Divide range of observations into non-overlapping intervals (bins) Count number of observations in each bin Enzyme concentration data: Range: 25 to bins of width 20

5
CS Remember: Enzyme concentrations Concentration Freq. Rel.Freq ≤ c < ≤ c < ≤ c < ≤ c < ≤ c < ≤ c < ≤ c < ≤ c < ≤ c < ≤ c < Totals

6
CS Relative Frequency Histogram height of the bar gives the relative frequency relative frequency

7
CS 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 = 20 = relative frequency density

8
CS Addition of areas relative frequencies of values between here and here = this area

9
CS Increase the number of samples... and decrease the width of the bin...

10
CS Relative frequency as area under the curve relative frequency of values between a and b = area

11
CS 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
CS 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
CS 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
CS Mean Mean determines the centre of the curve: Mean = 10 Mean = 30

15
CS Remember: Variance Measure of spread: variance

16
CS Remember: Variance sample variance = s 2 sample standard deviation = s = √ variance

17
CS Standard deviation Standard deviation determines the ‘width’ of the curve: Std. Devn. = 2 Std. Devn. = 1

18
CS Remember: Cumulative frequencies Number of piglets in a litter: (discrete data) Litter size Frequency Cum. Freq Total 36 c K = n

19
CS Remember: Plotting frequency cumulative frequency

20
CS Cumulative normal distribution For good demo, go to: and download the Excel file

21
CS Relationship between the two distributions area under curve = 0.84

22
CS Probability of sample lying within mean ± 2 standard deviations x Prob Dist Cum Prob Dist % % % % % % % % % % % % % % % % % % % % % Mean ( μ )= 10.0 Std. devn ( σ ) = 2.0 P ( X < μ – σ ) = % P ( X < μ – 2 σ ) = 2.275% P ( μ – 2 σ < X < μ + 2 σ ) = = (100 – 2 * 2.275)% = 94.5%

23
CS Probability of sample lying within mean ± 2 standard deviations 2.275% 94.5% μ – 2σμ + 2σμ

24
CS Uniform probability distribution Also called rectangular distribution x P(X) P(X < y) = 1.0 y = y y

25
CS 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
CS 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: Use a random number generator

27
CS 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
CS 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
CS 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
CS 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
CS 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
CS 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
CS 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
CS 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
CS ‘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
CS 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
CS 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
CS Breeding probability x P(X) if (rand.nextDouble() <= BREEDING_PROBABILITY)...

39
CS Number of births with each litter size equally likely 0 MAX_LITTER_SIZE x P(X) / MAX_LITTER_SIZE births = rand.nextInt(MAX_LITTER_SIZE)

40
CS Number of births with different probabilities of litter sizes 0 MAX_LITTER_SIZE x P(X) 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
CS Have a good Easter!

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google