pystra.distributions.distribution.Distribution#
Use it: User guide · Worked example · Theory
- class Distribution(name='', dist_obj=None, mean=None, std=None, start_point=None)[source]#
Bases:
objectBase class for all probability distributions used in reliability analysis.
Subclasses typically construct a
scipy.statsfrozen distribution object (dist_obj) and pass it to this base class, which then delegatespdf,cdf,ppf, and the Nataf-space transformations (x_to_u,u_to_x,jacobian) to it.Subclasses that do not wrap a SciPy distribution must override the transformation and Jacobian methods directly, or provide the tail functions below (see, e.g.,
Maximum).Tail accuracy. Every distribution provides
cdf,sf,logcdf,logsf,pdf,logpdf,ppfandisf, each accurate in its own tail.u_to_xandx_to_uevaluate the tail that the point lies in, so a probability near one is never formed:x = F^{-1}(Phi(u))foru <= 0andx = Fbar^{-1}(Phi(-u))foru > 0. Beyond the smallest normal double (|u|above about 37.5) the log-probability is inverted instead, in closed form where one exists and otherwise by solving the log-CDF or log-survival function. Jacobians switch to log densities when the densities underflow. Subclasses that overridecdforppfshould overridesforisfas well; the fallbacks1 - cdfandppf(1 - q)lose the upper tail.- Parameters:
name (
str) – Name of the random variable. Must match a keyword argument of the limit-state function.dist_obj (
rv_frozen|None) – A frozen SciPy distribution. When provided,meanandstdare computed from the distribution automatically.mean (
float|None) – Mean of the distribution (required if dist_obj isNone).std (
float|None) – Standard deviation (required if dist_obj isNone).start_point (
float|None) – Starting point for iterative search algorithms (defaults to the mean).
- name#
Name of the random variable.
- Type:
str
- dist_type#
Human-readable label set by each subclass (e.g.
"Normal").- Type:
str
Methods
Cumulative distribution function.
Derivatives of the CDF w.r.t.
Return the random-variable name.
Inverse survival function, accurate for small upper-tail
q.Diagonal Jacobian of the marginal x-to-u transformation.
Logarithm of the CDF, accurate in both tails.
Log density,
-infwhere the density is zero.Logarithm of the survival function, accurate in both tails.
Probability density function.
Plot the probability density function.
Percent-point function (inverse CDF).
Draw random samples from the distribution.
Update the location parameter of the underlying SciPy distribution.
Update the scale parameter of the underlying SciPy distribution.
Survival function
1 - F(x), accurate in the upper tail.Transform from standard normal space to physical space.
Rebuild an independent marginal, replacing constructor parameters.
Transform from physical space to standard normal space.
- property mean: float#
Mean of the distribution.
- property std: float#
Standard deviation of the distribution.
- property start_point: float#
Starting point of the design-point search; the mean by default.
- pdf(x)[source]#
Probability density function.
- Parameters:
x (
ArrayLike) – Value(s) in physical space.- Returns:
Density value(s).
- Return type:
float|ndarray
- cdf(x)[source]#
Cumulative distribution function.
- Parameters:
x (
ArrayLike) – Value(s) in physical space.- Returns:
Probability value(s) in [0, 1].
- Return type:
float|ndarray
- logcdf(x)[source]#
Logarithm of the CDF, accurate in both tails.
Below the median the logarithm of the CDF itself (or SciPy’s
logcdf) is used; above it,log1p(-sf(x))keeps the difference from zero.- Return type:
float|ndarray
- logsf(x)[source]#
Logarithm of the survival function, accurate in both tails.
- Return type:
float|ndarray
- ppf(u)[source]#
Percent-point function (inverse CDF).
- Parameters:
u (
ArrayLike) – Probability value(s) in (0, 1).- Returns:
Quantile(s) in physical space.
- Return type:
float|ndarray
- isf(q)[source]#
Inverse survival function, accurate for small upper-tail
q.- Return type:
float|ndarray
- u_to_x(u)[source]#
Transform from standard normal space to physical space.
Applies the marginal Nataf mapping
x = F^{-1}(Phi(u)). Aboveu = 3it is evaluated asFbar^{-1}(Phi(-u)), so the probability passed on is never rounded towards one; far into either tail the log-probability is used (see_tail_quantiles()).- Parameters:
u (
ArrayLike) – Value(s) in standard normal (u) space.- Returns:
Corresponding value(s) in physical (x) space.
- Return type:
float|ndarray
- x_to_u(x)[source]#
Transform from physical space to standard normal space.
Applies the marginal Nataf mapping
u = Phi^{-1}(F(x)), asu = -Phi^{-1}(Fbar(x))aboveu = 3, with log probabilities where these underflow.- Parameters:
x (
ArrayLike) – Value(s) in physical (x) space.- Returns:
Corresponding value(s) in standard normal (u) space.
- Return type:
float|ndarray
- jacobian(u, x)[source]#
Diagonal Jacobian of the marginal x-to-u transformation.
Returns a diagonal matrix
Jwhere the diagonal entry isf_X(x) / phi(u)(Lemaire, eq. 4.9). This is assembled into the full Jacobian by theTransformationclass. The ratio is formed from log densities where either density underflows.- Parameters:
u (
ArrayLike) – Value(s) in standard normal space.x (
ArrayLike) – Corresponding value(s) in physical space.
- Returns:
Diagonal Jacobian matrix of shape
(n, n)where n is the length of the input arrays.- Return type:
ndarray
- sample(n=1000)[source]#
Draw random samples from the distribution.
Uses inverse-transform sampling via
ppf.- Parameters:
n (
int) – Number of samples (default 1000).- Returns:
Array of shape
(n,)with sampled values.- Return type:
ndarray
- plot(ax=None, **kwargs)[source]#
Plot the probability density function.
- Parameters:
ax (
Axes|None) – Axes to plot on. A new figure is created ifNone.**kwargs (
Any) – Additional keyword arguments forwarded toax.plot().
- Returns:
The axes containing the plot.
- Return type:
Axes
- property sensitivity_params: dict[str, float]#
Distribution parameters for which sensitivities are computed.
Returns a dict
{param_name: current_value}listing every parameter with respect to which \(\partial\beta/\partial\theta\) should be evaluated.The default implementation returns
{"mean": μ, "std": σ}, which is appropriate for most distributions. Distributions with additional parameters of interest (e.g. the GEV shape parameter) should override this property to include them.Fixed constructor settings such as bounds are separate from these sensitivity coordinates. Native reconstruction parameters can be another representation of the same law: sensitivity analysis perturbs the declared coordinates together, keeping other moments fixed.
- Returns:
{param_name: current_value}- Return type:
dict
- property parameters: Mapping[str, object]#
Read-only snapshot of keyword arguments that rebuild this marginal.
type(dist)(**dist.parameters)reproduces its law, name and start point. Built-ins use native parameters where moment fitting could lose precision. Nested marginals and SciPy objects are independent copies, so mutating a snapshot never changes the source distribution. Custom marginals may override this property with their complete constructor mapping. Sensitivity coordinates are specified separately bysensitivity_params.
- with_parameters(**changes)[source]#
Rebuild an independent marginal, replacing constructor parameters.
- Parameters:
**changes (
object) – Replacements for keys ofparameters. Built-ins supporting moment sensitivities also acceptmeanandstd; supplying either switches to moment construction, holding the other moment and fixed bounds/shape constant. Native coordinates and moments cannot be supplied together. The start point remains unchanged unless explicitly replaced (Noneselects the new mean).- Returns:
Independent instance of the same type, including nested marginals. An empty replacement reproduces the distribution.
- Return type:
Self- Raises:
TypeError – A replacement is unknown, or mixes moments and native coordinates.
ModelError, ValueError – Constructor parameters do not define a valid distribution.
- cdf_gradient(x)[source]#
Derivatives of the CDF w.r.t. each sensitivity parameter.
Returns
∂F_X(x)/∂θfor every parameter listed bysensitivity_params. The base-class implementation uses central finite differences on the CDF viawith_parameters().Before computing derivatives, a reconstruction sanity check verifies that
with_parameters()(with no overrides) reproduces the current distribution. This catches both constructor failures (e.g. composite distributions) and silent mismatches (e.g. distributions whose extra constructor arguments are not stored inparameters).Subclasses may override this with analytical expressions for better accuracy and performance (see
NormalandLognormal).- Parameters:
x (
ArrayLike) – Evaluation point in physical space.- Returns:
{param_name: ∂F/∂θ}for each parameter insensitivity_params.- Return type:
dict[str,float|ndarray]- Raises:
ValueError – If the distribution cannot be faithfully reconstructed by
with_parameters().
- set_location(loc=0)[source]#
Update the location parameter of the underlying SciPy distribution.
After updating,
meanandstdare recomputed. This is available for callers that need an in-place parameter update.- Parameters:
loc (
float) – New location parameter (default 0).- Raises:
ModelError – If the distribution does not wrap a SciPy frozen distribution.
- Return type:
None
- set_scale(scale=1)[source]#
Update the scale parameter of the underlying SciPy distribution.
After updating,
meanandstdare recomputed. This is available for callers that need an in-place parameter update.- Parameters:
scale (
float) – New scale parameter (default 1).- Raises:
ModelError – If the distribution does not wrap a SciPy frozen distribution.
- Return type:
None