Skip to content

RobTillaart/Poisson

Repository files navigation

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

Poisson

Arduino library for Poisson distribution math.

Description

Experimental

Poisson is an experimental Arduino library to calculate the probability of a Poisson distributed variable.

The Poisson distribution calculates the probability of a given number of events occurring in a fixed interval of time or space, provided these events happen at a known constant rate and independently of one another.

There are six core functions

  • P(X == k)
  • P(X <= k) a.k.a. CDF
  • P(X < k)
  • P(X >= k)
  • P(X > k)
  • P(k1 <= X < k2) === P(X <= k2 - 1) - P(X <= k1)

The library is written for a small experiment, so the library is rather minimal. Might be useful for educational purposes etc.

Feedback as always is welcome.

Application example

TODO

History

See - https://en.wikipedia.org/wiki/Poisson_distribution#History

Accuracy / precision

The version 0.1.0 uses doubles for internal storage. But on many platforms doubles == 4 byte float. This means precision is at most 6-7 digits.

As the formula uses faculty (k!) the chances drop to zero fast when the parameter k grows. Also this faculty causes math overflow quite fast in the integer domain and accuracy loss in the float domain.

Therefore an iterative implementation is used for most functions. These are reasonably fast and can handle a wider range of the parameter k. This parameter is now an uint8_t as for now it is large enough. Values of k above 50 are not tested.

In short there is room for improvement. (feedback welcome).

Performance

The functions are optimized as the reference versions were quite slow. See Poisson_performance.ino (has some logs).

Character

parameter name ALT-code char notes
mean mu ALT-230 µ
lambda unknown.
plus minus ALT-0177 ±

Related

Backgrounders

Related libs

Interface

#include Poisson.h

Constructor

Note: mu is also known as lambda.

  • Poisson(double mu = 1) constructor, default mu == 1.
  • void begin(double mu) allows runtime change of mu (mean/average).

Typical use of begin() is if one has an array of events and calculates the average of those events.

double avg = average(array, length);
P.begin(avg);

P chance

Core functions implemented.

  • double equal(uint8_t k) P(X == k)
  • double smallerEqual(uint8_t k) P(X <= k)
  • double smaller(uint8_t k) P(X < k)
  • double largerEqual(uint8_t k) P(X >= k)
  • double larger(uint8_t k) P(X > k)
  • double between(uint8_t k1, uint8_t k2) P(k1 <= X < k2)
  • double outside(uint8_t k1, uint8_t k2) P(X < k1) + P(X >= k2)

Alternative names to be used.

  • double PMF(uint8_t k) <==> equal(k)
  • double CDF(uint8_t k) <==> smallerEqual(k)

Meta info

See - https://en.wikipedia.org/wiki/Poisson_distribution

  • double average() idem.
  • double variance() idem.
  • double stdev() idem. = standard deviation.
  • double median() idem.
  • double mode() idem.
  • double skewness() idem.
  • double kurtosis() idem.

Add

The add function allows to runtime adjust the mu / lambda parameter. This allows to start with an estimation for mu and adjust its value while new events come in.

  • double add(double value, double alpha = 0.05) adjusts the average mu with value with the given weight alpha. This weight alpha must be between 0 and 1. The higher alpha the more mu will change. Think of it as percentage change.

It is possible to add() a data set in a loop to determine mu.

Future

Must

  • improve documentation

Should

  • add examples

Could

  • add unit tests
  • optimize code(more possible?)
  • inverse CDF - search.
    • Smallest k for which P(X <= k) >= N (0..1);

Won't (unless requested)

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,

About

Arduino library for Poisson distribution math.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages