# plot of Probability Density Function (pdf) of standard normal distribution
plot(1/sqrt(2*pi) * exp(-x^2/2), (x,-4,4))
plot(1/sqrt(2*pi) * exp(-x^2/2), (x,-4,-3))
# never actually hits 0, but approaches it very quickly
# 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
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, -1,1)))
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, -2,2)))
N(integral(1/sqrt(2*pi) * exp(-x^2/2), (x, -3,3)))
# 68, 95, 99.7 Rule from statistics
# probabilities of lying within 1,2,3, standard devitations, respectively
# of average, for normally distributed random variable.
# 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))
# above is also clear because of symmetry of f
# 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))
import numpy as np
np.random.normal() # generates sample from standard normal N(0,1) dist
import matplotlib.pyplot as plt
plt.hist([np.random.normal() for n in range(10000)], bins=21)
# above histogram matches shape of normal distribution pdf, as it should
# Graphs/Networks
# package useful for making pictures of graphs
import networkx as nx
G = nx.Graph() # command to make new (empty) graph
nx.draw(G) # command for drawing graph (empty, since graph is still empty)
G.add_nodes_from([0,1,2]) # add some vertices labeled 0,1,2
nx.draw(G)
G.add_edges_from([(0,1), (1,2)]) # add two edges connecting 0,1 and 1,2
nx.draw(G)
# 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
# For G above:
# n= 3
A = [[0, 1, 0], [1, 0, 1], [0,1,0]]
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
nx.draw(H)
J = nx.Graph(np.array([[0,1,1,1], [1,0,1,1], [1,1,0,1], [1,1,1,0]]))
nx.draw(J)