Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
# Bradley J. Setzler
# Tutorial 5: Parallel Processing in Julia: Bootstrapping the MLE
# Python Comparison Script
import numpy as np
from scipy.stats import multivariate_normal as mn
from scipy.optimize import fmin_cg
import multiprocessing as mp
import time
X = np.random.multivariate_normal(np.zeros(K),np.eye(K),N)
constant = np.ones((N,1))
X = np.hstack((constant,X))
epsilon = np.random.normal(0,1,N)
trueParams = np.linspace(-K/2,K/2,K+1)*.02
Y =,trueParams) + epsilon
params0 = np.hstack((trueParams,1))
def loglike(rho,x,y):
beta = rho[0:K+1]
sigma2 = np.exp(rho[K+1])
residual =,beta)
contributions = mn.logpdf(residual,0,sigma2)
loglikelihood = np.sum(contributions)
return -loglikelihood
#def wrapLoglike(rho):
# return loglike(rho,X,Y)
#out = fmin_cg(wrapLoglike,params0)
def bootstrapSamples(B):
print "hi"
samples = np.zeros((B,K+2))
for b in range(B):
theIndex = np.random.permutation(N)[0:M]
x = X[theIndex]
y = Y[theIndex]
def wrapLoglike(rho):
return loglike(rho,x,y)
samples[b,:] = fmin_cg(wrapLoglike,params0)
samples[:,K+1] = np.exp(samples[:,K+1])
print "bye"
return samples
B = 1000
b = 1000//4
time0 = time.time()
samples = bootstrapSamples(B)
time_1proc = time.time() - time0
pool = mp.Pool(4)
time0 = time.time()
samples =,[b,b,b,b])
time_4proc = time.time() - time0