Tutorial: Basic plots

In this tutorial, the basic plot types of trianglechain are introduced

from trianglechain import TriangleChain
from trianglechain.TriangleChain import ensure_rec
import numpy as np
def get_samples(n_samples=100000, n_dims=3):
    covmat = np.random.normal(size=(n_dims, n_dims))
    covmat = np.dot(covmat.T, covmat)
    mean = np.random.uniform(size=(n_dims))
    samples = np.random.multivariate_normal(mean=mean, cov=covmat, size=(n_samples))
    return samples


samples = get_samples()
samples = ensure_rec(samples, names=["a", "b", "c"])

Contour plot

tri = TriangleChain()
tri.contour_cl(samples);
../_images/output_4_1.png

Density image

tri = TriangleChain()
tri.density_image(samples);
../_images/output_6_1.png

Scatter plot

tri = TriangleChain()
tri.scatter(samples[:1000]);
../_images/output_8_1.png

Scatter density

# scatter, color corresponds to density
tri = TriangleChain()
tri.scatter_density(samples);
../_images/output_10_1.png

LineChain

The same plotting types can also be done in a line.

line = LineChain()
line.contour_cl(samples);
../_images/output_13_0.png

Samples with probability

To plot a sample where the probability of the sample is given, the prob argument can be used (for all contour_cl, density_image and scatter_density)

n_dims = 3
n_samples = 1000000

# Initalize grid
sample = np.random.uniform(-5, 5, size=(n_samples, n_dims))

# loglikelihood
def loglike(x, mean, covmat):
    return -0.5 * np.dot(np.dot((x - mean).T, np.linalg.inv(covmat)), (x - mean))

# Generate the covariance matrix
covmat = np.random.normal(size=(n_dims, n_dims))
covmat = np.identity(n_dims)

# Generate the mean vector
mean = np.zeros(n_dims)

# Compute the probability for each generated sample
prob = np.zeros(n_samples)
for i in range(n_samples):
    prob[i] = loglike(sample[i], mean, covmat)

# Transform and normalize to probabilites
prob = np.exp(prob)
prob /= sum(prob)
tri = TriangleChain(names=["a", "b", "c"])
tri.contour_cl(sample, prob=prob);
../_images/output_17_1.png