Created
September 1, 2014 00:28
-
-
Save rcalsaverini/774220ff42afc139c167 to your computer and use it in GitHub Desktop.
Note to self
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pnd | |
import numpy as np | |
import numpy.random as rnd | |
from collections import namedtuple | |
class Sampler(list): | |
def __init__(self, items, weights=None): | |
super(Sampler, self).__init__(items) | |
_weights = np.array(weights) if weights else np.ones(len(self)) | |
self.probabilities = _weights / _weights.sum() | |
def sample(self, size=1): | |
indices = rnd.choice(len(self), size, p=self.probabilities) | |
output = [self[index] for index in indices] | |
return output[0] if size == 1 else output | |
def simulate(mixture, num_sims, sim_horizon): | |
columns = pnd.MultiIndex.from_product([['samples', 'names'], range(num_sims)]) | |
df = pnd.DataFrame(columns=columns) | |
for sim in xrange(num_sims): | |
for t in xrange(sim_horizon): | |
process = mixture.sample() | |
df.loc[t, [('names', sim), ('samples', sim)]] = [process.name, process.sample()] | |
return df | |
process = namedtuple("process", ["name", "sample"]) | |
mixture = Sampler([ | |
process("good", lambda : rnd.bernoulli(0.9)), | |
process("bad", lambda : rnd.bernoulli(0.5)) | |
]) | |
df = simulate(mixture, 10, 100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment