Top Banner
Computational modeling Instructor : Cedric Weber Course : 4CCP1000 Lecture 4 : Central Limit Theorem Theory: Normal distribution Programming: Arrays
31

① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Mar 10, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Computational  modeling  

Instructor  :    Cedric  Weber  Course        :    4CCP1000  

Lecture 4 : Central Limit Theorem

Theory:

①  Normal distribution

Programming:

①  Arrays

Page 2: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Class/Week   Chapter   Topic   Milestones  

1   Monte  Carlo   UNIX  system  /  Fortran  

2   Monte  Carlo   Fibonacci  sequence  

3   Monte  Carlo   Random  variables  

4   Monte  Carlo   Central  Limit  Theorem  

5   Monte  Carlo   “Monte  Carlo”  integration   Milestone  1  

6   Differential  equations   The  Pendulum  

7   Differential  equations   A  Quantum  Particle  in  a  box  

8   Differential  equations   The  Tacoma  bridge   Milestone  2  

9   Linear  Algebra   System  of  equations  

10   Linear  Algebra   Matrix  operations   Milestone  3  

2  

Schedule  

Page 3: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

1.  Summing up sequences with a do loop " " "ý""

2.  Defining your own functions " ý"

3.  Using a random generator to produce random values ý"

4.  Generating files and plotting them with xmgrace " "ý

5.  Using modules to build up your own library" " "ý"   3  

What  did  you  learn  last  time?  

Page 4: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Lecture  1:  *  Introduction  

*  Lecture  2:    *  Fibonacci  sequence  /  do  loop  

*  Lecture  3:  *  Random  variables  /  functions  

*  Lecture  4:  *  Summing  up  random  variables  /  

multiple  events  statistics  4  

Where  are  we  going?  

0%  10%  20%  30%  40%  50%  60%  70%  80%  90%  100%  

wee

k  1  

wee

k  2  

wee

k  3  

wee

k  4  

wee

k  5  

wee

k  6  

wee

k  7  

wee

k  8  

wee

k  9  

wee

k  10  

Milestone  1  :  Monte  Carlo  integration  

Page 5: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Part  1  :  Theory  Central  limit  theorem  

5  

Page 6: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

q Simple idea: lets define the outcome of an experiment Xi , and the result of the experiment is either Xi=0 or Xi=1. When I repeat the experiment, I sometimes get Xi=0, and sometimes I obtain Xi=1"

q  Letʼs think of Xi as a coin, which is flipped (head/tail). "

q  Experiment: flip the coin 1000 times"

q  how many “tail”, how many “head”?"

q  Definition of a Probability: "

q P(head) = N(head) / total P(tail) = N(tail) / total ""

q  Reference: The Life and Times of the Central Limit Theorem, William J. Adams" 6  

Jacob  Bernoulli  (1654-­‐1705):  Game  Theory  

= 500 = 500

Page 7: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Now,  what  if  I  throw  many  coins  at  the  same  time?  *  Each  coin  is  labeled  with  an  index  *  We  attribute  the  value  0  to  tail,  1  to  head    

*                                                                                                                   0                        0                      1                    0                      0                      1                      1  *  To  describe  the  outcome  of  this  experiment,  we  define  the  variable  Sn  :    

 *  In  the  experiment  above  I  get  :    Sn  =  3  *  Now,  I  throw  the  coins  again  …    *  We  get  (fill  the  blank)  :          Sn=  […]  ?              1                    1                        1                    0                    0                      1                    0  

7  

Flipping  many  coins  …  

Sn = X1 +X2 + ...+Xn

X = (X1, X2, X3, X4, X5, X6, X7)

Page 8: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  I  repeat  the  experiments  many  times  …  I  throw  7  coins,  count  how  many  heads  I  obtain,  I  do  it  again  *  I  obtain  the  sequence  :  *  S=3,  S=4,  S=1,  S=7,  S=4,  S=3,  S=4,  S=5,  S=4,  S=0,  S=2  ……………..  

*  I  count  how  many  times  I  obtain  S=0,  how  many  times  I  obtain  S=1,  …  ,  and  how  many  times  I  obtain  S=7  

8  

Central  limit  theorem  

0

25

50

75

100

S=0 S=1 S=2 S=3 S=4 S=5 S=6 S=7

Number of occurence

Question:  if  I  throw  the  coins  700  times,  how  many  times  will  you  obtain  the  result  S=0?    As  many  times  as  you  obtain  S=1,  S=2,  S=3,  S=4,  S=5,  S=6  and  S=7  right?  

Page 9: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  This  program  will  be  written  in  the  practice  session  *  Result  :  The  number  of  S1,S2,S3…S7  obtained  

 

*  This  histogram  gives  the  probability  P(S)  to  obtain  S,  if  we  divide  the  number  of  occurrence  of  each  Si  by  the  total  number  of  experiments  (700  in  this  case)  *  Who  can  spot  something  wrong  in  the  histogram  ?  

9  

Simulating  this  experiment  with  a  computer  

0

50

100

150

200

S=0 S=1 S=2 S=3 S=4 S=5 S=6 S=7

Page 10: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Nope,  I  am  not  cheating  you…    *  Central  limit  theorem  (CLT)  :    *  the  sum  of  a  large  collection  of  random  variables  (Sn  =  X1  +  …  +  Xn  )  is  

distributed  as  a  binomial  law  (or  gaussian)  

*  Where  m  is  called  the  average  and  s  the  variance  *  First  discussed  by  Abraham  De  Moivre  (1667-­‐1754)  

*  Physical  experiment  in  a  laboratory:  There  is  a  large  number  of  unknown  parameters  which  are  uncontrolled  and  contribute  to  your  physical  measurement  (measure  velocity  with  a  timer,  …  )  *  CLT  :  by  repeating  the  same  measurement,  you  will  obtain  a  distribution  of  results  (data),  this  distribution  will  a  gaussian  centered  around  the  average  value  (m=your  final  measurement)  and  with  a  given  width  (s=your  error  bars)   10  

Seriously?  

P (S) = e−(S−µ)2/(2σ2)

Page 11: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

m=0,  s=1  

11  

Gaussian  function  

m=1,  s=1  m=0,  s=3  

Average  m

Variance  s

Page 12: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

* Problem  to  be  solved  in  the  practice  session  * Throwing  to  the  casino  a  pair  of  dice  at  the  same  time  * Sum  of  a  pair  of  dice  takes  values  from  2  to  12  * Where  do  you  put  your  bet?  * S=2  ?      * Rolling  many  dices  :  Distribution  is  again  gaussian   12  

Example  1  :    gambling  (Dices)  

Page 13: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

13  

Example  2:  Galton    

http://www.elica.net  (software  to  perform  simple  model  calculations)  

q  Balls  are  bumping  against  many  ticks  during  their  fall  

q  Balls  are  collected  at  the  end  of  the  free  fall  q  Gaussian  distribution  

Page 14: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Distribution  of  people  according  to  their  height  is  a  gaussian  as  well  (top:  college  male  students,  bottom:  men  in  black  and  women  in  white)  

Connecticut  State  College  (J.  Heredity  5:511–518,  1914).   14  

Example  3:  population  height  

Pr  Joiner’s  students,  class  of  1975,  Penn  state    Joiner,  B.  L.  (1975),  “Living  Histograms,”  International  Statistical  Review,  3,  339–340.  

Page 15: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Larry  Gonick,  The  Cartoon  Guide  to  Statistics,  New  York,  NY  :  Collins  Reference  /  HarperPerennial  1993,  p.  83.  15  

Fuzzy  CLT  …  

Page 16: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

16  

“Normal”  is  everywhere…  

Page 17: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

17  

Convergence  to  the  Normal  distribution  by  increasing  the  number  of  coins  to  ~1000  

average  

variance  

Page 18: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Part  2  :  Programming  Arrays  

18  

Page 19: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

19  

Collection  of  variables    1.  Program  something  2.  Integer(4)  ::    x1,x2,x3,x4,x5  3.  Integer(4)  ::    x6,x7  

4.  x1 = 1 5.  x2 = 2 6.  x3 = 3 7.  x4 = 4 8.  x5 = 5 9.  x6 = 6 10.  x7 = 7

11.  end  program  

Ø  We  need  to  define  a  collection  of  variables  

Ø  X1,….,X10  Ø  We  need  to  assign  values  to  these  variables  

Ø  Time  consuming  !  

Page 20: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  An  array  is  a  collection  of  variables,  grouped  under  the  same  name    *  myarray  is  the  array’s  name  for  the  array  in  this  example  

*  Each  box  of  the  array  corresponds  to  a  real(8)  variable,  which  can  take  any  value  

*  To  access  a  particular  variable  in  the  group,  the  position  of  the  variable  in  the  array  needs  to  be  provided,  example  :      

* write(*,*)      myarray(2)  20  

Arrays  

Page 21: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Arrays  

21  

Variables    real(8)          ::        aa  aa  =  16  

Arrays    real(8)        ::        myarray(7)  myarray(2)  =  16  

16  

16  

Page 22: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Arrays  

22  

Variables    real(8)          ::        aa  aa      =    16  

16  

16  

Arrays    real(8)    ::    myarray(7)    myarray(2)  =  16  

Number  of  members  (7)  Accessing  member  2  

Page 23: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Array:  Global  assignment  

23  

real(8) ":: myarray(3)""myarray(2) = 0"

real(8) ":: myarray(3)""myarray = 0"

What do you think the difference is between those two operations ? "

On the right side, "which element is set to zero ? "On the left side, "which element is set to zero ? "

How many members/elements does the array myarray have ?""

"Answer : " " [FILL IN]"

Page 24: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Array:  Global  assignment  

24  

real(8) ":: myarray(3)""myarray(2) = 0"

real(8) ":: myarray(3)""myarray = 0"

0  

0  

0  

0  

Page 25: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Array:  global  operations  

25  

1.  program  testarray  2.  real(8)        ::        myarray(10)  

3.  myarray  =  2.0  

4.  write(*,*)  ‘the  second  element  of  the  array  has  the  value:  ‘,      myarray(2)  

5.  myarray  =  sin  (  myarray  )  

6.  end  program  

*  Is  this  operation  global  and  affects  the  whole  array,  or  is  it  local  and  only  affects  one  element?          [Tick]  

                     global                                    local  

☐ ☐

☐ ☐

☐ ☐  

Page 26: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

26  

Defining  an  array’s  dimension  

real(8)              ::            myarray(  x  :  y  )  

The  first  index  of  the  array  is  x  (if  not  specified  x=1)  

The  last  index  of  the  array  is  y  

by  default      :  x=1  x  can  be  zero    :  x=0  x  can  be  negative    :  x=-­‐5  

Real(8)::  myarray(1:5),  myarray(5)  Real(8)::  myarray(0:7)  Real(8)::  myarray(-­‐5:5)  

Page 27: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  What  we  will  practice  today  :  we  define  an  array  which  contains  the  number  of  outcomes  obtained  by  repeating  the  experiment  of  throwing  7  coins.    

*  Each  box  of  the  array  is  counting  the  number  of  outcomes  obtained  with  a  given  sum  of  coins,  S=0  ,  S=1  ,  S=2  ,  S=3,  S=4  ,  S=5  ,  S=7  

*  Each  time  an  experiment  is  realized,  we  measure  S,  and  we  increment  myarray(S):  

* myarray(S) = myarray(S)+1"  *  myarray  is  the  histogram  of  the  obtained  S  

27  

Arrays  &  Coins  

Page 28: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Outcome  of  experiment:  

 S=3            0                    0                  1                    0                  0                  1                    1  We  keep  track  that  we  obtained  one  more  event  S=3  by  repeating  our  experiment  :    

" " " " "myarray(3) = myarray(3) + 1"      

 S=4              1                  1                    1                  0                    0                  1                    0  We  now  want  to  keep  track  that  we  obtained  S=4:    

" " " " "myarray(4) = myarray(4) + 1"  

28  

Arrays  &  flipping  coins  

Page 29: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

*  Summation  of  all  values  contained  in  the  array  “myarray”  :      

* y=sum(myarray)"

*  Returns  the  minimum  value  contained  in  “myarray”:  

*  y=minval(myarray)  

*  Returns  the  maximum  value  contained  in  “myarray”:  

* y=maxval(myarray)"

*  Returns  the  dimension  of  the  array  (i  in  this  example  should  be  an  integer)  :  

*  i  =size(myarray)  29  

Functions  of  arrays  

Page 30: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

Combined  conditional  statements    

*  The  if  statement  executes:  

*   the  block  1  if  the  condition  “a>0.1”  and  the  condition  “a<0.5”  are  satisfied  

*  The  block  2  if  either  of  “a>0.1”  or  “a<0.5”  is  NOT  satisfied  

30  

1.  Program  something  2.  Implicit  none  3.  Real(8)      ::    a    

4.  a=0.2  

5.  If  (                a  >  0.1    &&    a  <  0.5        )    then  

6.         write(*,*)  ‘  It  is  true’  

7.       else  

8.           write(*,*)  ‘It  is  false’    9.   end  if  

10.  end  program  

BLOCK

 1    

BLOCK

 2    

Page 31: ① Normal distribution Programming"Simple idea: lets define the outcome of an experiment X i, and the result of the experiment is either X i=0 or X i=1.When I repeat the experiment,

u Exercice  1  :  Central  limit  theorem,  convergence  to  a  Normal  distribution  with  random  variables  (Xi=0,1)  

   u Exercice  2  :  Rolling  dices  at  the  casino    

31  

Practice  Problems