Created
April 8, 2018 05:41
-
-
Save joelouismarino/621ac2b8137688f40697ad41808ab4b7 to your computer and use it in GitHub Desktop.
This file contains 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 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