In [3]:
# plot of Probability Density Function (pdf) of standard normal distribution 
plot(1/sqrt(2*pi) * exp(-x^2/2), (x,-4,4))
Out[3]:
In [4]:
plot(1/sqrt(2*pi) * exp(-x^2/2), (x,-4,-3))
# never actually hits 0, but approaches it very quickly
Out[4]:
In [6]:
# What is prob that standard normally distributed random variable lies in
# (2,3)?
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, 2,3)))
# N() does numerical approximation
Out[6]:
0.0214002339165491
In [7]:
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, -1,1)))
Out[7]:
0.682689492137086
In [8]:
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, -2,2)))
Out[8]:
0.954499736103642
In [9]:
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, -3,3)))
Out[9]:
0.997300203936740
In [ ]:
# 68, 95, 99.7 Rule from statistics
# probabilities of lying within 1,2,3, standard devitations, respectively 
# of average, for normally distributed random variable.  
In [10]:
# What is average value of normal distribution? 
# In general, 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[10]:
0
In [11]:
# above is also clear because of symmetry of f
In [12]:
# What is variance of standard normal dist?
# integral(x^2 f(x), (x, -infty, infty)
integral(x^2 * 1/sqrt(2*pi) * exp(-x^2/2), (x, -oo,oo))
Out[12]:
1
In [13]:
import numpy as np
In [23]:
np.random.normal() # generates sample from standard normal N(0,1) dist
Out[23]:
1.5851381524213268
In [24]:
import matplotlib.pyplot as plt
In [28]:
plt.hist([np.random.normal() for n in range(10000)], bins=21)
Out[28]:
(array([5.000e+00, 3.800e+01, 9.200e+01, 1.880e+02, 3.380e+02, 5.980e+02,
        9.430e+02, 1.199e+03, 1.379e+03, 1.427e+03, 1.250e+03, 1.027e+03,
        6.770e+02, 4.140e+02, 2.330e+02, 1.200e+02, 5.100e+01, 1.500e+01,
        5.000e+00, 0.000e+00, 1.000e+00]),
 array([-3.33451112, -2.969702  , -2.60489288, -2.24008376, -1.87527464,
        -1.51046552, -1.14565641, -0.78084729, -0.41603817, -0.05122905,
         0.31358007,  0.67838919,  1.04319831,  1.40800743,  1.77281655,
         2.13762567,  2.50243479,  2.86724391,  3.23205303,  3.59686214,
         3.96167126,  4.32648038]),
 <a list of 21 Patch objects>)
In [29]:
# above histogram matches shape of normal distribution pdf, as it should 
In [30]:
# Graphs/Networks
# package useful for making pictures of graphs
import networkx as nx
In [34]:
G = nx.Graph() # command to make new (empty) graph
In [35]:
nx.draw(G) # command for drawing graph (empty, since graph is still empty)
In [36]:
G.add_nodes_from([0,1,2]) # add some vertices labeled 0,1,2
In [37]:
nx.draw(G)
In [38]:
G.add_edges_from([(0,1), (1,2)]) # add two edges connecting 0,1 and 1,2
In [39]:
nx.draw(G)
In [40]:
# Usually we'll represent graph as an adjacency matrix A
# A is a nxn matrix, where n is the number of vertices
# vertices are labelled 0,1,...,n-1
# A[i,j] = 1 if i,j are connected, and 0 otherwise 
In [43]:
# For G above: 
# n= 3
A = [[0, 1, 0], [1, 0, 1], [0,1,0]]
In [47]:
H=nx.Graph(np.array(A)) # can use Graph() to make a new graph
# specified by an adjacency matrix by making the argument an numpy array
In [50]:
nx.draw(H)
In [51]:
J = nx.Graph(np.array([[0,1,1,1], [1,0,1,1], [1,1,0,1], [1,1,1,0]]))
In [52]:
nx.draw(J)
In [ ]: