Skip to content

Instantly share code, notes, and snippets.

@oborchers
Last active February 15, 2019 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oborchers/a7f94a4377783caef5c293127886852f to your computer and use it in GitHub Desktop.
Save oborchers/a7f94a4377783caef5c293127886852f to your computer and use it in GitHub Desktop.
Simple Gaussian Mixture with TF Eager / Probability
import numpy as np
import tensorflow as tf
from tensorflow_probability import distributions as tfd
tf.enable_eager_execution()
# Set values for the mixture
alphas = [0.6, 0.3, 0.1]
means = [30, 60, 120]
sigmas = [5, 3, 1]
# Use the tf.probability class
gm = tfd.MixtureSameFamily(
mixture_distribution=tfd.Categorical(probs=alphas),
components_distribution=tfd.Normal(
loc=means,
scale=sigmas))
# Draw 1e5 random samples from the mixture distribution
prices = gm.sample(sample_shape=(int(1e5)), seed=42)
# Create a normal distribution with empirical mean & std. deviation
nd_empirical = tfd.Normal(loc=np.mean(prices), scale=np.std(prices))
print(np.mean(prices))
print(np.std(prices))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment