Probability expression library
Project description
Probnode
Description
Probability expression library
Features
- Basic events and probability expressions modeling, similar to SymPy
- Calculation on a chain of probability expressions
- Contract and/or expand probability expressions chain, using probability mathematical characteristics
Installation
pip install probnode
Quick Usage
- Events and probability modeling:
from probnode import Event, P
e1 = Event("Sample event 1")
p1 = P(e1)
e2 = Event("Sample event 2")
p2 = P(e2)
p3 = P(p1 | p2) # Or Probability expression
p4 = P(p1 & p2) # And Probability expression
p5 = P(p1 // p2) # Conditional Probability expression
# Display mathematical representation of the object(s)
print(repr(p1))
- Mathematical treatment on probability expressions:
# First convert probability expression to math node
from probnode import N
n1 = N(p1) # Each below is a (math) node
n2 = N(p2)
n3 = N(p3)
n4 = N(p4)
n5 = N(p5)
# A sum of nodes
snode = n1 + n2 - n4
# A product of nodes
pnode = n5 * n2
-
Contract/ expand mathematical chain of probability nodes
- Contract
from probnode.computation import contract c1 = contract(snode) # P(A) + P(B) - P(A and B) -> P(A or B) print(repr(c1)) c2 = contract(pnode) print(repr(c2)) # P(A when B) * P(B) -> P(A and B)
- Expand
from probnode.computation import expand x = expand(n3) print(repr(x[0])) # P(A or B) -> P(A) + P(B) - P(A and B)
-
Value calculations
node.value
will return the value of nodeIf
node
is a chain node (comprising multiple nodes in sum / product mathematic operation), and either childrennode.value
is not defined, the result parentnode.value
will beNone
n1.value # None
Node value can be calculated from probability expression value
p1.value = 0.7 n1.value # equals 0.7
Or by assigning value to itself
n1.value = 0.6 n1.value # equals 0.6
The
value
assigned to itself takes precedence over the calculatedvalue
from its probability expression, or calculated from children nodes (if it is a chain node)
Notes
-
Event does not have value. Only probability of event
P(Event("Raining")).value = 0.3
can be assigned value -
There are normal events, and
SureEvent
. Probability ofSureEvent
is always 1, and assigning value to probability ofSureEvent
will raise error -
Nodes, probabilities, events comparisons:
-
Events will be compared based on its name only
-
Probability compared based on its representation (type, value)
-
Nodes compared based on its underlying structure (its probability expressions, children nodes (respecting order))
-
node.is_permutation_of(other)
will check if 2 nodes are permutations. For example:N(P(event1)) + N(P(event2)) - N(P(event3))
is permutation ofN(P(event2)) - N(P(event3)) + N(P(event1))
-
-
Expansion returns a list of expanded nodes
-
Contraction return a single contracted node
-
Expansion and contraction will be done 1 time, not exhaustively until unexpandable / uncontractable
Details
Events
-
Normal events
from probnode import Event
-
Sure events
from probnode import SureEvent
Probability
-
Simple probability expression: Probability of a single event
P(Event("e1")
-
And probability expression, Or probability expression: Probability expression of 2 probability expressions (Thus, there is only a And probability of 2 probability of events, not And probability of 2 events.
❌
P(Event("e1") & Event("e2"))
,✅ use
P(P(Event("e1")) & P(Event("e2")))
instead ) -
Conditional probability expression: Probability of X when Y
P(p1 // p2)
(p1
andp2
are probability expressions)from probnode.probability import SimpleProbabilityExpression, AndProbabilityExpression, OrProbabilityExpression, ConditionalProbabilityExpression
Node
-
Normal node (Pure node): Node of a single probability expression
from probnode.core import Node
-
Chain node: a sum or product of nodes
✅ use
n1 + n2 - n3
to createSumNode
instead of invoke directlySumNode()
(n1
,n2
,n3
can be either normal nodes or chain nodes)✅ use
n1 * n2 / n3
to createProductNode
instead of invoke directlyProductNode()
(n1
,n2
,n3
can be either normal nodes or chain nodes)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file probnode-0.1.2.tar.gz
.
File metadata
- Download URL: probnode-0.1.2.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0865b83146cd55e0afeb953f96c150b4fca07b66da256138202d5670a125535f |
|
MD5 | 873c32c04be9bc2e5dfcf2175976d5d1 |
|
BLAKE2b-256 | ad2c0250598e68a0c1e093c01bd21ce89dd1d0febb082c1657a81f5ea819956f |
File details
Details for the file probnode-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: probnode-0.1.2-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67ca12068f621f92bf3b617aa6d1602322f9b09516e8681cd3947288eddb61f3 |
|
MD5 | 97809d5b5b7109fb68296e7c70224161 |
|
BLAKE2b-256 | 8c0a83e89bb22b8457a163648d3a0e97c7c705288a3d18023bb49f508e1ef60f |