Introduction to Monte Carlo concepts & basic techniques -
Common distributions

ECE generic - segment 2

1. Introduction: Some basic stochastics concepts reviewed

1.1 Uniform distribution

Ask a physicist or engineer to make 20 points uniformly on a line, this is what he/she will make:

blueline={Blue,Thickness[0.002],Line[{{0.,0.},{1.,0.}}]};

redpointarray={Red,PointSize[0.02], Point /@ Table[{(m/20.),0.},{m,0,20}]} ;

Graphics[{blueline,redpointarray},PlotRange-> {{-0.05,1.05},{-0.05,0.05}}, ImageSize-> 600 ]

Ask a statistician 20 uniform points on a line, this is what he/she will make:

blueline={Blue,Thickness[0.002],Line[{{0.,0.},{1.,0.}}]};

redpointarray2={Red,PointSize[0.02], Point /@ Table[{RandomReal[{0.,1}],0.},{m,0,20}]} ;

Graphics[{blueline,redpointarray2},PlotRange-> {{-0.05,1.05},{-0.05,0.05}}, ImageSize-> 600 ]

Both have to do with uniformity. One type of uniformity is exact and deterministic. The other is uniformity with randomness: the probability for a point to be anywhere on the line is uniform. But the event is random, also known as stochastic phenomenon, hence the points can end up anywhere.

In the previous lecture, we have done a Monte Carlo simulation of this:

The rain is uniform in the sense that any lemon/gumdrop has a uniform probability to fall into any mouth. But the actual final location - because of so many uncontrollable factors of random air movement, falling-object mechanical motion, Bernouilly air dynamics and air resistance, etc - is not uniform in the physics/engineering sense - which is essential for a stochastic phenomenon (otherwise, it wouldn’t be stochastic). So, we did use RandomReal to generate a two-dimensional random arrays with uniform distribution for the lemon/gumdrops. But did the kids have uniform chance to get any number of lemon/gumdrops? They get Poisson distribution as you have learned.

1.2 Non-uniform distribution

What is it like to have a NON-uniform distribution? Watch this, known as a Galton’s board (click on link)

link

You see that the candies (or color marbles) are NOT spatially uniformly distributed. The center has more than the two sides.

This is how it works: without those path-deflecting pegs, the balls would fall straight down the center. However, as a ball goes through each stage, it hits a peg and has EQUAL chance to go left or right. It is like flipping a coin. If it keeps on flipping head, it goes one side, for example, let’s say left and left until getting to the furthest left bin. Or tails and tails and ends up in the furthest right bin. But that’s rare to flip that many heads or tails in a row. Most have roughly equal chance to flip head or tail and end up somewhere in the middle. Hence, we have a non-uniform distribution.

2. Galton board simulation: Binomial distribution

2.1 Tutorial

It is extremely important for you to learn techniques of the Monte Carlo method: to simulate a random process using computer. Hence, you will start learning to simulate Galton’s board in this exercise. Follow instructions below, then do the work at the end. Later, when you learn about stochastic processes like Brownian motion-Wiener process for stochastic calculus, you will need all these stuffs.
To simulate a ball going left or right, we use:

choice={0,1}; (* only two choices: 0 or 1 *)
RandomInteger[choice] 
RandomInteger[{0,1}] (* this is the same as above *)

But instead of 0 and 1, we can convert it to +1 and -1 to make it goes right or left equally (you will learn later something similar to this: random walk. Perhaps you might have read this book “A Random Walk Down Wall Street”). This is done with this:

2*RandomInteger[{0,1}]-1

To make the ball go through many stage, we do this:

numberofstage=10;
RandomInteger[{0,1},numberofstage]

Convert 0 and 1 into -1 and 1:

numberofstage=10;
2*RandomInteger[{0,1},numberofstage]-1

Which bin does the ball end up in? by adding up all the left (-1) and right (+1). Use function Total to get that.

numberofstage=10;
xball=2*RandomInteger[{0,1},numberofstage]-1
finalbin=Total[xball]

The ball ends up in bin 4 in the left (if you run again, you will get a different number - that’s what’s random simulation is about). This is what you will do: generate 1000 balls like the one above, get their final bins, and make a histogram to find how many balls in each bin. Suggestion: you can use Table or Do loop to generate 1000 balls. This is an example to generate the paths of only 5 balls

numberofstage=10;
numberofballs=5;
allballs=Table[2*RandomInteger[{0,1},numberofstage]-1,numberofballs]

To get the final bin position, we just have to do Total:

Total /@ allballs

This means ball 1 is in left 8 bin, ball 2 in right 6 bin, ball 3 in left 2 bin, ball 4 in left 8 bin, and ball 5 in right 2 bin (if you run again, you will get a different number). When you do for a thousand balls, you don’t want to print out to see for sure, just use ; to terminate each statement. Show only the histogram. Is the distribution of balls spatially uniform?

result:

numberofstage=10;
numberofballs=1000;
allballs=Table[2*RandomInteger[{0,1},numberofstage]-1,numberofballs];
bins=Total /@ allballs ;
Histogram[bins]

It looks like in those experiment demos on Youtube.

2.2 Galton board simulation

Exercise - Galton’s board with variable stages

If you feel you do well with simulation technique, do a simulation of the Galton board with input choice such as the number of stages and balls etc. See the simulation below to get an idea what it is about.

Answer

You don’t have to do the thing below if not feel up to the task. But you should look through as it gives you another perspective how a seemingly uniform random process leads to a non-uniform distribution. But if interested, you can try to see how far you can program demo like the one below: one can vary the number of stages, the number of balls, and simulate each ball falling path. You can vary the ball number and see the histogram fills up similarly to the physical bins in real experiments.

code

Manipulate[
If[TrueQ[GALTONsim],
Manipulate[
allball1=Table[2*RandomInteger[{0,1},nstage]-1,nball];
bins=Total /@ allball1;
hist0=Histogram[bins,ChartStyle->White, ImageSize->300];
Manipulate[
k=Min[k,nball];
ballLRpath=Table[Total @ Take[allball1[[k]],j],{j,1,Length[allball1[[k]]]}];
histk=Histogram[Take[bins,k],ChartStyle->Hue[0.35,1,1,0.6], ImageSize->300];;
path=Join[{{0,0}},Transpose[{ballLRpath,-Range[nstage]}]];
Column[{
Animate[
Graphics[{triangle[nstage], Orange,Thickness[0.01],Line[path]
,Red,PointSize[0.05],Point[path[[j]]]
,Text[Style["ball #: "<>ToString[k],16,Black,FontFamily-> "Arial"],{0,1}]}
,PlotRange-> {{-nstage-1,nstage+1},{-nstage-1,2}}
,Frame-> True,GridLines->Automatic
,AspectRatio->1, ImageSize->300]
,{j,1,nstage+1,1}]
,Show[hist0,histk]}, Alignment-> Center]
,{k,1,nball,1}]

,{{nstage,10},2,20,1},{nball,{100,500,1000,2500,10000}}
]
,"click the Galton's board to start"]
,Button[,If[TrueQ[GALTONsim],GALTONsim=False,GALTONsim=True]]
,Initialization:>{
triangle[ns_]={Hue[0.5,0.4,1],Polygon[{{0,0.},{-ns,-ns},{ns,-ns},{0,0}}]
,Black,PointSize[0.025],Table[Point[{m,n+1}],{n,0,-ns,-1},{m,n+1,-n-1,2}]};
}]

Note: this is also a preview of what we will learn later: imagine the ball as an investor on “a random walk down Wall Street”. Each stage it bounces is an investment decision. You can buy or sell and your porfolio increases (say, move to the right) or loses (move to the left). The ball path is a representation of a stochastic process path. What will be your porfolio as a function of time? This type of calculation is in the field of “stochastic calculus” that is the foundation of financial calculation that we will study later.

2.3 Binomial distribution

One can prove that the distribution of the balls of a Galton board is binomial distribution, expressed as:
                                                  P[n] =(1/(2^N))(({{N}, {n}})) ; for n=0, 1, .., N       (2.1)

Exercise - plot the binomial distribution

Make a Manipulate loop of plot of
                                         P[n] =(1/(2^N))(({{N}, {n}})) ; for n=0, 1, .., N               (2.1)
where one can vary control variable N . (Do not use symbol N in Mathematica since it is a reserved named function). For example, for nstage=8:

nstage=8;
prob=Table[(1./(2^nstage)) Binomial[nstage,m],{m,0,nstage}]

Note that the total is always = 1

Total @ prob

Answer

Manipulate[
(* this is raw probability *)
binom=Table[(1./(2^nstage))Binomial[nstage,m],{m,0,nstage}];

(* each probability must be paired with a number, in this case, m
to make an {x,y} pair for graphing the points: {m, (1./(2^m))(({{N}, {n}}))}  *) 

binompt=Transpose[{Range[nstage+1],binom}]; (*Transpose is used for pairing *)

(* now make blue dots with each {x,y} pair of binompt  *) 

bluepoint=Graphics[{PointSize[0.025],Blue,Point /@ binompt}];

(* make a line plot of raw probability  *)

lineplot=ListLinePlot[binom
, PlotStyle-> {Thickness[0.017],Hue[0.5,1,1]}
,PlotRange-> All];

(* show both plots together  *)

Show[lineplot , bluepoint ] 

,{nstage,3,100,1}]

2.4 An example of binomial distribution: coin flipping

A more common example of binomial distribution is the game of coin flipping. Let’s consider this exercise

Exercise

We have 500 students playing the game of coin flipping. Assume the coin is perfectly fair, which means it has equal chance to flip head or tail. Everyone flips 12 times. Approximately many students can you expect to get:
1-  2 tails and 10 heads? (in any order, of course)
2-  3 tails and 9 heads?
3-  4 heads and 8 tails
4-  9 tails and 3 heads?
5- what is the chance for a student who can flip 12 heads in a row?
6-  which of the question from 1-5 above you can answer with a previous calculation and there is no need for any new calculation?
7- How many students can you expect to get anywhere from 1 to 5 tails? Is is equal to the expected number of students who get from 1 to 5 heads?

Note: If you want to calculate the probability using the formula:
                                                P[n] =(1/(2^N))(({{N}, {n}})) ; for n=0, 1, .., N               (R.2.1)

There are 2 ways, you can use Mathematica function BinomialDistribution, or just this (recommended to use it here):

numberofstage=8;
m=3;
(1/(2^numberofstage)) Binomial[numberofstage,m]

If you want a floating point number instead of a fraction like above, use a decimal point (red dot below) on any number like this:

numberofstage=8;
m=3;
((1.)/(2^numberofstage)) Binomial[numberofstage,m]

Answer

For questions 1-5:

moutcome={2,3,4,12}; nstudent=500;
nstage=12;
(* this is the probability *)
probab=Table[((1.)/(2^nstage)) Binomial[nstage,k],{k,moutcome}]
(* this is the expected number of students with the outcomes *)

expNstudent=500 * probab

We can put the answer in a table:

moutcome={2,3,4,12}; nstudent=500;
nstage=12;
(* this is the probability *)
probab=Table[((1.)/(2^nstage)) Binomial[nstage,k],{k,moutcome}];
(* this is the expected number of students with the outcomes *)

expNstudent=500 * probab ;

answertable=Transpose[{{"2 tails 10 heads","3 tails 9 heads","4 heads 8 tails","12 H/T in a row"}
,probab,expNstudent}];

header={"outcome","probability","expected # of students"};

answerwtheader=Join[{header},answertable];

Grid[answerwtheader , Frame-> All,BaseStyle->{20,FontFamily-> "Calibri"}]

6. which of the question from 1-5 above you can answer with a previous calculation and there is no need for any new calculation?

We don’t have to do question #4 because  “9 tails and 3 heads” has the same chance as “3 tails and 9 heads”. The coin doesn’t care head or tail. Both has equal chance.

7. How many students can you expect to get anywhere from 1 to 5 tails? Is is equal to the expected number of students who get from 1 to 5 heads?

First, we can answer the second part. The expected number of students getting from 1 to 5 tails is the same as those with 1-5 heads for the same reason stated in answer 6 above.
To calculate the probability of 1-5 tails, we just have to add:

nstage=12; nstudent=500;

p1to5=(1./(2^nstage)) Binomial[nstage,1] + (1./(2^nstage)) Binomial[nstage,2]+(1./(2^nstage)) Binomial[nstage,3]+(1./(2^nstage)) Binomial[nstage,4]+(1./(2^nstage)) Binomial[nstage,5]

expNstud15=nstudent*p1to5

Grid[{{"the probab. to get 1-5 H or T is: ",p1to5}
,{"the expected # students to get that is: ", expNstud15}},Frame-> All,BaseStyle->{20,FontFamily-> "Calibri"}]

It is more elegant if we use Total:

nstage=12; nstudent=500;
moutcome15={1,2,3,4,5}; 
p1to5= Total @ Table[(1./(2^nstage)) Binomial[nstage,m],{m,moutcome15}] ;
expNstud15=nstudent*p1to5 ;

Grid[{{"the probab. to get 1-5 H or T is: ",p1to5}
,{"the expected # students to get that is: ", expNstud15}},Frame-> All,BaseStyle->{20,FontFamily-> "Calibri"}]

3. Normal distribution as the limit of binomial distribution

3.1 Asymptotic behavior of binomial distribution with large stage number

In the last exercise, this is what you might have:

Manipulate[
(* this is raw probability *)
binom2=Table[(1./(2^nstage))Binomial[nstage,m],{m,0,nstage}];

(* each probability must be paired with a number, in this case, m
to make an {x,y} pair for graphing the points: {m, (1./(2^m))(({{N}, {n}}))}  *) 

binompt2=Transpose[{Range[nstage+1],binom2}]; (*Transpose is used for pairing *)

(* now make blue dots with each {x,y} pair of binompt  *) 

bluepoint2=Graphics[{PointSize[0.025],Blue,Point /@ binompt2}];

(* make a line plot of raw probability  *)

lineplot2=ListLinePlot[binom2
, PlotStyle-> {Thickness[0.017],Hue[0.5,1,1]}
,PlotRange-> All];

(* show both plots together  *)

Show[lineplot2 , bluepoint2 ] 

,{nstage,3,100,1}]

We will change the code above slightly: instead letting the x-axis to be {0,1,2,3,...nstage}, we will shift it by ((-nstage)/2) to make:   

{((-nstage)/2), ((-nstage)/2)+1,((-nstage)/2)+2,...   ,  (nstage/2)-2  ,(nstage/2)-1 ,(nstage/2) }

It’s no big deal, the x-axis is only for plotting and it is prettier to make it symmetric across zero.

Manipulate[
(* this is raw probability *)
binom3=Table[(1./(2^nstage))Binomial[nstage,m],{m,0,nstage}];

(* each probability must be paired with a number, in this case, m
to make an {x,y} pair for graphing the points: {m, (1./(2^m))(({{N}, {n}}))}  *) 

binompt3=Transpose[{Range[nstage+1]-1-(nstage/2.),binom3}]; (*Transpose is used for pairing *)

(* now make blue dots with each {x,y} pair of binompt  *) 

bluepoint3=Graphics[{PointSize[0.025],Blue,Point /@ binompt3}];

(* make a line plot of raw probability  *)

lineplot3=ListLinePlot[binompt3
, PlotStyle-> {Thickness[0.017],Hue[0.5,1,1]}
,PlotRange-> All];

(* show both plots together  *)

Show[lineplot3 , bluepoint3 ] 

,{nstage,3,100,1}]

What do you think the curve would asymptotically approach to as nstage -> ∞? As nstage gets larger, the curve looks smoother and smoother. Can there be some mathematical function f[x] that approximate the binomial curve?

The answer is yes, and that function is:
                                     P[n] =(1/(2^N))(({{N}, {n}})) ~ f[n;N]=(Sqrt[(2/(N π))]) (e^(-((2 (((n-N/2))^2))/N)))              (R.3.1)   
and                     
                                        (Underscript[Limit, N-> ∞]) (P[n] =(1/(2^N))(({{N}, {n}})) ) =(Underscript[Limit, N-> ∞])  f[n;N]             (R.3.2)   
we highlight the function because it is such an important function that we will encounter over and over throughout the course. Eqs. (R.3.1) and (R.3.2) say that as N gets ∞, the two functions at their limits become equal. This result is a consequence of a well-known theorem: central-limit theorem that explains why Gaussian or Normal distribution are found in so many phenomena.

Exercise - demonstrate the approximation of binomial with normal distribution with graphics

Write a code showing (R.3.1). Plot the right hand side and the left handside of the equation (f[n;N]=(Sqrt[(2/(N π))]) (e^(-((2 (((n-N/2))^2))/N))))  on top of each other, and show that at large nstage, they become practically equal. 

You already have the code to plot P[n] =(1/(2^N))(({{N}, {n}})). However we’ll do one more thing: to make thing prettier with symmetry, we will shift the x-axis by ((-nstage)/2). Hence the right handside function becomes:
                                           f[x;N]=(Sqrt[(2/(N π))])  (e^(-((2(x^2))/N)))   where x=n-(N/2)
You can use the code below for plotting   f[x;N]=(Sqrt[(2/(N π))])  (e^(-((2(x^2))/N))). You must combine both plots to show they are on top of each other.

Manipulate[
Plot[(Sqrt[(2/(nstage π))])  (E^(-((2(x^2))/nstage))),{x,-2.(Sqrt[nstage]),2.(Sqrt[nstage])}, PlotStyle-> Red]
,{nstage,5,100,1}]

You can take a sneak peek at the answer but should write your own code.

Answer

Manipulate[

(* binom dist prob values  *)

binom4=Table[{m-0.5nstage,(1./(2^nstage))Binomial[nstage,m]},{m,0,nstage}];

(* deviation is the difference between the 2 functions  *)
deviation=Table[(1./(2^nstage))Binomial[nstage,m]-(Sqrt[(2/(nstage π))])  (E^(-((2(((m-0.5nstage))^2))/nstage))),{m,0,nstage}];

(* plot of the binomial dist. as a joined line *)

binomplot4=ListLinePlot[binom4
, PlotStyle-> {Thickness[0.017],Hue[0.5,1,1]}
,PlotRange-> All];

(* plot of the binomial dist. as discrete blue points  *)

binomBluept4=Graphics[{PointSize[0.025],Blue,Point /@ binom4}];

(* plot of the normal distribution that approximate the binom.  *)

normdistplot4=Plot[(Sqrt[(2/(nstage π))])(E^(-2. (x^2)/nstage)),{x,-2.(Sqrt[nstage]),2.(Sqrt[nstage])}, PlotStyle->{Thickness[0.005], Red}];

(* show all together  *)

Grid[{{Show[binomplot4, binomBluept4, normdistplot4,ImageSize-> 400]

,Column[{ListPlot[ deviation,Joined-> True,PlotRange-> All,ImageSize-> 300]
,ListLogPlot[ Abs @ deviation,Joined-> True,PlotRange-> All,ImageSize-> 300]}]
}}]
,{nstage,5,100,1}]

Exercise - comparing Poisson with normal distribution with graphics

Do you recall this from Lesson 3:
                                      (Subscript[P, Poisson])[m; (Subscript[n, μ])] =(((Subscript[n, μ])^m)/(m!))(e^(-(Subscript[n, μ])))                       (R.3.3)
where (Subscript[n, μ]) is the mean or expected (or average) number of lemon/gumdrops each kid can get, and m is the actual number

Compare this distribution by plotting it directly above this given form of normal distribution, similarly what we did above for binomial dist. vs. normal dist.
                                                              f[x;(Subscript[n, μ])]=(1/(Sqrt[(Subscript[n, μ]) 2π]))  (e^(-((((x-(Subscript[n, μ])))^2)/(2 (Subscript[n, μ])))))    (R.3.4)
You can use this code to plot the normal distribution:

Manipulate[
Plot[(1./( (Sqrt[2 π nmean])))  (E^(-((((x-nmean))^2)/(2. nmean)))),{x,nmean-4. (Sqrt[nmean]),nmean+4. (Sqrt[nmean])}
, PlotStyle->{Thickness[0.005], Red}]
,{nmean,5,100,1}]

Answer

Manipulate[

(* poisson dist prob values  *)

poisson=Table[{m,((nmean^m)/(m!))(E^(-nmean))},{m,0,2.5 nmean}];

(* deviation is the difference between the 2 functions  *)
deviation2=Table[((nmean^m)/(m!))(E^(-nmean))-(1./( (Sqrt[2 π nmean])))  (E^(-((((m-nmean))^2)/(2 nmean)))),{m,0,2.5nmean}];

(* plot of the binomial dist. as a joined line *)

poissonplot=ListLinePlot[poisson
, PlotStyle-> {Thickness[0.017],Hue[0.5,1,1]}
,PlotRange-> All];

(* plot of the binomial dist. as discrete blue points  *)

poissonBluept=Graphics[{PointSize[0.025],Blue,Point /@ poisson}];

(* plot of the normal distribution that approximate the binom.  *)

normdistplot5=Plot[(1./( (Sqrt[2 π nmean])))  (E^(-((((x-nmean))^2)/(2 nmean)))),{x,nmean-4. (Sqrt[nmean]),nmean+4. (Sqrt[nmean])}
, PlotStyle->{Thickness[0.005], Red}];

(* show all together  *)

Grid[{{Show[poissonplot,poissonBluept,normdistplot5,ImageSize-> 400]

,Column[{ListPlot[ deviation2,Joined-> True,PlotRange-> All,ImageSize-> 300]
,ListLogPlot[ Abs @ deviation2,Joined-> True,PlotRange-> All,ImageSize-> 300]}]
}}]
,{nmean,5,100,1}]

3.2 An interpretation of normal distribution

What is the meaning of what we did above? When we measure or make an observation, the quantity we measure may fluctuate randomly because of uncontrolled effects. These random effects can make the value a bit higher or lower, like a ball on a Galton’s board may bounce to the left or right or left ... etc. Eventually, after so many bounces the ball is not always exactly and deterministically at the center as it should be but has a spread of possible values, which means a distribution.

In the large number of bounces limit, the distribution becomes what we call normal distribution:
                                 f[x;N]=(Sqrt[(2/(N π))])  (e^(-((2(x^2))/N)))     =>        (Subscript[f, pdf])[x; σ]=(1/(σ (Sqrt[2 π])))  (e^(-((x^2)/(2 (σ^2)))))

More generally the function f[x] doesnot have to center on zero. It can center on any value, which we will call μ:
                                                      (Subscript[f, pdf])[x;μ, σ]=(1/(σ (Sqrt[2 π])))(e^(-((((x-μ))^2)/(2 (σ^2)))))                             (R.3.5)

μ is called the mean, and σ is called standard of deviation. This is among the most important distribution that we learn.

This distribution is ubiquitous. It shows up everywhere especially where additive random errors are involved. Additive means the errors add to each other. This is a consequence of what is known as central-limit theorem that you can learn later.

It has a cousin that we might have encountered or will encounter soon: log normal, which is the log form of errors that multiply together instead of adding:

(Subscript[g, pdf])[x;μ, σ]=(1/(x σ (Sqrt[2 π])))(e^(-(((Log[x/μ])^2)/(2 (σ^2)))))                             (R.3.6)

Exercise - Plot normal distribution

Make normal distribution plots on linear scale and log scale with variable input μ and σ. What shape do you observe on log scale. (hint: Log[(e^(-((((x-μ))^2)/(2 (σ^2)))))]=-((((x-μ))^2)/(2 (σ^2))) ; check on inverted parabola)

Answer

Manipulate[

linearNDplot=Plot[(1./(σ (Sqrt[2 π])))(E^(-((((x-μ))^2)/(2 (σ^2))))) ,{x,μ-4 σ,μ+4 σ}, PlotRange-> {{-5,5},{0.,1}}
,Frame-> True, GridLines->Automatic
,ImageSize-> 350];
(* we don't use LogPlot in this case because it is extremely slow while we
want fast demo with varying μ and σ *)
logNDplot=Plot[-((((x-μ))^2)/(2. (σ^2)))+ Log[(1./(σ (Sqrt[2 π])))],{x,μ-4. σ,μ+4. σ}
, PlotRange-> {{-5,5},Log /@ {0.0001,1.}}
,Frame-> True, GridLines->Automatic
,ImageSize-> 350];

Grid[{{linearNDplot},{logNDplot}}]

,{{μ,0.},-3.,3.},{{σ,0.5},0.3,2.}]

3.3 Calculation of probability

When we have a continuous probability distribution, like this:
                                                   (Subscript[f, pdf])[x;μ, σ]=(1/(σ (Sqrt[2 π])))(e^(-((((x-μ))^2)/(2 (σ^2)))))                             (R.3.5)

It is called pdf: probability density function: it is the probability per unit value of the variable. It is not the probability per se. To compare, consider discrete distribution:
                                                  (Subscript[P, Binom])[n] =(1/(2^N))(({{N}, {n}}))                              (R.2.1)
or: 
                                                  (Subscript[P, Pois])[m] =(((Subscript[n, μ])^m)/(m!))(e^(-(Subscript[n, μ])))                               (R.3.3)
Both (Subscript[P, Binom])[n] and (Subscript[P, Pois])[m] are probability for event n or m.

In an exercise above, we are asked how many students get any where from 1-5 tails when flipping coin. We just have to add:
                              (1/(2^N))(({{N}, {1}}))+(1/(2^N))(({{N}, {2}})) +(1/(2^N))(({{N}, {3}}))+(1/(2^N))(({{N}, {4}})) +(1/(2^N))(({{N}, {5}}))

What about continuous distribution like     (Subscript[f, pdf])[x;μ, σ]=(1/(σ (Sqrt[2 π])))(e^(-((((x-μ))^2)/(2 (σ^2)))))  ?

We can’t add, we have to integrate:  

                            probability for: a≤x≤ b=    (∫_a^b)(Subscript[f, pdf])[x;μ, σ]dx                      (R .3.6)

For discrete distributions, the sum of all probability values must be 1:
                                             (∑_(n=0)^N)(Subscript[P, Binom])[n] =(∑_(n=0)^N)(1/(2^N))(({{N}, {n}}))=((((1+1))^N)/(2^N)) = 1         (R.3.7)
and: 
                  (∑_(n=0)^N)(Subscript[P, Pois])[m] =(∑_(n=0)^N)(((Subscript[n, μ])^m)/(m!))(e^(-(Subscript[n, μ])))=(e^(-(Subscript[n, μ])))(∑_(n=0)^N)(((Subscript[n, μ])^m)/(m!)) =(e^(-(Subscript[n, μ]))) (e^(Subscript[n, μ])) =1      (R.3.8)

For continuous variable, it is the integration over the entire range:
                                 (∫_(-∞)^∞)(Subscript[f, pdf])[x;μ, σ]dx=(∫_(-∞)^∞)(1/(σ (Sqrt[2 π])))(e^(-((((x-μ))^2)/(2 (σ^2)))))dx=1                 (R.3.9)

Exercise - an illustration of integrating a continuous variable pdf

This exercise is adapted from the supplementary topical lecture on normal distribution.

Here is the app (or you can cut, copy and paste the app onto a new notebook to run outside this notebook while close all active Manipulate cells to make it runs faster).

Run the app to answer the following:

1- What is the probability for a variable (in this case, it is men’s height) to be in 1 standard of deviation (SD) immediately to left or right . Note that if we use unit SD, this calculation is independent of the actual variable: all normal distributions are identical and have the same properties if the mean is shifted to a common fixed value, such as zero, and the unit of length is in SD. In other words, let SD=1. 

2- Same as above but this range is 2 SD instead of 1 SD

3- What range, in SD unit, do you get 95% of population or probability (answer in anyway that makes sense to you and there are more than 1 relevant answers)

4- A business website has a well established record that the number of real hits from potential customers/users (not from bots) on a given day is a Poisson distribution with mean  ~520. (look up above how a Poisson of mean n can be appoximated as Gaussian with the same mean n, and SD ~ (Sqrt[n]).  Without doing much calculation, just use your result in 3, estimate the range of number of customers/users that you have 95% confidence: which means that there is 95% probability that the number of actual customers/users is inside that range.

5- Without doing any new calculation but using only the app, estimate the range with your confidence level =75%. 

6- One day, the website recorded 590 hits. Usually, there is a well-known ratio of hits vs. orders, but this time, the orders show no statistically significant increase, and you suspect the increase hits are due to bots. What is the probability that 590-and-higher hits are from real potential customers/users? Plot the chance of bot hits as a function of hits above certain level, let that level ranges from 550 to 600. 

7- It turns out that it didn’t happen on just one day. Many subsequent days also had high hits. After several days, you find the mean of all hits is now 610. As expected, it is a Poisson distribution also. While you don’t have a way yet to classify real users vs. bots, can you calculate the level of hits at which the chance you have bot hits is equal to the chance you have real customer hits? In other words, what number of hits above which, you think it is higher chance due to bots and below which, it is likely from real customers? (plot the distributions and you will understand the question).

4. Example of applications on financial data

First, you have seen this before in Lesson 2. It is the NASDAQ market index, the symbol is ^IXIC.

The left chart is from 2009 to May 2018. The right chart is a selected period from 8/25/2016 to 12/22/2017 that we choose to study (you can use the date cursor to slide around and zoom in any episode within the date range on the left). Notice the linear behavior on a log scale. It means that the market index grew exponentially (over that episode) vs. time like a compound yield:
                                                                p[t]=p[(Subscript[t, 0])] (e^(γ (t-(Subscript[t, 0]))))     (R.4.1)
where γ is the yield rate or growth rate.
On log scale, the growth function is:          Log[((p[t])/(p[(Subscript[t, 0])]))]= γ (t-(Subscript[t, 0]))       (R.4.2)
which is a linear function.

You will do exercises involving this data. The linear regression fit using Eq. (R.3.2) has been done and the fit graph is given to you as: (it is in the NASDAQ data cell)

growthfitplot

To do this exercise, you need the data in cell below

NASDAQ data (you can open this to take a look, but not necessary, just  shift+enter to get data read in)

ixic={{3681072000,5212.200195`},{3681158400,5218.919922`},{3681417600,5232.330078`},{3681504000,5222.990234`},{3681590400,5213.220215`},{3681676800,5227.209961`},{3681763200,5249.899902`},{3682108800,5275.910156`},{3682195200,5283.930176`},{3682281600,5259.47998`},{3682368000,5125.910156`},{3682627200,5211.890137`},{3682713600,5155.25`},{3682800000,5173.77002`},{3682886400,5249.689941`},{3682972800,5244.569824`},{3683232000,5235.029785`},{3683318400,5241.350098`},{3683404800,5295.180176`},{3683491200,5339.52002`},{3683577600,5305.75`},{3683836800,5257.490234`},{3683923200,5305.709961`},{3684009600,5318.549805`},{3684096000,5269.149902`},{3684182400,5312.`},{3684441600,5300.870117`},{3684528000,5289.660156`},{3684614400,5316.02002`},{3684700800,5306.850098`},{3684787200,5292.399902`},{3685046400,5328.669922`},{3685132800,5246.790039`},{3685219200,5239.02002`},{3685305600,5213.330078`},{3685392000,5214.160156`},{3685651200,5199.819824`},{3685737600,5243.839844`},{3685824000,5246.410156`},{3685910400,5241.830078`},{3685996800,5257.399902`},{3686256000,5309.830078`},{3686342400,5283.399902`},{3686428800,5250.27002`},{3686515200,5215.970215`},{3686601600,5190.100098`},{3686860800,5189.140137`},{3686947200,5153.580078`},{3687033600,5105.569824`},{3687120000,5058.410156`},{3687206400,5046.370117`},{3687465600,5166.169922`},{3687552000,5193.490234`},{3687638400,5251.069824`},{3687724800,5208.799805`},{3687811200,5237.109863`},{3688070400,5218.399902`},{3688156800,5275.620117`},{3688243200,5294.580078`},{3688329600,5333.970215`},{3688416000,5321.509766`},{3688675200,5368.859863`},{3688761600,5386.350098`},{3688848000,5380.680176`},{3689020800,5398.919922`},{3689280000,5368.810059`},{3689366400,5379.919922`},{3689452800,5323.680176`},{3689539200,5251.109863`},{3689625600,5255.649902`},{3689884800,5308.890137`},{3689971200,5333.`},{3690057600,5393.759766`},{3690144000,5417.359863`},{3690230400,5444.5`},{3690489600,5412.540039`},{3690576000,5463.830078`},{3690662400,5436.669922`},{3690748800,5456.850098`},{3690835200,5437.160156`},{3691094400,5457.439941`},{3691180800,5483.939941`},{3691267200,5471.430176`},{3691353600,5447.419922`},{3691440000,5462.689941`},{3691785600,5487.439941`},{3691872000,5438.560059`},{3691958400,5432.089844`},{3692044800,5383.120117`},{3692390400,5429.080078`},{3692476800,5477.`},{3692563200,5487.939941`},{3692649600,5521.060059`},{3692908800,5531.819824`},{3692995200,5551.819824`},{3693081600,5563.649902`},{3693168000,5547.490234`},{3693254400,5574.120117`},{3693600000,5538.72998`},{3693686400,5555.649902`},{3693772800,5540.080078`},{3693859200,5555.330078`},{3694118400,5552.939941`},{3694204800,5600.959961`},{3694291200,5656.339844`},{3694377600,5655.180176`},{3694464000,5660.779785`},{3694723200,5613.709961`},{3694809600,5614.790039`},{3694896000,5642.649902`},{3694982400,5636.200195`},{3695068800,5666.77002`},{3695328000,5663.549805`},{3695414400,5674.220215`},{3695500800,5682.450195`},{3695587200,5715.180176`},{3695673600,5734.129883`},{3695932800,5763.959961`},{3696019200,5782.569824`},{3696105600,5819.439941`},{3696192000,5814.899902`},{3696278400,5838.580078`},{3696624000,5865.950195`},{3696710400,5860.629883`},{3696796800,5835.509766`},{3696883200,5845.310059`},{3697142400,5861.899902`},{3697228800,5825.439941`},{3697315200,5904.029785`},{3697401600,5861.220215`},{3697488000,5870.75`},{3697747200,5849.180176`},{3697833600,5833.930176`},{3697920000,5837.549805`},{3698006400,5838.810059`},{3698092800,5861.72998`},{3698352000,5875.779785`},{3698438400,5856.819824`},{3698524800,5900.049805`},{3698611200,5900.759766`},{3698697600,5901.`},{3698956800,5901.529785`},{3699043200,5793.830078`},{3699129600,5821.640137`},{3699216000,5817.689941`},{3699302400,5828.740234`},{3699561600,5840.370117`},{3699648000,5875.140137`},{3699734400,5897.549805`},{3699820800,5914.339844`},{3699907200,5911.740234`},{3700166400,5894.680176`},{3700252800,5898.609863`},{3700339200,5864.47998`},{3700425600,5878.950195`},{3700512000,5877.810059`},{3700771200,5880.930176`},{3700857600,5866.77002`},{3700944000,5836.160156`},{3701030400,5805.149902`},{3701376000,5856.790039`},{3701462400,5849.470215`},{3701548800,5863.029785`},{3701635200,5916.779785`},{3701721600,5910.52002`},{3701980800,5983.819824`},{3702067200,6025.490234`},{3702153600,6025.22998`},{3702240000,6048.939941`},{3702326400,6047.609863`},{3702585600,6091.600098`},{3702672000,6095.370117`},{3702758400,6072.549805`},{3702844800,6075.339844`},{3702931200,6100.759766`},{3703190400,6102.660156`},{3703276800,6120.589844`},{3703363200,6129.140137`},{3703449600,6115.9599609375`},{3703536000,6121.22998046875`},{3703795200,6149.669921875`},{3703881600,6169.8701171875`},{3703968000,6011.240234375`},{3704054400,6055.1298828125`},{3704140800,6083.7001953125`},{3704400000,6133.6201171875`},{3704486400,6138.7099609375`},{3704572800,6163.02001953125`},{3704659200,6205.259765625`},{3704745600,6210.18994140625`},{3705091200,6203.18994140625`},{3705177600,6198.52001953125`},{3705264000,6246.830078125`},{3705350400,6305.7998046875`},{3705609600,6295.68017578125`},{3705696000,6275.06005859375`},{3705782400,6297.3798828125`},{3705868800,6321.759765625`},{3705955200,6207.919921875`},{3706214400,6175.4599609375`},{3706300800,6220.3701171875`},{3706387200,6194.89013671875`},{3706473600,6165.5`},{3706560000,6151.759765625`},{3706819200,6239.009765625`},{3706905600,6188.02978515625`},{3706992000,6233.9501953125`},{3707078400,6236.68994140625`},{3707164800,6265.25`},{3707424000,6247.14990234375`},{3707510400,6146.6201171875`},{3707596800,6234.41015625`},{3707683200,6144.35009765625`},{3708028800,6110.06005859375`},{3708201600,6150.85986328125`},{3708288000,6089.4599609375`},{3708374400,6153.080078125`},{3708633600,6176.39013671875`},{3708720000,6193.2998046875`},{3708806400,6261.169921875`},{3708892800,6274.43994140625`},{3708979200,6312.47021484375`},{3709238400,6314.43017578125`},{3709324800,6344.31005859375`},{3709411200,6385.0400390625`},{3709497600,6390},{3709584000,6387.75`},{3709843200,6410.81005859375`},{3709929600,6412.169921875`},{3710016000,6422.75`},{3710102400,6382.18994140625`},{3710188800,6374.68017578125`},{3710448000,6348.1201171875`},{3710534400,6362.93994140625`},{3710620800,6362.64990234375`},{3710707200,6340.33984375`},{3710793600,6351.56005859375`},{3711052800,6383.77001953125`},{3711139200,6370.4599609375`},{3711225600,6352.330078125`},{3711312000,6216.8701171875`},{3711398400,6256.56005859375`},{3711657600,6340.22998046875`},{3711744000,6333.009765625`},{3711830400,6345.10986328125`},{3711916800,6221.91015625`},{3712003200,6216.52978515625`},{3712262400,6213.1298828125`},{3712348800,6297.47998046875`},{3712435200,6278.41015625`},{3712521600,6271.330078125`},{3712608000,6265.64013671875`},{3712867200,6283.02001953125`},{3712953600,6301.89013671875`},{3713040000,6368.31005859375`},{3713126400,6428.66015625`},{3713212800,6435.330078125`},{3713558400,6375.56982421875`},{3713644800,6393.31005859375`},{3713731200,6397.8701171875`},{3713817600,6360.18994140625`},{3714076800,6432.259765625`},{3714163200,6454.27978515625`},{3714249600,6460.18994140625`},{3714336000,6429.080078125`},{3714422400,6448.47021484375`},{3714681600,6454.64013671875`},{3714768000,6461.31982421875`},{3714854400,6456.0400390625`},{3714940800,6422.693`},{3715027200,6426.922`},{3715286400,6370.5928`},{3715372800,6380.1636`},{3715459200,6453.2631`},{3715545600,6453.4507`},{3715632000,6495.9594`},{3715891200,6516.718`},{3715977600,6531.7143`},{3716064000,6534.6274`},{3716150400,6585.3561`},{3716236800,6590.1804`},{3716496000,6579.7309`},{3716582400,6587.2515`},{3716668800,6603.5484`},{3716755200,6591.5103`},{3716841600,6605.8013`},{3717100800,6624.0046`},{3717187200,6623.6565`},{3717273600,6624.22`},{3717360000,6605.0667`},{3717446400,6629.0531`},{3717705600,6586.8261`},{3717792000,6598.4297`},{3717878400,6563.891`},{3717964800,6556.774`},{3718051200,6701.263`},{3718310400,6698.9632`},{3718396800,6727.6692`},{3718483200,6716.5332`},{3718569600,6714.9429`},{3718656000,6764.4353`},{3718915200,6786.4355`},{3719001600,6767.7828`},{3719088000,6789.1183`},{3719174400,6750.0533`},{3719260800,6750.9389`},{3719520000,6757.5953`},{3719606400,6737.872`},{3719692800,6706.2082`},{3719779200,6793.2908`},{3719865600,6782.7906`},{3720124800,6790.7137`},{3720211200,6862.4768`},{3720297600,6867.3611`},{3720470400,6889.1601`},{3720729600,6878.5207`},{3720816000,6912.3579`},{3720902400,6824.389`},{3720988800,6873.9729`},{3721075200,6847.5863`},{3721334400,6775.3659`},{3721420800,6762.213`},{3721507200,6776.3753`},{3721593600,6812.8411`},{3721680000,6840.0808`},{3721939200,6875.0776`},{3722025600,6862.317`},{3722112000,6875.7988`},{3722198400,6856.5256`},{3722284800,6936.5828`},{3722544000,6994.7595`},{3722630400,6963.8512`},{3722716800,6960.9593`},{3722803200,6965.361`},{3722889600,6959.9603`}};

residRel={0.01937239055601475`,0.020035322479488304`,0.020725149524991382`,0.018313060559446592`,0.015815261093617472`,0.017869712098297574`,0.021575586618710008`,0.02401590980835877`,0.024909408105446895`,0.01964592636511986`,-0.006703599851397457`,0.008054475229934688`,-0.0034979601994454823`,-0.000537407634329079`,0.013404506617324286`,0.011803244559474402`,0.008106151935926065`,0.008687267333017668`,0.01827968780060163`,0.025992978646479925`,0.01902288359925325`,0.00800911209077837`,0.016513464654876797`,0.018305077895506283`,0.008347977226818293`,0.01582188063410167`,0.011848044130992441`,0.009105596969735064`,0.013451034575002407`,0.011099116954721655`,0.007747002123294322`,0.012700448468369652`,-0.003410212653831568`,-0.005517687505310009`,-0.011058795383401043`,-0.011525053979396682`,-0.01615551422606404`,-0.0083509334995151`,-0.008486363359744509`,-0.009985205527833163`,-0.007644773230597146`,0.00040206737712411567`,-0.005213423808281881`,-0.012129195613501786`,-0.019309057125423412`,-0.02490665586570806`,-0.026968037420468827`,-0.0344698781973794`,-0.04445491412960595`,-0.05436021291498827`,-0.057368720446457855`,-0.035782734493481794`,-0.03113382588656144`,-0.02073342544270873`,-0.029441259926530627`,-0.024646400264944135`,-0.03010177528502217`,-0.019821835970855017`,-0.01685986376630766`,-0.010073160681339566`,-0.013037416885616604`,-0.006055305047099613`,-0.003428349392172181`,-0.0051070183497752275`,-0.002973828465184458`,-0.010442858553236622`,-0.009001130483778597`,-0.020135261701506535`,-0.03448609971004746`,-0.034247354835788835`,-0.026044629362300675`,-0.022138965547286726`,-0.011435678847320496`,-0.007695246825821678`,-0.0033233767927751123`,-0.011087215220738467`,-0.0022811508491693`,-0.0078899155767016`,-0.004810392303945171`,-0.009050684186811875`,-0.0072041785077250065`,-0.0029856412146069267`,-0.005894878925812835`,-0.010918299689946537`,-0.008744523983135355`,-0.006725894533858329`,-0.016298865533266454`,-0.01811473458613211`,-0.027795979146811478`,-0.02179630032786023`,-0.01363396753052537`,-0.01226399480741086`,-0.006872528093784858`,-0.006801970554190406`,-0.0038185119657696276`,-0.002315400254818134`,-0.005849603091012651`,-0.0016862082819412705`,-0.010557330208723098`,-0.008132617035726736`,-0.011564540319759786`,-0.009441122322495138`,-0.011747861455983888`,-0.0037628315482169383`,0.005450708012923244`,0.004620197929355252`,0.004984413290464285`,-0.005241831982148948`,-0.0056749185960817405`,-0.0013507854053097512`,-0.0031199353334768753`,0.001663776403175804`,-0.0007810524212134295`,0.0004757566609912278`,0.0012996537174360945`,0.006417497104817427`,0.009102223934580711`,0.012414532642947146`,0.015012522882122181`,0.02074289167806595`,0.019336968555136724`,0.02277555829521649`,0.02495053583760573`,0.023417674007175843`,0.018496744903211876`,0.019549291735618507`,0.020507013310688604`,0.013642301717904537`,0.026417442792782825`,0.01851465220706408`,0.019513768326965997`,0.013956480472590321`,0.010720404673665557`,0.010715188481239313`,0.01030558453147723`,0.01359787578007332`,0.014115473482664703`,0.01025798888290208`,0.016986549012635688`,0.01648140498683273`,0.0158966483988649`,0.014110018789025458`,-0.00493348056879972`,-0.0007704878711718988`,-0.00207472294132241`,-0.0008025630307345322`,-0.0006856900822634626`,0.004624583595614712`,0.007806179967713434`,0.010023618557377795`,0.008958510224446409`,0.004192140963935109`,0.0042331004469002564`,-0.002195261193016329`,-0.0003563348441393832`,-0.001175757100834729`,-0.0025214725275981287`,-0.005557653001582813`,-0.011414278358509528`,-0.01736738182745734`,-0.0110130135391025`,-0.012889064669166284`,-0.011199130284280823`,-0.0026987508987544317`,-0.004382747309685103`,0.006066161457674024`,0.012380405426440255`,0.011711744179718875`,0.015013666760398142`,0.014168288307277122`,0.0195395429419217`,0.0195327714970647`,0.015156401161124294`,0.0149902784889214`,0.0185401965107391`,0.01697524411982254`,0.01928348032802593`,0.020054009386229338`,0.017275813858206396`,0.0175116578102546`,0.02027060898568711`,0.02292451870056418`,-0.003747647389588382`,0.0029016226972259085`,0.006983422893601016`,0.013279055932570005`,0.013483070700368416`,0.016809906871194907`,0.023014800111304648`,0.023183532122562425`,0.019553844108735854`,0.01817526657766111`,0.025313388990327507`,0.03408358708168123`,0.03060108065356691`,0.026694956749449675`,0.029620087816619067`,0.03285857772788903`,0.014061379870981397`,0.00694245916595547`,0.013563033032850266`,0.008832936275446984`,0.0034519237222098847`,0.0005954008900963675`,0.012802293201362017`,0.003972092748324246`,0.010740070642628191`,0.010553993918774554`,0.014497434026147574`,0.009727881929983029`,-0.0071205740242649`,0.006435574138120237`,-0.008740890355221811`,-0.016839136499979646`,-0.011434788266710655`,-0.02209274219837601`,-0.012324829608455282`,-0.010420035665082802`,-0.008311453305914998`,0.0019621036437041106`,0.003453808040151732`,0.008871187377156378`,0.007305225080424652`,0.011400596096418122`,0.017174530774613783`,0.01732557075484402`,0.016347927928896208`,0.01807506693435347`,0.017661696647568018`,0.0186848683997161`,0.011724314823390466`,0.009921478497165265`,0.003869877322421139`,0.0055762101246097096`,0.004905158386608349`,0.0007671184817361759`,0.001909741459663869`,0.005091710768479696`,0.002379081981488812`,-0.001096373472888254`,-0.023276945140073377`,-0.01753847412675487`,-0.006130358913173262`,-0.007895269814902475`,-0.006611921559230197`,-0.026844846975908263`,-0.02833543509517797`,-0.030758902361263468`,-0.01789959610091793`,-0.021557825922348506`,-0.0233116168430485`,-0.024844791027237384`,-0.023951195520570706`,-0.02157781327993541`,-0.011718754062408215`,-0.00291221696554321`,-0.002500693887795294`,-0.01433222914583393`,-0.012179028220299107`,-0.01209149588640808`,-0.018623862910910033`,-0.009232591741147433`,-0.0064405340430137015`,-0.006150725479587477`,-0.011603452831165359`,-0.009217455139857486`,-0.010137513050192129`,-0.009728650317065393`,-0.011171589756129585`,-0.016975690531912266`,-0.01694292881379933`,-0.02762253678168558`,-0.02674679174282696`,-0.015980089691019495`,-0.016576487692910827`,-0.010636582995147387`,-0.009322465295903193`,-0.007649372776065061`,-0.007828947169951334`,-0.0007213335151374858`,-0.0006144898906124824`,-0.004077769427265739`,-0.003560895219532567`,-0.0017154126376327378`,-0.00416551886947681`,-0.002625242046428715`,-0.0017497827501743862`,-0.002427803566721204`,-0.0029682014728542247`,-0.00648926237474632`,-0.0034897933668788725`,-0.011756562458759189`,-0.010621942945185217`,-0.016495540208591924`,-0.01820586200838157`,0.0029659722366279576`,0.0007463198651429179`,0.004396837371978712`,0.0021147439833939033`,0.0012524738686564701`,0.007970463513258513`,0.009341115253685951`,0.005963336152351721`,0.008485418832724315`,0.002089271424201313`,0.0015949936387169537`,0.0007040996176812322`,-0.0028443223747093085`,-0.008180244201527256`,0.004096071335604537`,0.0019237353168315915`,0.00121476719774034`,0.011101678688398516`,0.011187697488949504`,0.013106024408387993`,0.009684057980057415`,0.01396578522009135`,0.000532320655297628`,0.007146275285876398`,0.0026747963294682757`,-0.009804460629550548`,-0.012373098114772243`,-0.010906427129612813`,-0.006165009287362026`,-0.0028001614615306564`,0.0004268204389442122`,-0.0020564386529640342`,-0.0007192207159373906`,-0.004151673619551341`,0.006831278425776239`,0.013306838496776992`,0.008252799175166855`,0.007211971692630688`,0.0072186447106997636`,0.00581751040401457`};

growthfitplot= ;

Exercise - generate a time-series plot

Time-series is the most frequently used plot in finance, economics. Hence, you should start learning how to make it. The function to use is: DateListPlot or DateListLogPlot

Given data ixic, make a time-series plot and name it nasdaqplot

nasdaqplot=DateListLogPlot[ixic,DateTicksFormat->{"MonthNameShort","/","Year"}]

The above plot is not informative nor stylish. You should try to make one similar the one below, feel free to choose your own color and be creative, but it should show the axes with proper labels

Next, put your plot with the fit plot together like this:

Show[nasdaqplot,growthfitplot]

your answer

Exercise - plot the residual of the fit and its histograms

The deviation between the actual NASDAQ index value and its regression fit is given as . Plot the residuals like the one below. You can choose your own color.

Make 3 histograms: “LogPDF”, “PDF”, and “CDF”. (Go to the app in section R.3.3.3, select lesson 2 to read about CDF). The basic histogram is:

Histogram[residRel,Automatic,"LogPDF"]

But you can style your own style and put on proper labels.

your answer

Exercise - fit the histograms with normal distribution

Do this by obtaining the SD of the residual. You will that the mean is ~0 (why?). Then just plot the curves on top each histograms.

your answer

Exercise - buy high - sell low or buy the dip

You have heard “buy the dip” or “sell at the peak” (record, or before the correction) in market strategy. The basic idea is this:

If we truly believe the market is on a steady growth course, one can set a predicted growth rate based on partial history. Then, when the market is below the red growth line, the probablity is that the market would recover eventually, so-called “revert back to the mean” and one can buy. Likewise, when it overshoots, the market would correct and one can sell at high price. Does it really work? it works for those who make money and beat the market, and the same strategy doesn’t work for those who lose. 

Why? timing the market is a very difficult task, and if everyone rushes in buying at the dip at the same time, the market responds and it wouldn’t be a dip. If everyone is selling at the peak, the market would dip. In other words, it is not a betting game whose outcome is independent of the betting action, unlike the game betting a man or woman based on one’s height like above, but it is a function of it. This is the theory behind market efficiency. There is a whole field of computation about this, known as stochastic calculus that you will learn later. 

For the time being, do a research and write what various schools of investment think and your own thought as well.

5. Normal distribution from central limit theorem

5.1 Discussion

Suppose you have a large number of independent and identically distributed (iid for short) random variables:
                                         array of random variables:       (Subscript[x, 1]), (Subscript[x, 2]),.. (Subscript[x, N]). 
and their distribution can be anything, not necessarily normal, as long as they all have a mean (Subscript[μ, 0]) and standard of deviation σ. Then, you take the mean of all of them:
                                                                        (OverHat[μ])=(((Subscript[x, 1])+(Subscript[x, 2])+...+(Subscript[x, N]).)/N) 
 what is the distribution of the mean? The answer from the central limit theorem is: 
                                                (Subscript[f, pdf])[(OverHat[μ])]~(1/((Subscript[σ, N]) (Sqrt[2 π])))  (e^(-(((((OverHat[μ])-(Subscript[μ, 0])))^2)/(2 ((Subscript[σ, N])^2)))))    ;   (Subscript[σ, N])=(σ/(Sqrt[N]))
in the limit of large N. In other words, regardless what type of distribution of (Subscript[x, i]), if you take the average of many of them, the more the better, then the average itself would have a Gaussian/ND distribution.

This central limit theorem is the intuition behind “collecting more data and do average”. Whenever we want to measure something, like the length of an object, or the temperature of a heat or AC outlet, or our weight, we can do it just once. But we might not trust the accuracy and precision of just one measurement (or we hope the 1st weight measurement is way off-scale). We want to do several measurements and take the average (the mean). Why is the average of many is a better measurement than just one single measurement?

The reason is that the standard of deviation of many measurements, like N, is smaller than the standard of deviation of one measurement by (1/(Sqrt[N])). Do a measurement 10 times, and we are (Sqrt[10])=3.2 times more confident of the result.

Here is an example. Let’s generate 10 random numbers with uniform distribution between 0 and 1:

range1={0,1};numberofsample1=10;
xCLT1=RandomReal[range1,numberofsample1]

Now we take the mean of that:

Mean @ xCLT1

Now, do that say 1000, 2000 ... times and see what the distribution of the mean is. We don’t have to use a do loop for whatever. We can do this: generate 5000 sets of 10 random numbers with one function:

range1={0,1};numberofset=5000; numberineachset=10;
xCLT1=RandomReal[range1,{numberofset,numberineachset}];

So, xCLT1 above contains 5000 sets, 10 numbers in each set for a total of 50,000 numbers. Here to see it:

Dimensions[xCLT1]

Here are the first 5 sets:

Grid[Take[xCLT1,5],BaseStyle->{18}]

. We can take the mean of each set of the above, like this:

mean5=Mean /@ Take[xCLT1,5]

Here is another way to show the result:

Grid[Join[{Join[{"set","mean"},Range[10]]},
MapThread[Join,{Transpose[{Range[5],mean5}],Take[xCLT1,5]}]],Frame-> All,BaseStyle->{18}
,Background->{{Hue[0.55,0.4,1],Hue[0.35,0.4,1],},{Hue[0.15,0.5,1],}}]

The above is only for the first 5 sets of xCLT1. We can do for all 5000 to get 5000 values of mean, then show the histogram:

meanofall=Mean /@ xCLT1 ;
Histogram[meanofall]

What is its SD?

StandardDeviation[meanofall]

Instead of 10, let’s do 40 numbers in each set:

numberofset=5000; numberineachset=40;
xCLT2=RandomReal[{0,1},{numberofset,numberineachset}];
meanofall=Mean /@ xCLT2 ;
Histogram[meanofall]
StandardDeviation[meanofall]

Notice something? the SD of these sets with 40 number is 0.045737, which is ~ 1/2 of the SD of sets with 10 numbers: 0.0920633. 
(1/2)=(1/(Sqrt[4])): this is the central limit theorem: if we increase n, the number of variables in each set, the SD of its mean goes down as (1/(Sqrt[n]))

Exercise - write code to simulate and demonstrate the above discussion (central limit theorem)

This is project that you can work on your own. Generate a random array of numbers, for example 20, use uniform distribution for example. Take their mean. Then do this for a large number of trials, for example 1000, 5000,... then plot the histogram of all the means and you will see it is approximately a normal distribution. 
If you quadruple the number of variables, like from 20 to 80, you will see that the standard of deviation drops by (1/(Sqrt[4])) or 1/2.

5.2 A demo

Manipulate[
If[!TrueQ[{Npold,ntrialold}== {Np,ntrial}],RUNCLTsim=True;
{Npold,ntrialold}= {Np,ntrial},]; 

If[TrueQ[RUNCLTsim] ,
If[Np=="all",nVar={20,80,320,1280},nVar={Np}];

(* Marr generates ntrial event, each event has n number of uniformly distributed random
number between 0 and 1   *)
Marr=Table[Mean /@ RandomReal[{0.,1.},{ntrial,n}],{n,nVar}] ;

(* estimate of mean and SD of ntrial event *)
μarr=Mean /@ Marr ; σarr=StandardDeviation /@ Marr ;

If[TrueQ[Np=="all"],

(* a log log plot of ntrial vs. SD estimate to show 1/(Sqrt[N]) scaling behavior
of entral-limit theorem  *)
cltplot=Show[ListLogLogPlot[Transpose[{nVar,σarr}],PlotStyle-> {Red,PointSize[0.035]},PlotRange-> All
,Frame-> True, GridLines-> Automatic 
,FrameLabel-> {"number of variables","standard of deviation estimate"}
,Epilog->Inset[Style["note the 1/(Sqrt[N]) scaling
per central-limit theorem",18,Italic],ImageScaled[{0.7,0.85}]]]
,Graphics[{Hue[0.3,1,0.7],Thickness[0.012],Line @ Transpose[{Log[nVar],Log[σarr]}]}]
,ImageSize-> 400] 
,];
histoCALC=True; RUNCLTsim=False;iCLTSIMready=True
,];

If[TrueQ[histoCALC] || !TrueQ[htypeold==htype],histo=Table[Show[Histogram[Marr[[m]],Automatic,htype
,ChartStyle->Hue[(((m-1))/(Length[nVar]+1)),0.8,1.]
, ChartElementFunction->"FadingRectangle"
,PlotRange->{ {0.3,0.7},All}
,Frame-> True, GridLines-> Automatic
,Epilog->{ Inset[Style["(OverHat[σ])="<>ToString @ NumberForm[σarr[[m]],{5,4}]]
,Scaled[{0.8,0.85}]
,BaseStyle-> {16}]
,Inset["N="<>ToString @nVar[[m]],Scaled[{0.2,0.85}]
,BaseStyle-> {18,Bold}]}
,ImageSize->  250]
,Plot[If[htype== "PDF", (1/((OverHat[σ])(Sqrt[2 π])))(E^(-((((x-(OverHat[μ])))^2)/(2 ((OverHat[σ])^2))))) ,-((((x-(OverHat[μ])))^2)/(2 ((OverHat[σ])^2)))-Log[(OverHat[σ])(Sqrt[2 π])]]/. {(OverHat[μ]) -> μarr[[m]] , (OverHat[σ])->σarr[[m]]}
 ,{x,μarr[[m]]-3.7 σarr[[m]],μarr[[m]]+3.7σarr[[m]]},PlotStyle-> {Thickness[0.015],Hue[0.5+(m-1)*0.18,1.,1,0.7]},PlotRange-> All]]
 ,{m,Length[nVar]}] ;
histoCALC=False; htypeold= htype
,];

If[TrueQ[iCLTSIMready],Column[{Grid[Partition[histo,2,2,1,""]]
,If[TrueQ[Np=="all"],cltplot,]}]
,"Click button to start simulation"]

,{{Np,"all","number of variables"},{"all",20,80,320,1280}}
,{{ntrial,5000,"number of trials"},{500,1000,5000,10000}}
,Button["RUN simulation",{RUNCLTsim=True;iCLTSIMready=False}]
,{htype,{"PDF","LogPDF"}}
,Initialization:>{iCLTSIMready=False}]

Supplementary - tutorial on (pseudo-)random number generation

T.1  Uniform-distribution (pseudo-)random number generation

How to generate uniformly distributed random numbers with RandomReal (you have encountered before).

lowerlimit=-1; upperlimit=1;
RandomReal[{lowerlimit, upperlimit}]

lowerlimit=-1; upperlimit=1; numberofsample=5;
RandomReal[{lowerlimit, upperlimit},numberofsample]

Exercise - generate an array of RandomReal

Generate an array of 100, 500, and 1000 RandomReal number between 0. and 1. and:
1- plot the array
2- make a histogram

Answer

lowerlimit=0. ; upperlimit=1; numberofsample=100;
x=RandomReal[{lowerlimit, upperlimit},numberofsample];
ListLinePlot[x]
Histogram[x]

lowerlimit=0. ; upperlimit=1; numberofsample=500;
x=RandomReal[{lowerlimit, upperlimit},numberofsample];
ListLinePlot[x]
Histogram[x]

lowerlimit=0. ; upperlimit=1; numberofsample=1000;
x=RandomReal[{lowerlimit, upperlimit},numberofsample];
ListLinePlot[x]
Histogram[x]

Exercise - generate an array of RandomReal with choice of sample population with Manipulate

Use the code you have above in a Manipulate function to make sample population selectable for 100, 250, 1000, 2500, 10000, 25000, and 100000. Then, plot the data and its histogram. (Insert the plot codes into the Manipulate shell.). Based on the histogram, what kind of distribution is it? Is it similar to the Laplacian or lognormal or Poisson that you learned in previous lessons?
Below is shell of a Manipulate (just insert you plot code in side; the nsample is just for demo, remove it)

Manipulate[
(* when you click different value of nsample, you will see this change  *)
nsample

,{nsample,{100,250,1000,2500,10000,25000,100000}, ControlType-> SetterBar}
]

Answer

lowerlimit=0. ; upperlimit=1; 

Manipulate[

x=RandomReal[{lowerlimit,upperlimit},nsample] ;
{ListLinePlot[x] ,Histogram[x]}

,{nsample,{100,250,1000,2500,10000,25000,100000}, ControlType-> SetterBar}
]

The plots are too small to see, we can do two things:
1- specify how large the graph is by using: ImageSize-> 400 
2- Put the two plots side by side by using Grid.
you have done Grid before.

lowerlimit=0. ; upperlimit=1; 

Manipulate[

x2=RandomReal[{lowerlimit,upperlimit},nsample] ;
(* we wnat *)
Grid[{{ListLinePlot[x2, ImageSize-> 400]
,Histogram[x2, ImageSize-> 400]}}]

,{nsample,{100,250,1000,2500,10000,25000,100000}, ControlType-> SetterBar}
]

Clearly, the distribution we see is uniform: the probability is constant over the entire range.

T.2  RandomVariate[]: General (pseudo-)random number generation

In general, we often want to generate an array that has certain distribution that is not uniform. RandomVariate[] is the function to use. Let’s say we want to generate 1000 numbers with a Poisson distribution.

ndata=1000; μPt2=5.5;
xP=RandomVariate[PoissonDistribution[μPt2],ndata];
Histogram[xP,{1}]

Below is an example for Gaussian:

ndata=1000; μG=3; σG=1.5;
xG=RandomVariate[NormalDistribution[μG,σG],ndata];
Histogram[xG,20]