Tutorial: Parameters

This tutorial shows which formats can be used in trianglechain

import numpy as np
import pandas as pd
from trianglechain import TriangleChain
from trianglechain.params import add_derived, ensure_rec
def get_samples(n_samples=100000, n_dims=2):
    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()

Plotting rec arrays

# TriangleChain works best with rec arrays
# If you want to transform your ndarray/dict/pandas.df in a rec_array, you can do
samples_rec = ensure_rec(samples, names=["a", "b"])
# for numpy arrays, you can specify the names, for dicts/pandas they are automatically generated
tri = TriangleChain()
tri.contour_cl(samples_rec);
../_images/output_5_1.png

Plotting numpy arrays

# Plotting a numpy array of shape (n_samples, n_dim) can be done like this
# The parameters are labeled as numbers
tri = TriangleChain()
tri.contour_cl(samples);
../_images/output_7_11.png
# You can add names to parameters
tri = TriangleChain()
tri.contour_cl(samples, names=["a", "b"]);
../_images/output_8_11.png
# Or add a column prefix
samples_prefix = ensure_rec(samples, column_prefix="col_")
tri = TriangleChain()
tri.contour_cl(samples_prefix);
../_images/output_9_1.png

Plotting a dictionary

samples_dict = {}
samples_dict["a"] = samples[:, 0]
samples_dict["b"] = samples[:, 1]

# dictionaries can also be passed directly to trianglechain
tri = TriangleChain()
tri.contour_cl(samples_dict);
../_images/output_11_1.png

Plotting a pandas dataframe

df = pd.DataFrame({"a": samples[:, 0], "b": samples[:, 1]})

# dictionaries can also be passed directly to trianglechain
tri = TriangleChain()
tri.contour_cl(df);
../_images/output_13_1.png

Add a derived parameter

# Adding a derived parameters can again be done for all possibles types
# If using a rec array/dict/pd.df, the names argument is not needed
samples_c = add_derived(
    samples,
    new_param="c",
    derived=(samples[:, 0] - 3) * (samples[:, 1]),
    names=["a", "b"],
)

tri = TriangleChain()
tri.contour_cl(samples_c);
../_images/output_15_1.png