Skip to main content

A simple API for defining sample spaces (to run simple statistical simulations)

Project description

…is a very lightweight Python API for simulating sample spaces, events, random variables, and conditional probabilities.

Why?

Writing this helped me think about random variables as functions mapping from events in a sample space to points in R^n, and about what it means to condition. It’s also useful for checking answers / visualizing statistics problems!

Usage

First, define a subclass of sample_space.Experiment that responds to rerun(). Re-run should perform some random experiment and set instance variables to its results. You can also define functions of those results (such as composing them to form more complex events, or computing values of random variables).

Then, initialize a sample_space.SampleSpace with an instance of an Experiment. You can now query the sample space for the probabilities of your experiment’s events, using the names of experiment variables, methods, or even arbitrary functions of them you pass along in an array. You can make this query conditional, too.

Finally, you can also use SampleSpace to generate a sample of the distribution of a random variable of the experiment, and plot a histogram.

This library also exposes a few basic functions (Bern(p), Bin(n,p), RandomSign(p), and Categ(categories, weights)) to assist with defining experiments.

Example

For a concrete example, check out the iPython notebook example (if you’re reading this on Github), or read the following:

import sample_space
import matplotlib.pyplot as plt

class NCoinTosses(sample_space.Experiment):
    def __init__(self, n, p):
        self.n = n
        self.p = p

    def rerun(self):
        self.tosses = [sample_space.Bern(self.p) for _ in range(self.n)]

    def heads(self):
        return sum(self.tosses)

    def there_are_at_least_two_heads(self):
        return self.heads() >= 2

    def first_toss_heads(self):
        return self.tosses[0]

space = sample_space.SampleSpace(NCoinTosses(10, 0.5), iters=20000)

# ask for probability of any truthy method
print(space.probability_that('there_are_at_least_two_heads'))

# alias for the above, if it's more grammatical
print(space.probability_of('first_toss_heads'))

# change the number of iterations
print(space.probability_of('first_toss_heads', iters=1000))

# ask for probabilities of functions of random variables
def gt(x): return lambda y: y > x
print(space.probability_that(['heads', gt(5)]))

# ask for conditional probabilities
print(space.probability_that(['heads', gt(5)], given=['first_toss_heads']))
print(space.probability_of('first_toss_heads', given=[['heads', gt(5)]]))
print(space.probability_that(['heads', gt(5)],
    given=['first_toss_heads', 'there_are_at_least_two_heads']))

# plot distribution histograms
space.plot_distribution_of('heads')

# plot conditional distribution histograms (with kwargs)
space.plot_distribution_of('heads', given=['first_toss_heads'], bins=10)

License

MIT

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

sample_space-0.0.1.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file sample_space-0.0.1.tar.gz.

File metadata

File hashes

Hashes for sample_space-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c02a1251a8480e070fd9a38aed94e932752fe006cf638a0a6f1c0e3170a37928
MD5 a4dbffba2107815e7d44dae4fb4f5378
BLAKE2b-256 6466984c7034c08a3a1180356e62d9b3a857a6d61047727aee5f8e8bd107cf64

See more details on using hashes here.

Supported by

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