353 - Topic 05 - Discrete Variables

2788 days ago by Professor353

John Travis

Mississippi College

MATH 353 - Introduction to Mathematical Probability and Statistics

Textbook:  Tanis and Hogg, A Brief Course in Mathematical Statistics

Discrete random variables, mass functions, distributions and expected values.

%auto D = [1,2,3,5,6,8,9,11,12,14] # Domain Values Probs = [0.05,0.05,0.05,0.15,0.05,0.2,0.2,0.05,0.05,0.15] # Probabilities n = len(D) R = range(n) 
       
%auto one_huh = sum(Probs) html('\n\nJust to be certain, we should check to make certain the probabilities sum to 1\n') html('$\sum_{x\epsilon R} f(x) = %s$\n\n'%str(one_huh)) 
       


Just to be certain, we should check to make certain the probabilities sum to 1



                                
                            


Just to be certain, we should check to make certain the probabilities sum to 1



                                
%auto G = Graphics() if len(D)==len(Probs): f = zip(D,Probs) meanf = 0 variancef = 0 for k in R: meanf += D[k]*Probs[k] variancef += D[k]^2*Probs[k] G += line([(D[k],0),(D[k],Probs[k])],color='green') variancef = variancef - meanf^2 sd = sqrt(variancef) G += points(f,color='blue',size=50) G += point((meanf,0),color='yellow',size=60,zorder=3) G += line([(meanf-sd,0),(meanf+sd,0)],color='red',thickness=5) plot(G,ymin=0).show() g = DiscreteProbabilitySpace(D,Probs) html('<center>$ mean = %s$</center>'%str(meanf)) html('<center>$ variance = %s$</center>'%str(variancef)) # perhaps to add mean and variance for pmf here else: print 'Domain D and Probabilities Probs must be lists of the same size' 
       
# Now, let's sample from the distribution given above and see how a random sampling matches up @interact def _(n_samples=(100..10000)): counts = [0] * len(Probs) X = GeneralDiscreteDistribution(Probs) sample = [] for _ in range(n_samples): elem = X.get_random_element() sample.append(D[elem]) counts[elem] += 1 Empirical = [1.0*x/n_samples for x in counts] # random samplemean = mean(sample) samplevariance = variance(sample) sampdev = sqrt(samplevariance) E = points(zip(D,Empirical),color='orange',size=40) E += point((samplemean,0.005),color='brown',size=60,zorder=3) E += line([(samplemean-sampdev,0.005),(samplemean+sampdev,0.005)],color='orange',thickness=5) show(G+E) 
       

Click to the left again to hide and once more to show the dynamic interactive window

# Combining all of the above into one interactive cell @interact def _(D = input_box([1,2,3,5,6,8,9,11,12,14],label="Enter domain R (in brackets):"), Probs = input_box([1/20,1/20,1/20,3/20,1/20,4/20,4/20,1/20,1/20,3/20],label="Enter corresponding f(x) (in brackets):"), n_samples=slider(100,10000,100,100,label="Number of times to sample from this distribution:")): n = len(D) R = range(n) one_huh = sum(Probs) pretty_print('\n\nJust to be certain, we should check to make certain the probabilities sum to 1\n') pretty_print(html('<center>$\sum_{x\epsilon R} f(x) = %s$</center>'%str(one_huh))) G = Graphics() if len(D)==len(Probs): f = zip(D,Probs) meanf = 0 variancef = 0 for k in R: meanf += D[k]*Probs[k] variancef += D[k]^2*Probs[k] G += line([(D[k],0),(D[k],Probs[k])],color='green') variancef = variancef - meanf^2 sd = sqrt(variancef) G += points(f,color='blue',size=50) G += point((meanf,0),color='yellow',size=60,zorder=3) G += line([(meanf-sd,0),(meanf+sd,0)],color='red',thickness=5) g = DiscreteProbabilitySpace(D,Probs) pretty_print(' mean = %s'%str(meanf)) pretty_print(' variance = %s'%str(variancef)) # perhaps to add mean and variance for pmf here else: print 'Domain D and Probabilities Probs must be lists of the same size' # Now, let's sample from the distribution given above and see how a random sampling matches up counts = [0] * len(Probs) X = GeneralDiscreteDistribution(Probs) sample = [] for _ in range(n_samples): elem = X.get_random_element() sample.append(D[elem]) counts[elem] += 1 Empirical = [1.0*x/n_samples for x in counts] # random samplemean = mean(sample) samplevariance = variance(sample) sampdev = sqrt(samplevariance) E = points(zip(D,Empirical),color='orange',size=40) E += point((samplemean,0.005),color='brown',size=60,zorder=3) E += line([(samplemean-sampdev,0.005),(samplemean+sampdev,0.005)],color='orange',thickness=5) (G+E).show(ymin=0,figsize=(8,3)) 
       
Enter domain R (in brackets): 
Enter corresponding f(x) (in brackets): 
Number of times to sample from this distribution: 

Click to the left again to hide and once more to show the dynamic interactive window

# THIS GENERALIZED INTERACT NOT YET COMPLETED. FOR LATER IMPLEMENTATION. # This function is used to convert an input string into separate entries def g(s): return str(s).replace(',',' ').split() @interact def _(D=input_box('1,2,3,15'),probs=input_box('0.1,0.3,0.2,0.4')): C = range(len(D)) R = (ZZ(k) for k in g(D)) print R,C f1 = g(probs) f = (float(k) for k in f1) # f = zip(R,f2) G = Graphics() for k in C: print k print f1[k] G += line([(k,0),(k,f1[k])],color='green')+point((k,f1[k]),size=50) # G += line([(k,0),(k,f[k][1])],color='green')+point((k,f[k][1]),size=50) plot(G,ymin=0) 
       

Click to the left again to hide and once more to show the dynamic interactive window

reset()