Tutorial

This is a demonstration of basic trianglechain functionalities.

from trianglechain import TriangleChain
import numpy as np
color1 = "#BA1A13"
color2 = "#34BA09"

Generate some data

First, we generate some data that we want to plot. TriangleChain can plot structured arrays as the sample generated below. But it also takes dictionaries as input.

def get_samples(n_samples=10000, n_dims=4):
    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))
    from trianglechain.TriangleChain import ensure_rec
    samples = ensure_rec(samples, column_prefix='col')
    return samples
n_dims = 4
samples1 = get_samples(n_samples=100000, n_dims=n_dims)
samples2 = get_samples(n_samples=100000, n_dims=n_dims)
ranges = {f'col{i}': [-10,10] for i in range(n_dims)}

Basic contour plot

tri = TriangleChain(ranges=ranges)
tri.contour_cl(samples1, color=color1);
tri.contour_cl(samples2, color=color2);
_images/demo_plot_types_8_2.png

Density image plot

tri = TriangleChain(ranges=ranges)
tri.density_image(samples1, cmap="inferno");
tri.contour_cl(samples1, color='skyblue');
_images/demo_plot_types_10_2.png

Scatter density

tri = TriangleChain(ranges=ranges)
tri.scatter_density(samples1, cmap="inferno");
_images/demo_plot_types_12_1.png

simple scatter

tri = TriangleChain(ranges=ranges)
tri.scatter(samples1, color=color1);
_images/demo_plot_types_14_1.png

Scatter with additional value

This plot type gives the option of making a scatter plot where the color of each dot can be specified by the prob argument. For the colorbar and the 1D posteriors there are different options.

Normalize colorbar and 1D posterior

In this version, the color in the colorbar is normalized to be between 0 and 1. The 1D posterior assumes that the prob corresponds actually to a probability and therefore the 0th parameter shows a smooth increase.

samples = np.random.rand(1000, 5)
prob = (10*samples[:,0]-0.1)**3
tri = TriangleChain(colorbar=True, colorbar_label="normalized prob")
tri.scatter_prob(samples, prob=prob, normalize_prob2D=True, normalize_prob1D=True);
_images/demo_plot_types_19_1.png

Label in colorbar not normalized

In this case, the label of the colorbar is not normalized but shows the actual input numbers. But the 1D posterior stills shows a smooth increase in the 0th parameter. This can e.g. be used if you want to plot the error of a prediction across parameter space. Then the 1D posterior would visualize in which part of the parameter space this error is the largest.

tri = TriangleChain(colorbar=True, colorbar_label="e.g. value of 6th param")
tri.scatter_prob(samples, prob=prob, normalize_prob2D=False, normalize_prob1D=True);
_images/demo_plot_types_22_1.png

No normalization

Neither the colorbar nor the 1D posteriors are normalized. The 1D posterior corresponds to the actual density, the colorbar to the value of the prob argument. This could e.g. be used if you want to visualize an additional parameter in the color without changing the 1D posterior distributions.

tri = TriangleChain(colorbar=True, colorbar_label="e.g. value of 6th param")
tri.scatter_prob(samples, prob=prob, normalize_prob2D=False, normalize_prob1D=False);
_images/demo_plot_types_25_1.png