Skip to content

Anamorphosis🍋

Gaussian anamorphosis: Gaussian scores of a sample and their Hermite expansion.

Gaussian scores

By default (GaussScoreMethod.RANK), the i-th smallest of n valid values gets the score norm.ppf((i - 0.5) / n), the Hazen plotting position used by the GSLIB nscore program. Tied values share their average rank, hence their score. NaN values are ignored and keep a NaN score.

With declustering weights wiw_i, e.g. from cell_declustering, each value gets the middle of its step of the weighted cumulative distribution, as in nscore: norm.ppf((W_before + w_i / 2) / W), where W_before is the total weight of the smaller values and W the total weight. Tied values share the middle of their joint step. Without weights, or with equal weights, these are the rank scores above.

Tails

Rank scores stay within ±norm.ppf(1 - 0.5 / n): ±2.58 for 100 values, ±3.29 for 1,000, ±3.89 for 10,000 and ±4.89 for 1,000,000. Beyond the extreme scores the anamorphosis is constant: the empirical transforms return the data minimum and maximum, and the Hermite coefficients are fitted to a piece-wise linear anamorphosis extended by these constant values. Anamorphosis.theoretical_inverse_transform searches Gaussian values within ±5.2. With weights, the extreme scores are ±norm.ppf(1 - w / (2 W)), where w is the weight of the extreme value: a declustered value with a small weight gets a more extreme score.

GaussScoreMethod.QUANTILE_TRANSFORM is the default of GeoLime 2.x and earlier, the scikit-learn quantile transform with at most 1000 quantiles. It always maps the data minimum and maximum to ±5.2 whatever the sample size, interpolates the scores of the most extreme 0.1% of the data between the last two quantiles, and only uses a random subsample of 100,000 values on larger datasets, values beyond the subsample range sharing the score ±5.2. On skewed data this biases the variance of the Hermite expansion, hence the change of support coefficient and the uniform conditioning results. For lognormal samples with a log standard deviation of 1, it is 3% to 27% too low with 1,000 values, 15% to 65% too high with 100,000 values and 30% to 300% too high with 1,000,000 values, and the error grows with skewness. Rank scores keep it within 4% of the data variance from 1,000 values on.

Anamorphosis 🍋

Anamorphosis class.

Allows to transform skew data into standardize data. Transformation is bijective in the interval defined by the data.

Parameters:

Name Type Description Default
data ndarray

Numpy array containing the data.

required
gauss_scores ndarray | None

Option to use the Gaussian scores of the sample as an input directly. By default None. If None, the Gaussian scores will be obtained with gauss_score. If the Gaussian scores are given as an input, they must contain one score per element of data, in the same order, and increase with the data. Distinct values should get distinct scores: values sharing a score are replaced by their mean, with a warning when they differ by more than floating-point noise. If data contains NaN, the scores may match either data (scores at NaN positions are dropped with them) or only its valid values. Their order is not verified by this function. The default gauss_score scores meet these conditions.

None
gauss_score_method GaussScoreMethod

Method used to compute the Gaussian scores when gauss_scores is None, see gauss_score. By default GaussScoreMethod.RANK. Pass GaussScoreMethod.QUANTILE_TRANSFORM to reproduce the anamorphosis of GeoLime 2.x.

RANK
weights ndarray | None

Declustering weights used to compute the Gaussian scores when gauss_scores is None, see gauss_score. They may match either data (weights of NaN values are ignored) or only its valid values. By default None, all values weigh the same.

None

Attributes:

Name Type Description
data Vector

Data in real space.

gauss_scores Vector

Data in gaussian space.

hermite_coefficients ndarray

Coefficients of hermite polynomials used to transform data from real to gaussian.

data property 🍋

Return real data.

Returns:

Type Description
Vector

Array of data.

gauss_scores property 🍋

Return gaussian score of data.

Returns:

Type Description
Vector

Gaussian scores.

hermite_coefficients property 🍋

Return coefficients of Hermite Polynomials transform function.

Returns:

Type Description
ndarray

Coefficients of Hermite polynomials.

empirical_inverse_transform(x) 🍋

Compute the gaussian inverse anamorphosis of the input array using a linear interpolation of the gaussian scores.

Parameters:

Name Type Description Default
x ndarray

Input array containing values in reel space.

required

Returns:

Type Description
ndarray

Transformed array = gaussian values.

empirical_transform(x) 🍋

Compute the gaussian anamorphosis of the input array using a linear interpolation of the gaussian scores.

Parameters:

Name Type Description Default
x ndarray

Input array containing values in gaussian space.

required

Returns:

Type Description
ndarray

Transformed array = real values.

theoretical_inverse_transform(x, r=1.0) 🍋

Compute the gaussian inverse anamorphosis of the input array using an interpolation with Hermite polynomials of the gaussian scores. Obtained values are approximates from results of transform gausian values.

Parameters:

Name Type Description Default
x ndarray

Input array containing values in reel space.

required
r float

Change of support coefficient.

1.0

Returns:

Type Description
ndarray

Transformed array = gaussian values.

theoretical_transform(x, r=1.0) 🍋

Compute the gaussian anamorphosis of the input array using an interpolation with Hermite polynomials of the gaussian scores.

Parameters:

Name Type Description Default
x ndarray

Input array containing values in gaussian space.

required
r float

Change of support coefficient.

1.0

Returns:

Type Description
ndarray

Transformed array = real values.

gauss_score(z, method=GaussScoreMethod.RANK, weights=None) 🍋

Function to obtain the (standard) Gaussian scores of a sample.

Parameters:

Name Type Description Default
z Vector

numpy array containing the data.

required
method GaussScoreMethod

How the scores are computed. By default GaussScoreMethod.RANK, norm.ppf((rank - 0.5) / n) from the ranks of the n valid values, tied values sharing their average rank. GaussScoreMethod.QUANTILE_TRANSFORM is the scikit-learn quantile transform used by default up to GeoLime 2.x.

RANK
weights Vector | None

Declustering weights, one per element of z, e.g. from cell_declustering. Only their relative values matter. By default None, all values weigh the same. Weights of NaN values are ignored. Only available with GaussScoreMethod.RANK.

None

Returns:

Type Description
ndarray

A numpy array that contains the Gaussian scores.

Raises:

Type Description
ValueError
  • No valid data in the given Vector.
  • Weights are given with GaussScoreMethod.QUANTILE_TRANSFORM.
  • Weights do not contain one weight per data value.
  • Weights of valid values are not all positive and finite.

NaN values

NaN values are filtered out before performing the gaussian score computation.

Tails

Rank scores stay within ±norm.ppf(1 - 0.5 / n), e.g. ±3.29 for 1,000 values. The quantile transform always maps the data minimum and maximum to ±5.2 and compresses the scores of the most extreme 0.1% of the data, which biases the anamorphosis of skewed data. See the module documentation above for details.

Changed in 3.0.0

Gaussian scores are rank-based by default. Pass method=GaussScoreMethod.QUANTILE_TRANSFORM to reproduce the scores of GeoLime 2.x.