# Probability in R: Discrete Random Variables

## Bernoulli distribution

### Part 1

R commands and functions:
• # - comments out code
• runif(n) - generates n draws from standard uniform distribution
• round(x) - rounds x to the nearest integer
• sum(v) - sums elements of vector v
• mean(v) - averages elements of vector v
• v [ i : j ] - looks at elements from vector v indexed from i to j

### Part 2

In this video we generate a Bernoulli random variables using R. We familiarize ourselves with boolean variables and logical conditions in R.
R commands and functions:
• a < b - returns TRUE if a is less than b and FALSE otherwise
• rbinom(n,1,p) - generates n draws from bernoulli distribution distribution with parameter p

## Binomial distribution

### Part 1

We define binomial distribution and generate it using R. We familiarize ourselves with histogram in R.
R commands and functions:
• hist(v) - represents data from vector v in a histogram
• rbinom(n,m,p) - generates n draws from binomial distribution distribution with parameters m and p

### Part 2

In this episode we derive the probability of each outcome in binomial distribution. We show the probabilities add up to 1 using set theory, polynomials and Pascal's triangle.
Mathematical formulas:

### Part 3

In this video we are comparing the probability distribution of binomial random variables to simulation in R. We learn how to generate vectors and matrices, use the for loop and bar plot in R.
R commands and functions:
• 1:n - generates a vector of n integers
• matrix(nrow=n,ncol=m) - generates empty matrix of n rows and m columns
• for(i in v){do} - repeats do command for i taking all the values from vector v

## Geometric distribution

### Part 1

We define geometric distribution and draw random variables from it in R. We familiarize ourselves with while loop and scientific notation.
R commands and functions:
• while(bool){do} - repeats do command while bool logical condition is TRUE
• rgeom(n,p) - generates n draws from geometric distribution with parameter p
• 1+e6 - short notation for 10 to power 6

### Part 2

We derive probability distribution of the geometric random variables and learn about geometric series. We encounter cumulative distribution function
Mathematical formulas:

### Part 3

We want to increase to infinity the maximum value coming from geometric distribution. On our way we encounter NAs and machine epsilon but don't give up.
R commands and functions:
• max(v) - returns maximal element in data set v
• na.rm=TRUE - optional function argument making it ignore NA values
• .Machine\$Double.eps - returns lowest in absolute value number of type double

## Borel-Cantelli lemma

### Part 1

We create a function in R returning a result of Bernoulli trial and we use it in for loop to generate series of trials. We try to understand what does it mean for a series of events to happen infinitely many times.
R commands and functions:
• f=function(x){return(x*x)} - function syntax in R, f(3) returns square of 3
• print(x) - prints value of x in console
• flush.console() - forces output display
• Sys.sleep(x) - holds execution for x sec

### Part 2

We derive probability distribution of the Borel-Cantelli random variables and learn about Borel-Cantelli series. We encounter cumulative distribution function
Mathematical formulas:

### Part 3

We prove the second part of Borel-Cantelli lemma. We come across exponential function, complementary events, a monkey and William Shakespeare.
Borel - Cantelli lemma part 2:

### Part 4

In the last episode of discrete random variables we use Borel-Cantelli lemma to generate infinite series of successful Bernoulli trials. As we approach infinity we turn to philosophy and music.
R commands and functions:
• .Machine\$integer.max - returns maximal number od type integer
• sqrt(x) - square root of x
• if(bool){do} - executes do command if bool logical condition is TRUE