romanimpreprocess.L1_to_L2.GalPoisson.draw_with_tilnus

Functions

draw_from_Pearson(tilnu_21, tilnu_31, tilnu_41, I_arr, *)

Add a Pearson-family random deviate to each element of I,

_to_x(y, scale, shift, sign)

Transforms our distribution:

analytic_u_v_from_betas(beta1, beta2)

Solve for u=a+b and v=(a-b)^2/(ab) from:

ab_from_u_v(u, v, want_negative_skew)

Solves for shape parameters a (alpha) and b (beta):

central_moments_beta(a, b)

Helper function: finds the central moments of the Beta distribution

solve_beta_parameters_vec(tilnu_21, tilnu_31, ...)

Vectorised: returns α,β,mean_x,c arrays with shape == I_arr.

random_from_type1(tilnu_21, tilnu_31, tilnu_41, I_arr)

Generates a draw from a unique shifted/scaled

random_from_type3(tilnu_21, tilnu_31, I_vals[, rng])

Generates a draw from a unique shifted/scaled Gamma distribution

_log_k(m, nu[, a])

Generates log(k).

_log_reg_lower_gamma(→ float)

log P(a,x): regularized lower incomplete gamma, in a stable branch.

_branch_masses(m)

Return (g0, w_left, w_right) for normalized g(s), computed in log-space.

_sample_left(→ numpy.ndarray)

Left branch: s<0 with kernel (1+s^2)^(-m).

_sample_right(→ numpy.ndarray)

Right branch: s>0 with kernel (1+s)^(-2m) * exp( 2m*s/(1+s) ).

sample_g(m[, size, rng])

g0 is normalization const

_s_transform_constants(m, nu, a)

Constants for s↔x mapping and f_S.

_log_fS_from_s(→ float)

log f_S(s) = log f_X(x(s)) + log|dx/ds| under the −ν convention:

_log_g_pdf_scalar(→ float)

log g_S(s) at scalar s (normalized g).

_peak_scale_logc(→ float)

Peak scaling: ensure envelope matches/exceeds PT4 at the mode s=0 (for −ν).

pt4_rvs_devroye(m, nu, *[, a, lam, size, rng])

Generates a random variate from specified Pearson Type 4 using a Devroye sampler.

pt4_rvs_ar(→ float)

ONE draw X ~ Pearson-IV (−ν convention) via AR with proposal g(s).

devroye_acc_rate(nu, a, m)

Analytical approximation of Devroye Pearson Type 4 sampler:

random_from_type4(tilnu21, tilnu31, tilnu41, I_array, *)

Generates a draw from a unique Pearson Type 4 per element of I_arr in an array of shape I_arr.

solve_pearson5_parameters_vec(tilnu_21, tilnu_31, I_arr)

Vectorised version of the analytic solver.

random_from_type5(tilnu_21, tilnu_31, I_arr[, rng])

Generates a unique shifted inverse Gamma draw

solve_pearson6_params(tilnu_21, tilnu_31, tilnu_41, I_arr)

random_from_type6(tilnu_21, tilnu_31, tilnu_41, I_arr)

Generates a draw from a unique shifted/scaled

Module Contents

draw_from_Pearson(tilnu_21: float, tilnu_31: float, tilnu_41: float, I_arr: numpy.ndarray, *, atol: float = 0.0, rng=None)[source]

Add a Pearson-family random deviate to each element of I, chosen according to its β₁ & β₂.

Parameters:
  • tilnu_21 (float) – Scalar central-moment ratios (𝜈̃₂₁, 𝜈̃₃₁, 𝜈̃₄₁).

  • tilnu_31 (float) – Scalar central-moment ratios (𝜈̃₂₁, 𝜈̃₃₁, 𝜈̃₄₁).

  • tilnu_41 (float) – Scalar central-moment ratios (𝜈̃₂₁, 𝜈̃₃₁, 𝜈̃₄₁).

  • I_arr (np.ndarray) – Input intensities. They are modified in place.

  • atol (float, optional) – Equality tolerance for the region boundaries.

  • rng (Generator, optional) – Used for rng seeding

Returns:

draws – random draws from the Pearson family with desired moments

Return type:

np.ndarray

_to_x(y, scale, shift, sign)[source]

Transforms our distribution:

  1. Scales our distribution to have the correct variance

  2. Translates our distribution to draw the correct mean (zero mean)

  3. Changes the sign of the draw to obtain the correct skew

Notice: Scale only transforms “y”. Does not transform “shift”.

analytic_u_v_from_betas(beta1, beta2)[source]
Solve for u=a+b and v=(a-b)^2/(ab) from:

beta1 = 4 v (u+1)/(u+2)^2, beta2 = 3 + 6 [ v(u+1) - (u+2) ] / [(u+2)(u+3)].

ab_from_u_v(u, v, want_negative_skew)[source]
Solves for shape parameters a (alpha) and b (beta):

u = a + b v = (a-b)^2 / (ab)

There are two solutions, (a_+,b_+), (a_-,b_-), which generate opposite skew distributions.

central_moments_beta(a, b)[source]

Helper function: finds the central moments of the Beta distribution given shape parameters a (alpha) and b (beta).

solve_beta_parameters_vec(tilnu_21, tilnu_31, tilnu_41, I_arr)[source]

Vectorised: returns α,β,mean_x,c arrays with shape == I_arr.

random_from_type1(tilnu_21, tilnu_31, tilnu_41, I_arr, rng=None)[source]

Generates a draw from a unique shifted/scaled Beta distribution per element of I_arr in an array of shape I_arr. The probability distribution for every intensity should match the target moments.

