Skip to content

Instantly share code, notes, and snippets.

@tigershen23
tigershen23 / posterior_upvote_ratio.py
Last active March 11, 2018 13:31
Example from Ch 4 of Bayesian Methods for Hackers.
import pymc3 as pm
def posterior_upvote_ratio(upvotes, downvotes, samples = 20000):
  “””
  This function accepts the number of upvotes and downvotes a particular submission recieved,
  and the number of posterior samples to return to the user. Assumes a uniform prior.
  “””
  N = upvotes + downvotes
  with pm.Model() as model:
upvote_ratio = pm.Uniform(“upvote_ratio”, 0, 1) # (1)
import torch
import pyro
import pyro.distributions as dist
from pyro.infer import SVI, Trace_ELBO
from pyro.optim import Adam
from __future__ import print_function
import math
import os
import torch
import torch.distributions.constraints as constraints
import pyro
from pyro.optim import Adam
from pyro.infer import SVI, Trace_ELBO
import pyro.distributions as dist
pyro.enable_validation(True)
pyro.clear_param_store()
# Tiger Jason James
data = torch.cat((torch.zeros(9), torch.ones(7), torch.empty(4).fill_(2.)))
def model(data):
alpha = torch.tensor(6.0)
beta = torch.tensor(10.0)
pay_probs = pyro.sample('pay_probs', dist.Beta(alpha, beta).expand(3).independent(1))
normalized_pay_probs = pay_probs / torch.sum(pay_probs)
with pyro.iarange('data_loop', len(data)):
pyro.sample('obs', dist.Categorical(probs=normalized_pay_probs), obs=data)
def guide(data):
alphas = pyro.param('alphas', torch.tensor(6.).expand(3), constraint=constraints.positive)
betas = pyro.param('betas', torch.tensor(10.).expand(3), constraint=constraints.positive)
pyro.sample('pay_probs', dist.Beta(alphas, betas).independent(1))
def print_progress():
alphas = pyro.param("alphas")
betas = pyro.param("betas")
if torch.cuda.is_available():
alphas.cuda()
betas.cuda()
means = alphas / (alphas + betas)
normalized_means = means / torch.sum(means)
adam_params = {"lr": 0.0005}
optimizer = Adam(adam_params)
svi = SVI(model, guide, optimizer, loss=Trace_ELBO())
n_steps = 2501
for step in range(n_steps):
svi.step(data)
if step % 100 == 0:
print_progress()
@tigershen23
tigershen23 / ruby_unit_tests_plp.md
Last active February 17, 2019 21:41
Perceptual Learning Playlist of Ruby Unit Tests
  • Thor::Util#snake_case: well-isolated test cases. Clear and thorough about expected behavior with potentially ambiguous input
  • HTTParty.normalize_base_uri: describes behavior well for a method that might be confusing at first
  • Virtus::Attribute#==: good use of intermediate variable for clarity. tests are collectively exhaustive within the scope of the method
  • AASM::Core::Event#fire: good isolation of complex behavior; good use of mocks and doubles. when/then/expect division is clear (BDD). good use of context
  • [Money.from_amount](https://gi