This is a library for simulate probability theory problems specialy conditional probability

Related tags

Miscellaneouspprobs
Overview

Introduction

This is a library for simulating probability theory problems, especially conditional probability. It is also useful to create a custom single or joint distribution with a specific PMF or PDF to get a probability table and generate data based on a probability function.

How to install?

pip install pprobs

Probability Simulator

It simulates probability theory problems, especially conditional probability.

Example 1

We want to get some information by defining some events.

  • P(A) = 0.3
  • P(B) = 0.2
  • P(A^B) = 0.1
  • A and B are dependent
  • P(A+B) = ? , P(A|B) = ?
from pprobs.simulation import Simulator

space = Simulator()

space.add_event('A', 0.3)
space.add_event('B', 0.2)
space.add_event('A^B', 0.1)

prob_1 = space.get_prob('A+B') # A+B means union of A and B
prob_2 = space.get_prob('A|B')

print(prob_1, prob_2) # 0.4  0.5

Example 2

In a group of 100 sports car buyers, 40 bought alarm systems, 30 purchased bucket seats, and 20 purchased an alarm system and bucket seats. If a car buyer chosen at random bought an alarm system, what is the probability they also bought bucket seats?

By Statisticshowto

  • P(SEAT) = 0.3
  • P(ALARM) = 0.4
  • P(SEAT ^ ALARM) = 0.2
  • P(SEAT | ALARAM) = ?
from pprobs.simulation import Simulator

space = Simulator()

space.add_event('SEAT', 0.3).add_event('ALARM', 0.4) # We can also add events sequentially in a line (chaining) 
space.add_event('SEAT^ALARM', 0.2) # A^B means intersection of A & B

print(space.get_prob('SEAT|ALARM')) # 0.5

Example 3

Totaly 1% of people have a certain genetic defect.90% of tests for the gene detect the defect (true positives). 9.6% of the tests are false positives. If a person gets a positive test result, what are the odds they actually have the genetic defect?

By Statisticshowto

  • P(GEN_DEF) = 0.01
  • P(POSITIVE|GEN_DEF) = 0.9
  • P(POSITIVE|GEN_DEF!) = 0.096
  • P(GEN_DEF|POSITIVE) = ?
space = Simulator()

space.add_event('GEN_DEF', 0.01)
space.add_event('POSITIVE|GEN_DEF', 0.9) # A|B means A given B
space.add_event('POSITIVE|GEN_DEF!', 0.096) # A! means complement of A

print(space.get_prob('GEN_DEF|POSITIVE')) # 0.0865

Example 4

Bob has an important meeting tomorrow and he has to reach the office on time in the morning. His general mode of transport is by car and on a regular day (no car trouble) the probability that he will reach on time is 0.3. The probability that he might have car trouble is 0.2. If the car runs into trouble he will have to take a train and only 2 trains out of the available 10 trains will get him to the office on time.

By Hackerearth

  • P(ON_TIME|CAR_OK) = 0.3
  • P(ON_TIME|CAR_OK!) = 2/10 => Go by train
  • P(CAR_OK!) = 0.2
  • P(ON_TIME) = ?
space = Simulator()

space.add_event('ON_TIME|CAR_OK', 0.3)
space.add_event('ON_TIME|CAR_OK!', 2/10)
space.add_event('CAR_OK!', 0.2)

prob = space.get_prob('ON_TIME') # Probability of ON_TIME

print(prob) # 0.28

Distribution Simulator

It is useful to create a custom single or joint distribution with a specific PMF or PDF to get a probability table and generate data based on a probability function.

Example 1

Suppose that we have a discrete random variable with a specific PMF. We want to generate many data based on this variable. As you see in the second example 1 has the largest probability and duplicates more and 4 has the smallest probability and duplicates less.

from pprobs.distribution import Discrete

# First 
def pmf(x):
    return 1 / 6

dist = Discrete(pmf, [1, 2, 3, 4, 5, 6]) # The second is the sample space of our PMF

print(dist.generate(15)) # [4, 3, 1, 6, 5, 3, 5, 3, 5, 4, 2, 5, 6, 1, 6]


# Second
def pmf(x):
    return 1 / x

dist = Discrete(pmf, [1, 2, 3, 4])
print(dist.generate(15)) # [1, 2, 1, 1, 1, 4, 3, 1, 1, 3, 2, 4, 1, 2, 2]

Example 2

Suppose that we have a continuous random variable with a specific PDF.

from pprobs.distribution import Continuous

def pdf(x):
  if x > 1:
    return x / x ** 2
  return 0

dist = Continuous(pdf, [1, 6]) # The second is the sample interval of our PDF

print(dist.generate(15)) # [2.206896551724138, 4.103448275862069, ..., 5.655172413793104, 6.0]

Example 3

Suppose that we have a Continuous Joint variable with a specific PDF.

from pprobs.distribution import Joint

def pdf(x, y):
  if x > 1:
    return 1 / (x * y)
  return 0

dist = Joint(pdf, [1, 6], [3, 10]) # The second and third are the intervals of our PDF

print(dist.probability_table(force=20)) # if force gets more, many number will generate

Output:

X/Y x=3.0 X=3.7 ... X=10
X=1.0 0.000 0.000 ... 0.000
... ... ... ... ...
X=6.0 0.055 0.044 ... 0.016
print(dist.get_prob(3.5, 3.5)) # 0.081 is P(X=3.5, Y=3.5)
print(dist.get_prob([1, 6], 4)) # 0.041 is P(Y=4) because X includes its whole domain
print(dist.get_prob(2.1, [1, 4])) # 0.206 is P(X=2.1, Y in [1, 4])

Example 4

Suppose that we have a Discrete Joint variable with a specific PMF.

