Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evamaerey/tidybernoulli
probability branching in data frames
https://github.com/evamaerey/tidybernoulli
matrix probability tidy-data
Last synced: about 1 month ago
JSON representation
probability branching in data frames
- Host: GitHub
- URL: https://github.com/evamaerey/tidybernoulli
- Owner: EvaMaeRey
- License: other
- Created: 2023-05-09T20:46:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-18T19:26:38.000Z (over 1 year ago)
- Last Synced: 2024-10-27T13:09:37.946Z (3 months ago)
- Topics: matrix, probability, tidy-data
- Language: R
- Homepage: https://evamaerey.github.io/tidybernoulli/
- Size: 657 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# tidybernoulli
The goal of tidybernoulli creates a framework work with independent, repeated trials in an intuitive, fluid, and computation-friendly way.
A Bernoulli trial is an independent trial with two outcomes (usually success and a failure), where probabilities associated with each new trial are independent of previous trials.
Instead of looking at the realization of single trials and trials histories, we look at probability distributions that are generated by adding Bernoulli trials. Data frames that are generated contain a row for each outcome history and two columns for each trial index -- one column with the trial outcome and one column with the associated probability.
Once all the outcome-probability pathways have been built up, summary functions allow us to ask questions about global outcomes; e.g. how likely are we to observe at least one success in 5 fair coin flips. Students will be able to see distributions like the binomial distribution emerge from first principles.
tidybernoulli was inspired by and is complementary to [ma206distributions](https://evamaerey.github.io/ma206distributions/) which treats common discrete probability distributions consistent with data requirements for use in with ggplot2 and the rest of the tidyverse.
## Installation
You can install the development version of tidybernoulli from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("EvaMaeRey/tidybernoulli")
```Then load the package.
```{r}
library(tidybernoulli)
```# Single trials
We provide some single Bernoulli trial functions
```{r}
bernoulli_trial()
weighted_coin()
fair_coin()
```As well as few non-bernoulli events and probabilities
```{r}
prize_wheel()
```# Multiple trials
This is a basic example which shows you how to solve a common problem:
```{r example}
## basic example code
``````{r cars}
bernoulli_trial()trial_init() |>
add_trials()trial_init() |>
add_trials() |>
add_trials()
```# Summarizing possible outcome histories
```{r}
library(magrittr)
trial_init(prob = .3) %>%
add_trials() %>%
add_trials() %>%
.$out %>%
sum_across() %>%
prod_across()
``````{r}
library(magrittr)
bernoulli_trial(prob = .5) %>%
trial_init() %>%
add_trials() %>%
add_trials() %>%
add_trials(5) %>%
.$out %>%
sum_across() %>%
prod_across()
```# Further summary based on outcome of interest...
```{r}
library(dplyr)
bernoulli_trial(prob = .5) %>%
add_trials() %>%
add_trials() %>%
add_trials() %>%
add_trials(3) %>%
.$out %>%
sum_across() %>%
prod_across() %>%
group_by(global_outcome) %>%
summarize(probs = sum(global_probs))```
# Cross-validate work
```{r}
dbinom(x = 0:7, size = 7, prob = .5)```
---
or...
```{r}
bernoulli_trial(prob = .5) |>
add_trials() |>
add_trials() |>
to_tsibble() |>
group_by(history) |>
summarize(hist_prob = prod(prob),
count_successes = sum(outcome),
paths = paste(outcome, collapse = ",")) |>
arrange(count_successes) |>
group_by(count_successes) |>
summarize(count_prob = sum(hist_prob))```
---
# drob quick job on veridical paradox
> A #tidyverse simulation to demonstrate that if you wait for two heads in a row, it takes 6 flips on average, while you wait for a heads then a tails, it takes 4 flips on average
```{r}
library(tidyverse)# drob
crossing(trial = 1:1000,
flip = 1:100) %>%
mutate(heads = rbinom(n(), 1, .5)) %>%
group_by(trial) %>%
mutate(next_flip = lead(heads),
hh = heads & next_flip,
ht = heads & !next_flip) %>%
summarise(first_hh = which(hh)[1] + 1,
first_ht = which(ht)[1] + 1) %>%
summarise(first_hh = mean(first_hh),
first_ht = mean(first_ht))```
It's about the second chances...
```{r}
options(pillar.print_max = Inf)
fair_coin(outcome_set = c("T", "H")) %>%
select(-prob) %>%
trial_init() %>%
add_trials() %>%
add_trials() %>%
add_trials() %>%
add_trials() %>%
add_trials() %>%
to_tsibble() %>%
group_by(history) %>%
ggplot() +
aes(y = history, x = trial) +
geom_tile(color = "white") +
aes(fill = outcome) ->
baseplot; baseplotbaseplot +
geom_point(data = . %>% filter( outcome == "H" & lag(outcome) == "H"), color = "darkred")baseplot +
geom_point(data = . %>% filter( outcome == "T" & lag(outcome) == "H"), color = "darkred")
```## 16 dolphin trials
```{r}
bernoulli_trial(prob = .5) |>
add_trials() |>
add_trials() |>
to_tsibble() |>
group_by(history) |>
summarize(hist_prob = prod(prob),
count_successes = sum(outcome),
paths = paste(outcome, collapse = ",")) |>
arrange(count_successes) |>
group_by(count_successes) |>
summarize(prob = sum(hist_prob))options(scipen = 10)
bernoulli_trial(prob = .5) |>
add_trials(15) |>
to_tsibble() |>
group_by(history) |>
summarize(hist_prob = prod(prob),
count_successes = sum(outcome),
paths = paste(outcome, collapse = ",")) |>
arrange(count_successes) |>
group_by(count_successes) |>
summarize(prob = sum(hist_prob))collapse <- function(x, collapse = ", "){
paste(x, collapse = collapse)
}bernoulli_trial(prob = .5, outcome_set = c("nope", "fish")) |>
add_trials(15) |>
to_tsibble() %>%
group_by(history) %>%
summarise(history = collapse(outcome),
sum_successes = sum(outcome == "fish"),
prob = prod(prob)) %>%
group_by(sum_successes) %>%
summarise(prob = sum(prob))
```## Generalizing and simplifying to binomial equation...
```{r}
ma206equations::typeset_eq_binomial()
ma206equations::typeset_eq_choose()
```${{_N}C{_k}} \cdot p^kq^{N-k}$
where
$C = n!\(r!*(n-r)!)$
## Quick viz...
```{r}
ma206distributions::tidy_dbinom(num_trials = 16, single_trial_prob = .5) %>%
ggplot() +
aes(x = num_successes,
y = probability) +
ma206distributions::geom_lollipop(annotate = TRUE) +
labs(title = "prob distributions of successes if random") +
ma206equations::stamp_eq_binomial(x = 3, y = .1, size = 8)
```# Peek into internals of tidybernoulli
```{r}
readLines("R/bernoulli-trial.R")[150:200]
```