This is a demonstration of basic trianglechain functionalities.
from trianglechain import TriangleChain
import numpy as np
color1 = "#BA1A13"
color2 = "#34BA09"
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)}
tri = TriangleChain(ranges=ranges)
tri.contour_cl(samples1, color=color1);
tri.contour_cl(samples2, color=color2);
tri = TriangleChain(ranges=ranges)
tri.density_image(samples1, cmap="inferno");
tri.contour_cl(samples1, color='skyblue');
tri = TriangleChain(ranges=ranges)
tri.scatter_density(samples1, cmap="inferno");
tri = TriangleChain(ranges=ranges)
tri.scatter(samples1, color=color1);
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.
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);
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);
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);