MVUncertainParameterArray

MVUncertainParameterArray(*uparrays, cor[, ...])

Structured array of multiple parameter means and variances along with correlations.

Methods

MVUncertainParameterArray.cov([stdzd, whiten])

Covariance matrix (only supported for 0-D MVUParrays)

MVUncertainParameterArray.get(name[, default])

Return one component parameter as an UncertainParameterArray or a subset as an MVUncertainParameterArray

MVUncertainParameterArray.mahalanobis(parray)

Calculates the Mahalanobis distance between the MVUParray distribution and a (ParameterArray) point.

MVUncertainParameterArray.mvuparray(*args, ...)

Create an MVUncertainParameterArray using this instance's Standardizer

MVUncertainParameterArray.outlier_pval(parray)

Calculates the p-value that a given (ParameterArray) point is an outlier from the MVUParray distribution.

MVUncertainParameterArray.parray(*args, **kwargs)

Create a ParameterArray using this instance's Standardizer

MVUncertainParameterArray.uparray(*args, ...)

Create an UncertainParameterArray using this instance's Standardizer

Attributes

MVUncertainParameterArray.dist

Scipy multivariate_normal() object (only supported for 0-D MVUParrays)

MVUncertainParameterArray.t

Transformed values.

MVUncertainParameterArray.z

Standardized values.

MVUncertainParameterArray.μ

Means

MVUncertainParameterArray.σ

Standard deviations

MVUncertainParameterArray.σ2

Marginal variances

class gumbi.arrays.MVUncertainParameterArray(*uparrays, cor, stdzr=None)

Bases: ndarray

Structured array of multiple parameter means and variances along with correlations.

This class is essentially a combination of the ParameterArray and UncertainParameterArray classes.

See also

multivariate_normal: scipy Multivariate Normal random variable

ParameterArray

UncertainParameterArray

Parameters:
  • *uparrays (UncertainParameterArray) – UParrays of identical shape containing the marginal mean and variance of each parameter

  • stdzr (Standardizer) – An instance of Standardizer

  • stdzd (bool, default False) – Whether the supplied values are on standardized scale instead of the natural scale

Examples

Create an MVUParray from two UParrays with negative correlation

>>> import numpy as np
>>> from gumbi import uparray, mvuparray, Standardizer
>>>
>>> stdzr = Standardizer(m = {'μ': -5.30, 'σ': 0.582}, r = {'μ': -0.307, 'σ': 0.158}, log_vars=['d', 'c'])
>>> m_upa = uparray('c', np.arange(1,5)/10, np.arange(1,5)/100, stdzr)
>>> r_upa = uparray('d', np.arange(1,5)/10+0.5, np.arange(1,5)/100*2, stdzr)
>>> cor = np.array([[1, -0.6], [-0.6, 1]])
>>> cor
array([[ 1. , -0.6],
       [-0.6,  1. ]])
>>> mvup = mvuparray(m_upa, r_upa, cor=cor)

The MVUParray is displayed as an array of tuples ((μ_a, μ_b), (σ2_a, σ2_b))

>>> mvup
('c', 'd')['μ', 'σ2']: [((0.1, 0.6), (0.01, 0.02)) ((0.2, 0.7), (0.02, 0.04))
                        ((0.3, 0.8), (0.03, 0.06)) ((0.4, 0.9), (0.04, 0.08))]

The marginal means or variances can be extracted as ParameterArrays:

>>> mvup.μ
('c', 'd'): [(0.1, 0.6) (0.2, 0.7) (0.3, 0.8) (0.4, 0.9)]

The component UncertainParameterArrays can be extracted with the get() method:

>>> mvup.get('d')
r['μ', 'σ2']: [(0.6, 0.02) (0.7, 0.04) (0.8, 0.06) (0.9, 0.08)]

Slicing and indexing works as normal:

>>> mvup[::2]
('c', 'd'): [((0.1, 0.6), (0.01, 0.02)) ((0.3, 0.8), (0.03, 0.06))]

Transformed and standardized distributions can be obtained as with UncertainParameterArray

>>> mvup.t
('m_t', 'r_t')['μ', 'σ2']: [((-2.30258509, -0.51082562), (0.01, 0.02))
                            ((-1.60943791, -0.35667494), (0.02, 0.04))
                            ((-1.2039728 , -0.22314355), (0.03, 0.06))
                            ((-0.91629073, -0.10536052), (0.04, 0.08))]
>>> mvup.z
('m_z', 'r_z')['μ', 'σ2']: [((5.15019743, -1.29003559), (0.02952256, 0.80115366))
                            ((6.34117197, -0.31439838), (0.05904512, 1.60230732))
                            ((7.03784742,  0.53073702), (0.08856768, 2.40346098))
                            ((7.53214651,  1.27619927), (0.11809024, 3.20461465))]

For 0-d MVUParrays (or individual elements of larger arrays), the dist() exposes the scipy multivariate_normal object. Because this distribution is defined in standardized space (by default, stdzd=True) or transformed spaced (stdzd=False), ParameterArrays may be the most convenient way to pass arguments to the distribution methods:

>>> pa = mvup.parray(m=0.09, d=0.61)
>>> mvup[0].dist().cdf(pa.z.values())
0.023900979112885523
>>> mvup[0].dist(stdzd=False).cdf(pa.t.values())
0.023900979112885523

Perhaps most importantly, the rvs() allows drawing correlated samples from the joint distribution, returned as a ParameterArray:

>>> mvup[0].dist.rvs(10, random_state=2021)
('c', 'd'): [(0.0962634 , 0.74183363) (0.09627651, 0.56437764)
             (0.09140986, 0.64790721) (0.09816149, 0.70518567)
             (0.10271404, 0.60974628) (0.09288982, 0.60933939)
             (0.0983131 , 0.63588871) (0.12262933, 0.45941758)
             (0.1070759 , 0.4918009 ) (0.11118635, 0.49708401)]
>>> mvup[0].dist.rvs(1, random_state=2021).z
('m_z', 'r_z'): (5.08476443, 0.05297293)

Allowing us to easily visualize the joint distribution:

>>> import pandas as pd
>>> import seaborn as sns
>>> sns.jointplot(x='c', y='d', data=pd.DataFrame(mvup[0].dist.rvs(1000).as_dict()), kind='kde')
cov(stdzd=True, whiten=1e-10)

Covariance matrix (only supported for 0-D MVUParrays)

property dist: MultivariateNormalish

Scipy multivariate_normal() object (only supported for 0-D MVUParrays)

get(name, default=None) UncertainParameterArray | MVUncertainParameterArray

Return one component parameter as an UncertainParameterArray or a subset as an MVUncertainParameterArray

mahalanobis(parray: ParameterArray) float

Calculates the Mahalanobis distance between the MVUParray distribution and a (ParameterArray) point.

mvuparray(*args, **kwargs) MVUncertainParameterArray

Create an MVUncertainParameterArray using this instance’s Standardizer

outlier_pval(parray: ParameterArray) float

Calculates the p-value that a given (ParameterArray) point is an outlier from the MVUParray distribution.

parray(*args, **kwargs) ParameterArray

Create a ParameterArray using this instance’s Standardizer

property t: MVUncertainParameterArray

Transformed values.

Returns a MVUncertainParameterArray with the same Standardizer values as the current instance but with all transforms set to lambda x: x.

uparray(*args, **kwargs) UncertainParameterArray

Create an UncertainParameterArray using this instance’s Standardizer

property z: MVUncertainParameterArray

Standardized values.

Returns a MVUncertainParameterArray with a default Standardizer (mean and SD of all variables set to zero and all transforms set to lambda x: x).

property μ: ParameterArray

Means

property σ: ParameterArray

Standard deviations

property σ2: ParameterArray

Marginal variances