Skip to content

Instantly share code, notes, and snippets.

class ThompsonSampler:
"""Thompson Sampling using a Beta distribution associated with each option.
The beta distribution will be updated when rewards associated with each option
are observed.
"""
def __init__(self, env, n_learning=0):
# boilier plate data storage
self.env = env
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.utils.data import Dataset, DataLoader, SubsetRandomSampler
from torchvision.datasets.folder import ImageFolder, default_loader
from torchvision.datasets.utils import check_integrity
from torchvision import transforms
from torchvision import models
import matplotlib.pyplot as plt
# usr/bin/env python3
# author: Conor McDonald
# torch==0.4.1
# numpy==1.14.3
import torch
import torch.nn as nn
import torch.nn.functional as F
class ThompsonSampler(BaseSampler):
def __init__(self, env):
super().__init__(env)
def choose_k(self):
# sample from posterior (this is the thompson sampling approach)
# this leads to more exploration because machines with > uncertainty can then be selected as the machine
self.theta = np.random.beta(self.a, self.b)
# select machine with highest posterior p of payout
class eGreedy(BaseSampler):
def __init__(self, env, n_learning, e):
super().__init__(env, n_learning, e)
def choose_k(self):
# e% of the time take a random draw from machines
# random k for n learning trials, then the machine with highest theta
self.k = np.random.choice(self.variants) if self.i < self.n_learning else np.argmax(self.theta)
class RandomSampler(BaseSampler):
def __init__(self, env):
super().__init__(env)
def choose_k(self):
self.k = np.random.choice(self.variants)
return self.k
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from scipy.stats import beta
sns.set_style("whitegrid")
class Environment:
def __init__(self, variants, payouts, n_trials, variance=False):
#%load_ext cythonmagic
#%cython
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set_style("whitegrid")
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set_style("whitegrid")
get_ipython().run_line_magic('matplotlib', 'inline')
from IPython.core.display import display, HTML
# coding: utf-8
# In[150]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns