| Title: | Probability Spaces with the Same Syntax in R and Python |
|---|---|
| Description: | Write probability statements such as 'Pr[X == 0] <- 0.25' and read them back with 'Pr[X == 0]'. Events can declare their support (real, integer, count, or a finite set), and anything the rules of probability then pin down, such as complements and totals, is derived. Assignments that contradict earlier ones are flagged with the range of values that would have been consistent. A companion Python package on PyPI offers the same syntax. |
| Authors: | Ivan Gutierrez [aut, cre] |
| Maintainer: | Ivan Gutierrez <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-09-27 01:41:24 UTC |
| Source: | https://github.com/ivanharvard/prspace |
An Event is a quantity you make probability statements about.
Comparing it with a single value
(==, !=, <, <=, >, >=) gives a condition
such as S_B == 0, which is what you index a PrSpace with.
!(X == 0) is the complement X != 0.
The support says which values the event can take, which lets a PrSpace derive and
check more. For example, with support = "count", S_B >= 1 is the complement of
S_B == 0 and Pr[S_B < 0] is 0.
If name is omitted, it is taken from the variable the event is bound to
the first time it is compared, so S_B <- Event() prints as S_B.
Event(name = NULL, support = NULL)Event(name = NULL, support = NULL)
name |
Optional label used when printing, a single string. |
support |
|
A prspace_event object.
X <- Event() X == 0 !(X < 3) Event("S_B", support = "count") Event("Coin", support = c("H", "T"))X <- Event() X == 0 !(X < 3) Event("S_B", support = "count") Event("Coin", support = c("H", "T"))
A PrSpace stores probabilities of conditions built from Events.
Assign with Pr[X == 0] <- p (or =) and read back with Pr[X == 0].
Remove a value with Pr[X == 0] <- NULL.
Reading a condition returns, in order: the value assigned to it or to any condition describing
the same event; one minus the value assigned to its complement; 0 or 1 if the event's support
makes it impossible or certain; and, with derive = TRUE, any value the rules of probability
pin down given everything assigned to that event. Otherwise it signals an error of class
prspace_undefined whose fields lower and upper give the range the value is
known to lie in.
Assigning a condition replaces any stored value for the same event or its complement.
With check on, the new value is compared with the range consistent with the other stored
values, and a violation signals a condition of class prspace_inconsistent.
A PrSpace has reference semantics: modifying it inside a function modifies the caller's copy.
PrSpace(check = "warn", derive = TRUE)PrSpace(check = "warn", derive = TRUE)
check |
What to do when an assignment is inconsistent with the others:
|
derive |
If |
A prspace_space object.
Pr <- PrSpace() S_B <- Event(support = "count") n_samples <- choose(19, 10) for (B in c(100, 1000)) { Pr[S_B == 0] <- prod(1 - (0:(B - 1) / n_samples)) cat("B =", B, " Pr[S_B >= 1] =", Pr[S_B >= 1], "\n") } Pr X <- Event(support = c(1, 2, 3)) Pr[X == 1] <- 0.2 Pr[X == 2] <- 0.3 Pr[X == 3] Y <- Event() Pr[Y < 3] <- 0.6 tryCatch(Pr[Y < 5] <- 0.4, warning = function(w) conditionMessage(w))Pr <- PrSpace() S_B <- Event(support = "count") n_samples <- choose(19, 10) for (B in c(100, 1000)) { Pr[S_B == 0] <- prod(1 - (0:(B - 1) / n_samples)) cat("B =", B, " Pr[S_B >= 1] =", Pr[S_B >= 1], "\n") } Pr X <- Event(support = c(1, 2, 3)) Pr[X == 1] <- 0.2 Pr[X == 2] <- 0.3 Pr[X == 3] Y <- Event() Pr[Y < 3] <- 0.6 tryCatch(Pr[Y < 5] <- 0.4, warning = function(w) conditionMessage(w))