Skip to content

Instantly share code, notes, and snippets.

@fohria
Created December 13, 2017 18:26
Show Gist options
  • Save fohria/e8d2f23db316c7ca7c55fe05d9617e71 to your computer and use it in GitHub Desktop.
Save fohria/e8d2f23db316c7ca7c55fe05d9617e71 to your computer and use it in GitHub Desktop.
example of multiprocessing usage
import numpy as np
import pandas as pd
from scipy.optimize import minimize
import sys
from multiprocessing import Pool
# local modules and functions imported here
sys.path.append('../experiment/simulation')
from utils import softmax, autocorrelation
class MaxLike(object):
def __init__(self, df):
self.number_of_actions = 2
self.dataframe = df
def neg_log_likelihood(self, alphabeta):
df = self.dataframe
alpha = alphabeta[0]
beta = alphabeta[1]
actions, rewards = df.action.values, df['reward'].values
prob_log = 0
Q = np.zeros(self.number_of_actions)
for action, reward in zip(actions, rewards):
Q[action] += alpha * (reward - Q[action])
prob_log += np.log(softmax(Q, beta)[action])
return -2 * prob_log # -2 because reasons
def ml_estimation(self, start_parameters):
# print("running estimation with parameters: {}".format(start_parameters))
rawr = minimize(self.neg_log_likelihood, start_parameters,
method='L-BFGS-B', bounds=((0.01,2), (0.01,5)),
options={'disp': False})
return rawr
def ml_estimation_multicore(self, start_parameters):
""" start_parameters is a list of the form [[0.1, 0.2]..[0.9, 0.9]]"""
print("booting up extra core engines to run estimation...")
with Pool() as pooler:
results = pooler.map(self.ml_estimation, start_parameters)
best_bic = 10000
best_index = None
for index, result in enumerate(results):
lnlike = -result['fun']
bic = 2 * np.log(80) - lnlike
if bic < best_bic:
best_bic = bic
best_index = index
return results[best_index]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment