{ "info": { "author": "Andrew Ross", "author_email": "andrewslavinross@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "Sample Space\n============\n\n...is a very lightweight Python API for simulating sample spaces,\nevents, random variables, and (conditional) distributions.\n\nExample\n-------\n\nCheck out the `iPython notebook <./example.ipynb>`__ or read the\nfollowing:\n\n.. code:: python\n\n from sample_space import *\n\n class NCoinTosses(Experiment):\n def __init__(self, n, p):\n self.n = n\n self.p = p\n\n def rerun(self):\n self.tosses = [Bern(self.p) for _ in range(self.n)]\n\n def heads(self):\n return sum(self.tosses)\n\n def there_are_at_least_two_heads(self):\n return self.heads() >= 2\n\n def first_toss_heads(self):\n return self.tosses[0]\n\n space = SampleSpace(NCoinTosses(10, 0.5), iters=20000)\n\n # ask for probability of any truthy method\n print(' P(#H>=2):', space.probability_that('there_are_at_least_two_heads'))\n\n # alias for the above, if it's more grammatical\n print(' P(H1):', space.probability_of('first_toss_heads'))\n\n # change the number of iterations\n print(' P(H1), 1K iters:', space.probability_of('first_toss_heads', iters=1000))\n\n # ask for probabilities of functions of random variables\n print(' P(#H>5):', space.probability_that(['heads', is_greater_than(5)]))\n\n # ask for conditional probabilities\n print(' P(#H>5|H1):', space.probability_that(['heads', is_greater_than(5)], given=['first_toss_heads']))\n print(' P(H1|#H>5):', space.probability_of('first_toss_heads', given=[['heads', is_greater_than(5)]]))\n print(' P(#H>5|H1,H>=2):', space.probability_that(['heads', is_greater_than(5)],\n given=['first_toss_heads', 'there_are_at_least_two_heads']))\n\n # ask for expectations, variances, and moments, conditionally or absolutely\n print(' E(#H):', space.expected_value_of('heads'))\n print(' E(#H|H1):', space.expected_value_of('heads', given=['first_toss_heads']))\n print(' Var(#H):', space.variance_of('heads'))\n print(' Var(#H|H1):', space.variance_of('heads', given=['first_toss_heads']))\n print('1st moment of #H:', space.nth_moment_of('heads', 1))\n print('2nd moment of #H:', space.nth_moment_of('heads', 2))\n print('3rd moment of #H:', space.nth_moment_of('heads', 3))\n print('4th moment of #H:', space.nth_moment_of('heads', 4))\n print(' Skewness of #H:', space.nth_moment_of('heads', 3, central=True, normalized=True), '(using nth_moment_of w/ central=True, normalized=True)')\n print(' Skewness of #H:', space.skewness_of('heads'), '(using skewness_of)')\n print(' Kurtosis of #H:', space.kurtosis_of('heads'))\n\n # some plots\n fig = plt.figure(figsize=(14,3))\n\n # plot distribution histograms\n fig.add_subplot(121)\n space.plot_distribution_of('heads') # pass kwargs\n plt.legend()\n\n # plot conditional distribution histograms\n fig.add_subplot(122)\n space.plot_distribution_of('heads', given=['first_toss_heads'], bins=10) # can pass kwargs\n plt.legend()\n plt.show()\n\nWhich should output (plus some plots):\n\n::\n\n P(#H>=2): 0.98975\n P(H1): 0.502\n P(H1), 1K iters: 0.48\n P(#H>5): 0.37665\n P(#H>5|H1): 0.5076294006183305\n P(H1|#H>5): 0.6580109757729888\n P(#H>5|H1,H>=2): 0.49361831442463533\n E(#H): 4.9983\n E(#H|H1): 5.48924623116\n Var(#H): 2.4486457975\n Var(#H|H1): 2.31806506582\n 1st moment of #H: 4.99245\n 2nd moment of #H: 27.5097\n 3rd moment of #H: 163.13055\n 4th moment of #H: 1015.54155\n Skewness of #H: -0.00454435802967 (using nth_moment_of w/ central=True, normalized=True)\n Skewness of #H: 0.00414054522343 (using skewness_of)\n Kurtosis of #H: 2.78225928171\n\nWhy?\n----\n\nMostly to avoid bugs / reduce boilerplate in statistical simulations for\nsanity-checking homework solutions. But also to get a better\nunderstanding of probability theory.\n\n`Sample spaces `__ are a\ncore concept in probability theory. They encapsulate the idea of\nrepeatedly running an experiment with random results. Almost every\nimportant statistical quantity -- the probability of an event, or any\nmoment of a random variable -- is always defined relative to a sample\nspace. So if you're trying to program meaningful simulations (and if\nyou're more concerned with expressiveness than performance), you might\nas well organize your code by explicitly defining one.\n\nInstallation / Usage\n--------------------\n\nFirst run\n\n::\n\n pip install sample_space\n\nand ``import`` the library. Then define a subclass of ``Experiment``\nthat responds to ``rerun(self)``. ``rerun`` should perform a random\nexperiment and store one or more basic results as instance variables. If\nyou want to define more complex events or random variables, you can\nexpress them as instance methods.\n\nThen, initialize a ``SampleSpace`` with an instance of your\n``Experiment``. You can query your sample space for the\n``probability_that``/``probability_of`` an event, or you can query it\nfor the ``distribution_of``, ``expected_value_of``, ``variance_of``,\n``skewness_of``, ``kurtosis_of``, or ``nth_moment_of`` of random\nvariable (which can also just be an event, in which case it will be\ninterpreted as an indicator). Finally, for any of these methods, you can\npass a ``given`` keyword argument with a list of events, which will make\nany results you obtain conditional on all of those events occurring.\nBehind the scenes, ``SampleSpace`` will just ``rerun`` your experiment\n10000 times and average your random variable or count how often an event\noccurs (conditionally). You can pass an ``iters`` keyword argument to\nany method or to ``SampleSpace.__init__`` to increase the number of\niterations.\n\nTo reference events or random variable, pass the string name of an\ninstance variable or instance method of your experiment, or pass an\narray with a variable/method name and a lambda function. For example:\n\n.. code:: python\n\n space = SampleSpace(CoinTossExperiment(10))\n space.probability_that('first_toss_is_heads')\n space.probability_that(['n_heads', lambda h: h > 5])\n space.expected_value_of('n_heads')\n space.expected_value_of('n_heads', given=['first_toss_is_heads'])\n space.probability_that('first_toss_is_heads, given=[['n_heads', lambda h: h > 3], 'last_toss_is_heads'])\n\nAdditionally, ``sample_space`` defines a few helpful lambda-returning\nmethods (``is_greater_than(x)``, ``is_less_than(x)``,\n``is_at_least(x)``, ``is_at_most(x)``, ``equals(x)``) for convenience.\nOf course, you could also define instance methods on your ``Experiment``\nto accomplish the same goal.\n\nThe library also exposes a few basic sampling functions (``Bern(p)``,\n``Bin(n,p)``, ``RandomSign(p)``, and ``Categ(categories, weights)``) to\nassist with defining experiments.\n\nLite Version\n------------\n\nIf you'd prefer not to define a full ``Experiment`` class, you can also\njust define a random event / random variable function that returns\neither a boolean or a number, and call\n``probability_that``/``expected_value_of``:\n\n.. code:: python\n\n import sample_space as ss\n\n def weighted_coin_flip_is_heads(p=0.4):\n return ss.Bern(p)\n\n def n_weighted_heads(n=100, p=0.4):\n return sum(weighted_coin_flip_is_heads(p) for _ in range(n))\n\n print(ss.probability_that(weighted_coin_flip_is_heads))\n print(ss.probability_that(lambda: weighted_coin_flip_is_heads(0.5))\n print(ss.expected_value_of(n_weighted_heads))\n print(ss.expected_value_of(lambda: n_weighted_heads(200, 0.3)))\n\nLicense\n-------\n\n`MIT `__", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/asross/sample_space", "keywords": null, "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "sample_space", "package_url": "https://pypi.org/project/sample_space/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/sample_space/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/asross/sample_space" }, "release_url": "https://pypi.org/project/sample_space/0.2.0/", "requires_dist": null, "requires_python": null, "summary": "A simple API for defining sample spaces (to run simple statistical simulations)", "version": "0.2.0" }, "last_serial": 2429023, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "a4dbffba2107815e7d44dae4fb4f5378", "sha256": "c02a1251a8480e070fd9a38aed94e932752fe006cf638a0a6f1c0e3170a37928" }, "downloads": -1, "filename": "sample_space-0.0.1.tar.gz", "has_sig": false, "md5_digest": "a4dbffba2107815e7d44dae4fb4f5378", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3154, "upload_time": "2016-09-30T00:43:27", "url": "https://files.pythonhosted.org/packages/64/66/984c7034c08a3a1180356e62d9b3a857a6d61047727aee5f8e8bd107cf64/sample_space-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "e53ec0a6050aaccfb13993803f981162", "sha256": "c4b65d54eac715de7773afb24476956b86e116efc01717a3615387dccc081ac9" }, "downloads": -1, "filename": "sample_space-0.0.2.tar.gz", "has_sig": false, "md5_digest": "e53ec0a6050aaccfb13993803f981162", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3849, "upload_time": "2016-10-07T14:33:27", "url": "https://files.pythonhosted.org/packages/69/36/58d333a7378ce64ec1e331a75ffb1a3e5c425bf7ebc105d50c0405f58cc2/sample_space-0.0.2.tar.gz" } ], "0.1.0": [ { "comment_text": "", "digests": { "md5": "2113f8ff2f2e7a5976c4203cc0ebdcc9", "sha256": "ffba0ab253065695aa9f21873132866cb9d11effb012073d977662af6753e266" }, "downloads": -1, "filename": "sample_space-0.1.0.tar.gz", "has_sig": false, "md5_digest": "2113f8ff2f2e7a5976c4203cc0ebdcc9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4514, "upload_time": "2016-10-19T14:53:53", "url": "https://files.pythonhosted.org/packages/ed/5b/f7f05f009eaec193afc743b8abca1b64b67a5a6f07a0df3a993916447ca6/sample_space-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "c6e40bbca72e85234c6c855788941146", "sha256": "0471c6fcf5c1f0b5abe764ea651382d159278e7b7eef8a21fccaca527643578e" }, "downloads": -1, "filename": "sample_space-0.2.0.tar.gz", "has_sig": false, "md5_digest": "c6e40bbca72e85234c6c855788941146", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5515, "upload_time": "2016-10-28T15:57:57", "url": "https://files.pythonhosted.org/packages/19/1b/e464c47a65f6b7abe1029a4083c97fadbb995ceac4a2ba565b681a6eff92/sample_space-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c6e40bbca72e85234c6c855788941146", "sha256": "0471c6fcf5c1f0b5abe764ea651382d159278e7b7eef8a21fccaca527643578e" }, "downloads": -1, "filename": "sample_space-0.2.0.tar.gz", "has_sig": false, "md5_digest": "c6e40bbca72e85234c6c855788941146", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5515, "upload_time": "2016-10-28T15:57:57", "url": "https://files.pythonhosted.org/packages/19/1b/e464c47a65f6b7abe1029a4083c97fadbb995ceac4a2ba565b681a6eff92/sample_space-0.2.0.tar.gz" } ] }