Skip to main content

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()

mass of atmosphere

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

moles in the atmosphere

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'))

co2 moles added a year

This let's us estimate how much carbon dixode is added to the atmosphere each year:

(co2_mol / atm_mol).to('ppm/year')

co2 concentration added each 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, its lognormal, else normal.

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 while sigfig(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 use data, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplefermi-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simplefermi-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file simplefermi-0.1.0.tar.gz.

File metadata

  • Download URL: simplefermi-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for simplefermi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 93d1ef7d096fe88a3a736d8584245b91091c638d27f5b32330e5233098efd79c
MD5 617a195f6850cb7ae74d3cfc91f87395
BLAKE2b-256 3e54039c88484df59106fb32bf860013580698632e5c1cce2358693a7a3966e1

See more details on using hashes here.

File details

Details for the file simplefermi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: simplefermi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for simplefermi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 226d825b1786bab44f32e329f3905381552a3291ac33c45c1e7559cda7156682
MD5 33ca4e0916b4a0013d02720c374144b5
BLAKE2b-256 5aa5ab3da4c2544e4be1a8a17aa4e7e359e33d8f5db04ee29277a4f2ff3be533

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page