from pprobs.distribution import Joint

def pmf(x, y):
  if x > 1:
    return 1 / (x * y)
  return 0

dist = Joint(pmf, range(1, 6), range(6, 10)) # The second and third are the sample space of our PMF

print(dist.probability_table()) 

Output:

X/Y Y=6 Y=7 Y=8 Y=9
X=1 0.000000 0.000000 0.000000 0.000000
X=2 0.083333 0.071429 0.062500 0.055556
X=3 0.055556 0.047619 0.041667 0.037037
X=4 0.041667 0.035714 0.031250 0.027778
X=5 0.033333 0.028571 0.025000 0.022222
print(dist.get_prob(2, range(6, 10))) # 0.272 is P(X=2)
print(dist.get_prob(2, 6)) # 0.083 is P(X=2, Y=6)

Thank you if giving a star me on Github. https://github.com/mokar2001

Owner
Mohamadreza Kariminejad
I am interested in AI, Backend Development, and Mathematics.
Mohamadreza Kariminejad
Python script for changing the SSH banner content with other content

Banner-changer-py Python script for changing the SSH banner content with other content. The Script will take the content of a specified file range and

2 Nov 23, 2021
A guy with a lot of useful things to do when doing AtCoder in Python

atcoder_python_env Python で AtCoder をやるときに便利な諸々を用意したやつ コンテスト用フォルダの作成 セットアップ 自動テス

2 Dec 28, 2021
An example of Connecting a MySQL Database with Python Code

An example of Connecting a MySQL Database with Python Code And How to install Table of contents General info Technologies Setup General info In this p

Mohammad Hosseinzadeh 1 Nov 23, 2021
Allows you to purge all reply comments left by a user on a YouTube channel or video.

YouTube Spammer Purge Allows you to purge all reply comments left by a user on a YouTube channel or video. Purpose Recently, there has been a massive

4.3k Jan 09, 2023
Larvamatch - Find your larva or punk match.

LarvaMatch Find your larva or punk match. UI TBD API (not started) The API will allow you to specify a punk by token id to find a larva match, and vic

1 Jan 02, 2022
Mommas-cookbook - A Repository About Mom's Recipes

Mommas Cookbook A Repository for Mom's Recipes Contents bacalhau à Gomes de Sá Beef-Rendang bacalhau à Gomes de Sá, recommended by @s0undt3ch One of t

1 Jan 08, 2022
This tool helps you to reverse any regex and gives you the opposite/allowed Letters,numerics and symbols.

Regex-Reverser This tool helps you to reverse any regex and gives you the opposite/allowed Letters,numerics and symbols. Screenshots Usage/Examples py

x19 0 Jun 02, 2022
Minimal, super readable string pattern matching for python.

simplematch Minimal, super readable string pattern matching for python. import simplematch simplematch.match("He* {planet}!", "Hello World!") {"p

Thomas Feldmann 147 Dec 01, 2022
Custom SLURM wrapper scripts to make finding job histories and system resource usage more easily accessible

SLURM Wrappers Executables job-history A simple wrapper for grabbing data for completed and running jobs. nodes-busy Developed for the HPC systems at

Sara 2 Dec 13, 2021
A tool to help plan vacations with friends and family

Vacationer In Development A tool to help plan vacations with friends and family Deployment Requirements: NPM Docker Docker-Compose Deployment Instruct

JK 2 Oct 05, 2021
Perform oocyst segmentation in mercurochrome stained mosquito midgut

Midgut_oocyst_segmentation Perform oocyst segmentation in mercurochrome stained mosquito midguts This oocyst segmentation model also powers the webtoo

Duo Peng 3 Oct 27, 2021
ELF file deserializer and serializer library

elfo ELF file deserializer and serializer library. import elfo elf = elfo.ELF.from_path('main') elf ELF( header=ELFHeader( e_ident=e

Filipe Laíns 3 Aug 23, 2021
Python programming language Test

Exercise You are tasked with creating a data-processing app that pre-processes and enriches the data coming from crawlers, with the following requirem

Monirul Islam Khan 1 Dec 13, 2021
poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions

poetry2nix poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions. It does so by parsing pyproject.t

Nix community projects 405 Dec 29, 2022
Tool to automate the enumeration of a website (CTF)

had4ctf Tool to automate the enumeration of a website (CTF) DISCLAIMER: THE TOOL HAS BEEN DEVELOPED SOLELY FOR EDUCATIONAL PURPOSE ,I WILL NOT BE LIAB

Had 2 Oct 24, 2021
LinkScope allows you to perform online investigations by representing information as discrete pieces of data, called Entities.

LinkScope Client Description This is the repository for the LinkScope Client Online Investigation software. LinkScope allows you to perform online inv

108 Jan 04, 2023
Hotpile: High Order Turing Machine Language Compiler

Hotpile: High Order Turing Machine Language Compiler Build and Run Requirements: Python 3.6+, bison, flex, and GCC installed. Needs to be run under UN

Jiang Weihao 4 Dec 29, 2021
Stop ask your soraka to ult you, just ult yourself

Lollo's auto-ultimate script Are you tired of your low elo friend who can't ult you with soraka when you ask for it? Use Useless Support and just ult

9 Oct 20, 2022
EFB Docker image with efb-telegram-master and efb-wechat-slave

efb-wechat-docker EFB Docker image with efb-telegram-master and efb-wechat-slave Features Container run by non-root user. Support add environment vari

Haukeng 1 Nov 10, 2022
The learning agent learns firstly approaching to the football and then kicking the football to the target position

Football Court This project utilized Pytorch and Tensorflow so that the learning agent learns firstly approaching to the football and then kicking the

1 Nov 19, 2021