random_from_type3(tilnu_21: float, tilnu_31: float, I_vals: numpy.ndarray, rng=None)[source]

Generates a draw from a unique shifted/scaled Gamma distribution per element of I_arr in an array of shape I_arr. The probability distribution for every intensity should match the target moments.

_log_k(m: float, nu: float, a: float = 1.0)[source]

Generates log(k).

2^(2m-2) and Gamma terms blow up very fast.

log(k) instead of “k” necessary to avoid math overflor errors.

_log_reg_lower_gamma(a: float, x: float) float[source]

log P(a,x): regularized lower incomplete gamma, in a stable branch.

_branch_masses(m: float)[source]
Return (g0, w_left, w_right) for normalized g(s), computed in log-space.

g0 is the normalization constant P_left = 0.5 * B(1/2, m-1/2) P_right = exp(2m) * (2m)^(-(2m-1)) * Γ(2m-1) * P(2m-1, 2m)

_sample_left(n: int, m: float, rng: numpy.random.Generator) numpy.ndarray[source]

Left branch: s<0 with kernel (1+s^2)^(-m). Negative half of a scaled Student-t with ν=2m-1.

_sample_right(n: int, m: float, rng: numpy.random.Generator) numpy.ndarray[source]

Right branch: s>0 with kernel (1+s)^(-2m) * exp( 2m*s/(1+s) ). With y=1+s (>1) it’s InvGamma(α=2m-1, β=2m), truncated to y>1. Exact draw via inverse survival function.

sample_g(m: float, size: numpy.typing.ArrayLike = 1, rng: numpy.random.Generator | None = None)[source]

g0 is normalization const Draw samples from normalized g(s). Returns (samples, g0). g(s) = g0*(1+s^2)^(-m) for s<0; g0*(1+s)^(-2m) exp(2ms/(1+s)) for s>0; continuous at 0.

_s_transform_constants(m: float, nu: float, a: float)[source]

Constants for s↔x mapping and f_S.

_log_fS_from_s(s: float, m: float, nu: float, theta: float, root: float, logk: float, log_dxds: float) float[source]
log f_S(s) = log f_X(x(s)) + log|dx/ds| under the −ν convention:

f_X(x) ∝ (1+ξ^2)^(-m) * exp( -ν * atan ξ ), ξ=(x-λ)/a

Note: λ cancels in f_S(s); only m,ν,a matter in s-space.

_log_g_pdf_scalar(s: float, m: float, g0: float) float[source]

log g_S(s) at scalar s (normalized g).

_peak_scale_logc(m: float, nu: float, a: float, g0: float) float[source]

Peak scaling: ensure envelope matches/exceeds PT4 at the mode s=0 (for −ν). Returns logc with c = exp(logc).

pt4_rvs_devroye(m: float, nu: float, *, a: float = 1.0, lam: float = 0.0, size=None, rng=None)[source]

Generates a random variate from specified Pearson Type 4 using a Devroye sampler.

Sampler from Heinrich, A Guide to the Pearson Type IV Distribution (2004), Section 7

pt4_rvs_ar(m: float, nu: float, a: float, lam: float, rng: numpy.random.Generator | None = None) float[source]

ONE draw X ~ Pearson-IV (−ν convention) via AR with proposal g(s). Envelope is c*g(s) with c = exp(logc) chosen by peak-scaling at s=0.

devroye_acc_rate(nu, a, m)[source]

Analytical approximation of Devroye Pearson Type 4 sampler:

random_from_type4(tilnu21: float, tilnu31: float, tilnu41: float, I_array, *, devroye_threshold=0.005, rng=None)[source]

Generates a draw from a unique Pearson Type 4 per element of I_arr in an array of shape I_arr. The probability distribution for every intensity should match the target moments.

If the acceptance rate of the devroye sampler > devroye_threshold, we use the devroye sampler. Otherwise, we use the AR sampler.

solve_pearson5_parameters_vec(tilnu_21, tilnu_31, I_arr)[source]

Vectorised version of the analytic solver.

Parameters:
  • tilnu_21 (floats) – Used to solve for Pearson variables Beta_1 and Beta_2

  • tilnu_31 (floats) – Used to solve for Pearson variables Beta_1 and Beta_2

  • I_arr (np.ndarray) – Used to solve for Pearson variables Beta_1 and Beta_2

Returns:

  • a_arr (np.ndarray) – Pearson type 5 shape parameter

  • b_arr (np.ndarray) – Pearson type 5 shape parameter

  • mu_arr (np.ndarray) – Mean shifts

random_from_type5(tilnu_21, tilnu_31, I_arr, rng=None)[source]

Generates a unique shifted inverse Gamma draw per element of I_arr in an array of shape I_arr. The probability distribution for every intensity should match the target moments.

solve_pearson6_params(tilnu_21, tilnu_31, tilnu_41, I_arr)[source]
Parameters:
  • tilnu_21 (floats) – Tilde-ν coefficients (shared across all intensities).

  • tilnu_31 (floats) – Tilde-ν coefficients (shared across all intensities).

  • tilnu_41 (floats) – Tilde-ν coefficients (shared across all intensities).

  • I_arr (array-like) – Noise-intensity values.

Returns:

  • alpha, beta, scale, shift (ndarrays (shape = I.shape))

  • sign (float (+1 or −1) – skew sign (same for every element))

random_from_type6(tilnu_21, tilnu_31, tilnu_41, I_arr, rng=None)[source]

Generates a draw from a unique shifted/scaled Beta Prime per element of I_arr in an array of shape I_arr. The probability distribution for every intensity should match the target moments.