Skip to content

Instantly share code, notes, and snippets.

@joelouismarino
Created April 8, 2018 05:41
Show Gist options
  • Save joelouismarino/621ac2b8137688f40697ad41808ab4b7 to your computer and use it in GitHub Desktop.
Save joelouismarino/621ac2b8137688f40697ad41808ab4b7 to your computer and use it in GitHub Desktop.
import math
import torch
from torch.distributions import Normal
# standard univariate Gaussian (Normal)
mean = torch.zeros(1)
std = torch.ones(1)
# evaluate from -0.5 to 0.5
x_min = -0.5 * torch.ones(1)
x_max = 0.5 * torch.ones(1)
# using built-in functions
normal = Normal(mean, std)
prob_mass = normal.cdf(x_max) - normal.cdf(x_min)
# implemented from scratch
def cdf(mu, sigma, val):
return 0.5 * (1 + torch.erf((value - mu) / (math.sqrt(2) * sigma)))
prob_mass = cdf(mean, std, x_max) - cdf(mean, std, x_min)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment