In [3]:
# Probability Density Function (pdf) of standard normal distribution
plot(1/sqrt(2 *pi) * exp(-x^2/2), (x,-5,5))
Out[3]:
In [7]:
# zoom in 
plot(1/sqrt(2 *pi) * exp(-x^2/2), (x,-5,-4))
# never actually reaches 0, but gets super small very quickly if |x| large
Out[7]:
In [34]:
# What is prob that standard normal r.v. lies in (2,3)?
N(integral(1/sqrt(2 *pi) * exp(-x^2/2), (x,2,3)))
# N() does numerical approximation
Out[34]:
0.0214002339165491
In [11]:
N(integral(1/sqrt(2 *pi) * exp(-x^2/2), (x,-1,1)))
Out[11]:
0.682689492137086
In [ ]:
# Above means that for any normal distribution,
# the probability of lying within one
# standard deviation of the average is 0.68...
In [12]:
N(integral(1/sqrt(2 *pi) * exp(-x^2/2), (x,-2,2)))
Out[12]:
0.954499736103642
In [14]:
N(integral(1/sqrt(2 *pi) * exp(-x^2/2), (x,-3,3)))
Out[14]:
0.997300203936740
In [ ]:
# 68, 95, 99.7 Rule from Statistics
# probabilities that normally dist r.v. lies within 
# 1,2,3 standard deviations, respectively, of average value
In [15]:
# Let's verify that average of standard normal is 0 
# average of dist given by pdf f is:
# integral(x f(x), (x, -infty, infty))
integral(x* 1/sqrt(2 *pi) * exp(-x^2/2), (x,-oo,oo))
Out[15]:
0
In [16]:
# Variance (when average is 0)
# integral( x^2 f(x), (x, -infty, infty))
integral(x^2* 1/sqrt(2 *pi) * exp(-x^2/2), (x,-oo,oo))
Out[16]:
1
In [17]:
# total probability should be 1 
integral(1/sqrt(2 *pi) * exp(-x^2/2), (x,-oo,oo))
Out[17]:
1
In [18]:
# Defining and drawing graphs
import networkx as nx
# Python package that we'll use for drawing pictures of graphs
In [19]:
G = nx.Graph() # command to make new empty graph
In [20]:
nx.draw(G) # command for drawing graph (empty, since graph is still empty)
In [21]:
G.add_nodes_from([0,1,2]) # add some vertices labeled 0,1,2
In [22]:
nx.draw(G)
In [23]:
G.add_edges_from([(0,1),(1,2)]) # add two edges connecting 0,1 and 1,2
In [24]:
nx.draw(G)
In [25]:
# Usually will represent graph as an adjacency matrix A
# A is an nxn matrix, where n is the number of vertices
# vertices are labelled as 0,1,...,n-1
# A[i,j] = 1 if i,j are connected, and A[i,j]=0 otherwise
In [26]:
# For G above (n=3)
A = [[0,1,0], [1,0,1], [0,1,0]]
In [30]:
import numpy as np # allows us to use numpy array
H=nx.Graph(np.array(A))
In [33]:
nx.draw(H)
In [ ]:
 
In [ ]: