Second Order Error Propagation for Python
Project description
soerp Second Order Error Propagation for Python
Overview
soerp is the Python implementation of the original Fortran code SOERP by N. D. Cox to apply a second-order analysis to error propagation (or uncertainty analysis). The soerp package allows you to easily and transparently track the effects of uncertainty through mathematical calculations. Advanced mathematical functions, similar to those in the standard math module can also be evaluated directly.
In order to correctly use soerp, the first eight statistical moments of the underlying distribution are required. These are the mean, variance, and then the standardized third through eighth moments. These can be input manually in the form of an array, but they can also be conveniently generated using either the nice constructors or directly by using the distributions from the scipy.stats sub-module. See the examples below for usage examples of both input methods. The result of all calculations generates a mean, variance, and standardized skewness and kurtosis coefficients.
Basic examples
Let's begin by importing all the available constructors:
>>> from soerp import * # uv, normal, uniform, exponential, etc.
Now, we can see that there are several equivalent ways to specify a statistical distribution, say a Normal distribution with a mean value of 10 and a standard deviation of 1:
- Manually input the first 8 moments (mean, variance, and 3rd-8th standardized central moments):
>>> x = uv([10, 1, 0, 3, 0, 15, 0, 105])
- Use the
rvkwarg to input a distribution from thescipy.statsmodule:
>>> x = uv(rv=ss.norm(loc=10, scale=1))
- Use a built-in convenience constructor (typically the easiest if you can):
>>> x = normal(10, 1)
A Simple Example
Now let's walk through an example of a three-part assembly stack-up:
>>> x1 = normal(24, 1) # normally distributed
>>> x2 = normal(37, 4) # normally distributed
>>> x3 = exponential(2) # exponentially distributed
>>> Z = (x1*x2**2)/(15*(1.5 + x3))
We can now see the results of the calculations in two ways:
- The usual
printstatement (or simply the object if in a terminal):
>>> Z # "print" is optional at the command-line
uv(1176.45, 99699.6822917, 0.708013052944, 6.16324345127)
- The
describeclass method that explains briefly what the values are:
>>> Z.describe()
SOERP Uncertain Value:
> Mean................... 1176.45
> Variance............... 99699.6822917
> Skewness Coefficient... 0.708013052944
> Kurtosis Coefficient... 6.16324345127
Distribution Moments
The eight moments of any input variable (and four of any output variable) can be accessed using the moments class method, as in:
>>> x1.moments()
[24.0, 1.0, 0.0, 3.0000000000000053, 0.0, 15.000000000000004, 0.0, 105.0]
>>> Z.moments()
[1176.45, 99699.6822917, 0.708013052944, 6.16324345127]
Correlations
Statistical correlations are correctly handled, even after calculations have taken place:
>>> x1 - x1
0.0
>>> square = x1**2
>>> square - x1*x1
0.0
Derivatives
Derivatives with respect to original variables are calculated and are accessed using the intuitive class methods:
>>> Z.d(x1) # dZ/dx1
45.63333333333333
>>> Z.d2(x2) # d^2Z/dx2^2
1.6
>>> Z.d2c(x1, x3) # d^2Z/dx1dx3 (order doesn't matter)
-22.816666666666666
When we need multiple derivatives at a time, we can use the gradient and hessian class methods:
>>> Z.gradient([x1, x2, x3])
[45.63333333333333, 59.199999999999996, -547.6]
>>> Z.hessian([x1, x2, x3])
[[0.0, 2.466666666666667, -22.816666666666666], [2.466666666666667, 1.6, -29.6], [-22.816666666666666, -29.6, 547.6]]
Error Components/Variance Contributions
Another useful feature is available through the error_components class method that has various ways of representing the first- and second-order variance components:
>>> Z.error_components(pprint=True)
COMPOSITE VARIABLE ERROR COMPONENTS
uv(37.0, 16.0, 0.0, 3.0) = 58202.9155556 or 58.378236%
uv(24.0, 1.0, 0.0, 3.0) = 2196.15170139 or 2.202767%
uv(0.5, 0.25, 2.0, 9.0) = -35665.8249653 or 35.773258%
Advanced Example
Here's a slightly more advanced example, estimating the statistical properties of volumetric gas flow through an orifice meter:
>>> from soerp import normal, umath # sin, exp, sqrt, etc.
>>> H = normal(64, 0.5)
>>> M = normal(16, 0.1)
>>> P = normal(361, 2)
>>> t = normal(165, 0.5)
>>> C = 38.4
>>> Q = C*umath.sqrt((520*H*P)/(M*(t + 460)))
>>> Q.describe()
SOERP Uncertain Value:
> Mean................... 1330.99973939
> Variance............... 58.210762839
> Skewness Coefficient... 0.0109422068056
> Kurtosis Coefficient... 3.00032693502
This seems to indicate that even though there are products, divisions, and the usage of sqrt, the result resembles a normal distribution (i.e., Q ~ N(1331, 7.63), where the standard deviation = sqrt(58.2) = 7.63).
Main Features
-
Transparent calculations with derivatives automatically calculated. No or little modification to existing code required.
-
Basic
NumPysupport without modification. -
Nearly all standard math module functions supported through the
soerp.umathsub-module. If you think a function is in there, it probably is. -
Nearly all derivatives calculated analytically.
-
Easy continuous distribution constructors:
normal(mu, sigma)orN(mu, sigma): Normal distributionuniform(a, b)orU(a, b): Uniform distributionexponential(lamda, [mu])orExp(lamda, [mu]): Exponential distributiongamma(k, theta)orGamma(k, theta): Gamma distributionbeta(alpha, beta, [a, b])orBeta(alpha, beta, [a, b]): Beta distributionlog_normal(mu, sigma)orLogN(mu, sigma): Log-normal distributionchi_squared(k)orChi2(k): Chi-squared distributionf_distribution(d1, d2)orF(d1, d2): F-distributiontriangular(a, b, c)orTri(a, b, c): Triangular distributionstudent_t(v)orT(v): T-distributionweibull(lamda, k)orWeib(lamda, k): Weibull distribution
The location, scale, and shape parameters follow the notation in the respective Wikipedia articles. Discrete distributions are not recommended for use at this time. If you need discrete distributions, try the mcerp python package instead.
Installation
You have several easy, convenient options to install the soerp package.
pip
pip install soerp
To install with plotting support:
pip install soerp[plot]
To install all optional dependencies:
pip install soerp[all]
uv
uv add soerp
uv sync
Or in an existing uv environment:
uv pip install soerp
git
To install the latest version from git:
pip install --upgrade "git+https://github.com/eggzec/soerp.git#egg=soerp"
Requirements
- Python >=3.10
- NumPy : Numeric Python
- SciPy : Scientific Python (the nice distribution constructors require this)
- Matplotlib : Python plotting library (optional)
See Also
- uncertainties : First-order error propagation.
- mcerp : Real-time latin-hypercube sampling-based Monte Carlo error propagation.
Acknowledgements
The author wishes to thank Eric O. LEBIGOT who first developed the uncertainties python package (for first-order error propagation), from which many inspiring ideas (like maintaining object correlations, etc.) are re-used and/or have been slightly evolved. If you don't need second order functionality, his package is an excellent alternative since it is optimized for first-order uncertainty analysis.
References
- N.D. Cox, 1979, Tolerance Analysis by Computer, Journal of Quality Technology, Vol. 11, No. 2, pp. 80-87
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file soerp-0.9.7.tar.gz.
File metadata
- Download URL: soerp-0.9.7.tar.gz
- Upload date:
- Size: 145.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a4d5baeb2abd7b28d8a7a51c98e608265f62a78a1a51e2e3b84de888990454d
|
|
| MD5 |
0a8f316e85e9d02703f7e3e5d617a216
|
|
| BLAKE2b-256 |
ff15afc545693810991a43c2cb528dd86c261bb661583007ec63d853c1bfdf8b
|
Provenance
The following attestation bundles were made for soerp-0.9.7.tar.gz:
Publisher:
publish_dist.yml on eggzec/soerp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
soerp-0.9.7.tar.gz -
Subject digest:
5a4d5baeb2abd7b28d8a7a51c98e608265f62a78a1a51e2e3b84de888990454d - Sigstore transparency entry: 1338085839
- Sigstore integration time:
-
Permalink:
eggzec/soerp@e3a3b2edeab747d3436fe22149792a9b1d215c6f -
Branch / Tag:
refs/tags/v0.9.7 - Owner: https://github.com/eggzec
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_dist.yml@e3a3b2edeab747d3436fe22149792a9b1d215c6f -
Trigger Event:
release
-
Statement type:
File details
Details for the file soerp-0.9.7-py3-none-any.whl.
File metadata
- Download URL: soerp-0.9.7-py3-none-any.whl
- Upload date:
- Size: 30.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4e79556e2f45ff2091ebcbbbc8b39d56b31fc02e5735f08f41edb78e71668d7
|
|
| MD5 |
0edaf8ffe21fb28eaa0cd34606d60c49
|
|
| BLAKE2b-256 |
1b7af1d5b6cc688182f6a5b326028a17784c0f6dd052823ac4ba130a4fe8ebbc
|
Provenance
The following attestation bundles were made for soerp-0.9.7-py3-none-any.whl:
Publisher:
publish_dist.yml on eggzec/soerp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
soerp-0.9.7-py3-none-any.whl -
Subject digest:
f4e79556e2f45ff2091ebcbbbc8b39d56b31fc02e5735f08f41edb78e71668d7 - Sigstore transparency entry: 1338085920
- Sigstore integration time:
-
Permalink:
eggzec/soerp@e3a3b2edeab747d3436fe22149792a9b1d215c6f -
Branch / Tag:
refs/tags/v0.9.7 - Owner: https://github.com/eggzec
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_dist.yml@e3a3b2edeab747d3436fe22149792a9b1d215c6f -
Trigger Event:
release
-
Statement type: