Simple uncertainty calculations.
Project description
SimpleFermi
A simple uncertainty and unit aware programming environment. Think of it as (frink or rink) + (squiggle or MonteCarloMeasurements), a less visual version of Guesstimate with units support. It's a realization of the GUM Supplement on the Monte Carlo Method.
It's a programming environment that lets you work easily with both units and a sense of uncertainty.
Installation
pip install git+https://github.com/alexalemi/simplefermi.git
Afterwards the library can be used from the command line with:
python -m simplefermi
of you can launch an ipython {console, qtconsole, notebook} session and use it like a library with the recommended abbreviation:
import simplefermi as sf
Example Usage
Let's try to estimate how much the carbon dioxide in the atmosphere is rising due to human activity. First let's estimate the total number of moles of gas in the atmosphere, we'll estimate that by starting with the mass of the atmosphere:
atm_mass = (sf.sigfig('1.0', 'atm') / (sf.gravity * sf.percent(7)) * 4 * sf.pi * sf.earth_radius**2).to_base_units()
Then we'll estimate the molar mass of the air by using our knowledge that its ~70% nitrogen and the rest oxygen and nitogren is 28 g/mol and oxygen is 32 g/mol
f = sf.outof(70, 100)
atm_molarmass = f * sf.sigfig('28', 'g/mol') + (1-f) * sf.sigfig('32', 'g/mol')
atm_mol = atm_mass / atm_molarmass
Now let's estimate how much carbon dixode is released into the atmosphere each year. I remember that globally we use ~18 TW of energy, and that ~80% of that comes from fossil fuels, and that fossil fuels are like fats and so have ~9 kcal/g of energy, and that for every gram of fossil fuels ~3 g of carbon dioxide is released, and carbon dioxide has a molecular mass of 32+12
co2_mol = sf.Q(18, 'TW') * sf.outof(80, 100) / (sf.db() * sf.Q(9, 'kcal/g')) * (sf.db() * 3) / (sf.sigfig('12', 'g/mol') + sf.sigfig('32', 'g/mol'))
This let's us estimate how much carbon dixode is added to the atmosphere each year:
(co2_mol / atm_mol).to('ppm/year')
Currently the concentration of CO2 in the atmosphere is growing at about 2.37 ppm/year.
Core library
The basic datatype in simplefermi is a Quantity which represents both some value (potentially with uncertainty) as well as its units.
Quantity aka Q generates quantities. For example:
Quantity(1, 'm/s')represents 1 meter per second.Q(1, 'm')represents 1 meter.Q(32, 'years')Q(2, 'm') / Q(3, 's') == Q(2/3, 'm/s')basic arithmetic works as expected.
Distributions
plusminus(mean, std)- represents a number like 3 +/- 2, with a given mean and standard deviation.normal(left, right)- represents a normal distribution between the given limits.timesdivide(mean, std)- represents a number and some typical multiplicative variance.lognormal(low, high)- represents a log normal distribution between the two limits, use this for most positive physical quantities.to(low, high)- for both arguments being positive, itslognormal, elsenormal.
For percentages, there are some helpers:
outof(part, whole)- generates a beta distribution with the observed counts, 7 out of 10 and 70 out of 100 have the same mean but different concentrations.against(part, other)- different phrasing, its often easier to estimate two opposing population sizes than it is to estimate fractions.
You can also twiddle existing numbers:
sigfig(string)- will generate a uniform distribution consistent with the given value, interpreting it in terms of sigfigs, i.e.sigfig('1.0')is between 0.95 and 1.05 whilesigfig(1.)is between 0.5 and 1.5.percent(percentage)- will introduce some percentage error in an estimate (multiplicatively), if you know how things vary.db(decibels)- introduces a multiplicative variation in terms of the decibels of relative error.
And you can use data to formulate distributions:
data(values, weights=None)- does bootstrap sampling from a finite set of values, use this for empirical measurements, optional weights give the weighting values.mixture(*sources, weights=None)- generates a mixture distribution with the given weights.normalfit(values)- fits a normal distribution to the given values, prefer to usedata, but this has its uses.
Library of Constants
All of the CODATA18 physical constants are implemented with their measured errors, so you can use hbar, stefan_boltzmann_constant, c etc and they will reflect mankinds current accepted experimental uncertainty, though note that a lot of physical constants will become exact once CODATA2022 is finalized due to the 2019 redefinition of the SI base units.
Notebooks
If you want nice quantile dotplots, I recommend using either jupyter qtconsole or juypter notebook which
should automatically represent quantities with quantile dotplots.
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 simplefermi-0.1.1.tar.gz.
File metadata
- Download URL: simplefermi-0.1.1.tar.gz
- Upload date:
- Size: 219.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3737f27e0ae960ba5a99956e072417c95ceeae96950403aee7e53e1f4217b02b
|
|
| MD5 |
18c414417e8bc5fa3b4b0437b185e501
|
|
| BLAKE2b-256 |
db11883cc7ea0cc439394fb8de236906a1cc7967b5d193148343ff5e70691b55
|
File details
Details for the file simplefermi-0.1.1-py3-none-any.whl.
File metadata
- Download URL: simplefermi-0.1.1-py3-none-any.whl
- Upload date:
- Size: 33.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e93d90836994fa94f344bda61240c2f7427c74003e6cb2014f5b934179ba9ef
|
|
| MD5 |
a77edc6d4fd5ee3ff902e595e06ff7b6
|
|
| BLAKE2b-256 |
0d02f6cb1c5002b0fcfb8d6372000679c03fbc229d131f81a86e1538dcd3a0de
|