Skip to content

Instantly share code, notes, and snippets.

from random import randint, choice
import numpy as np
class Passenger():
def __init__(self, desired_floor = 0, current_floor = 0):
self.desired_floor = desired_floor #floor passenger wants to go to
self.current_floor = current_floor #current floor the elevator is at
self.waittime = 0 #total waittime for the passenger
from math import sqrt
class Car():
#define a car and its attributes
def __init__(self, mileage = 0, make = "Tesla", model = "Model X", x = 0, y = 0):
self.mileage = mileage
self.make = make
self.model = model
self.x = x
TASK 1:
def bad_proposal_rvs(size):
return {
'mu': sts.norm.rvs(loc=1, scale=3, size=num_samples),
'sigma': sts.norm.rvs(loc=1, scale=3, size=num_samples)}
def good_proposal_rvs(size):
return {
'mu': np.concatenate((sts.norm.rvs(loc=-4.5, scale=1, size=1000), sts.norm.rvs(loc=3, scale=1, size=4000))),
def norminvgamma_rvs(mu, nu, alpha, beta, size=1):
'''
Generate n samples from the normal-inverse-gamma distribution. This function
returns a (size x 2) matrix where each row contains a sample, (x, sigma2).
'''
sigma2 = stats.invgamma.rvs(a=alpha, scale=beta, size=size) # Sample sigma^2 from the inverse-gamma
x = stats.norm.rvs(loc=mu, scale=np.sqrt(sigma2 / nu), size=size) # Sample x from the normal
return np.vstack((x, sigma2)).transpose()
samples = norminvgamma_rvs(mu_1, nu_1, alpha_1,beta_1, size = 1000)
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import pystan
# Field goal rate data. Each tuple contains (field goals made, field goals attempted).
player_field_goal_data = {
# http://www.espn.com/nba/player/gamelog/_/id/2594922/year/2018/otto-porter-jr
'Otto Porter Jr': [(0, 0), (4, 9), (4, 8), (3, 7), (5, 10), (4, 7), (0, 0), (5, 8), (4, 9)],
# http://www.espn.com/nba/player/gamelog/_/id/3024/year/2018/jj-redick
#Task 1
from scipy.special import betaln
import numpy as np
def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import pystan
electoral_votes = {
'Alabama': 9,
'Alaska': 3,
'Arizona': 11,
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import stats
plt.figure(1, figsize=(12, 10))
n=1000
p = .1
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
social_data = pd.read_csv('socialmobility.csv')
print (social_data)
#establish alphas_0 and prior
##Task 1
stan_results_c = stan_model.sampling(data=eczema_data['control'])
print(stan_results_c.stansummary(pars=['p'], probs=[0.025, 0.5, 0.975]))
posterior_samples_c = stan_results_c.extract()
print(
"Posterior 95% confidence interval for p:",
np.percentile(posterior_samples_c['p'], [2.5, 97.5]))
plt.hist(posterior_samples['p'], bins=50, density=True)