<jupyter_start><jupyter_text>R for Data Analysis and Probability Functions<jupyter_code>library(gtools)
#install.packages("moments")
library(moments)<jupyter_output><empty_output><jupyter_text>Random Variables (R.V.)* A random variable, usually written X, is a variable whose possible values are numerical outcomes of a random phenomenon.* A random variable is always denoted with an upper case* An observed variable is denoted with a lower case There are two types of random variables:* **Discrete random variable**: Takes on a finite (or countably infinite) number of values. ex) average height of 100 people* **Continuous random variable**: Takes on an interval of values (uncountably infinite values) ex) number of people present in this class  Discrete Distribution of Discrete RV* The distribution of a discrete random variable is described by a collection of possible distinct outcomes and their associated probabilities. * It is described by the probability mass function (PMF) and the cumulative distribution function (CDF).  Example 1: Binomial DistributionA binomial random variable counts how often a particular event occurs in a fixed number of tries or trials. This is usually likened to a coin flip or True verses False. For a variable to be a binomial random variable, ALL of the following conditions must be met:* There are a fixed number of trials (a fixed sample size).* On each trial, the event of interest either occurs (success) or does not (failure).* The probability of occurrence (or not) is the same on each trial.* Trials are independent of one another.Example: Number of left-handers in a randomly selected sample of 50 unrelated peopleYou need:* n=number of trials (sample size)* p= probability event occurs on any one trial$$P(X=x)={{n}\choose{x}} p^x (1-p)^{n-x}$$for $x=0, 1, 2,..., n$ where $ {{n}\choose{x}}=\frac{n!}{x!(n-x)!}$<jupyter_code>#Generate N=15 independent binomial random variables with n=5 trials and probability p=.6
rbinom(15, 5,.6 ) ## this means that out of 5 trials we either get a success or we do not with a probability of 0.6
#What if we up the probability to p=0.99 for getting a success?

rbinom(15, 5,.9 )
# Probability distribution
#Generate a sample of N=10,000 with n=7 trials and a probability of success p=.3
X=rbinom(10000,7, .3)

#X
# Barplot of the probability distribution 
## Why barplot and not a histogram??
# If I sample 10,000 numbers from Bin(n=7, p=.3), what does the distribution look like?
barplot(table(X))

##distribution: this gives you the probability of x=0,1,2,3,4,5,6,7
dbinom(0:7, 7, .3)
#note: these outputs are between 0 and 1 and are probabilities! ie) a single point/outcome has probability
#check our assumption that the probabilities sum to 1!
sum(dbinom(0:7, 7, .3))

#Example: What is the probability of exactly 2 successes (P(X=2)) for a binomial distribution Bin(n=7, p=.3)? 
dbinom(2,7,.3) #calculate using R
#choose(7,2)*(.3^2)*(.7^5) #calculate by hand<jupyter_output><empty_output><jupyter_text>Describing our distribution: Population Mean of Binomial Distribution: $E(x)=n*p$ Population Variance of Binomial Distribution: $Var(x)=n*p(1-p)$ Population Standard deviation of Binomial Distribution: $sd(x)=\sqrt{ n*p(1-p)}$<jupyter_code>#sample mean
mean(X) 
7*.3
#sample variance
var(X)
7*.3*(1-.3)
#sample standard deviation
sd(X)
sqrt(7*.3*(1-.3))<jupyter_output><empty_output><jupyter_text>With a large sample our expected mean and variance for the population are close to our sample mean and variance! What happens when we have a smaller sample?<jupyter_code>#Sample of 10
X_10=rbinom(10,7, .3)
#sample mean
mean(X_10)
7*.3
#sample variance
var(X_10)
7*.3*(1-.3)
#sample standard deviation
sd(X_10)
sqrt(7*.3*(1-.3))<jupyter_output><empty_output><jupyter_text>Continuous  Distribution of Continuous RV* The distribution of a continuous random variable is described by its probability density function (PDF). You can find the probability between two constants by adding up the area under the density curve. * Note: Unlike discrete random variables where we can determine $P(X=x)$ for all possible values of x and get a probability for a point, the probability that X takes on any particular value x is 0 for continuous random variables. AKA. We can't find $P(X=x)$!* Instead, we find the probability that $X$ falls into some interval $(a,b)$ ie. $P(a<X<b)$.  We can use the PDF to do this! Probability Density Function (PDF) and Cumulative Distribution Function (CDF)* PDF is denoted by $f_X(x)$* CDF is denoted by $F_X(x)$Outputs of $f_X(x)$ are NOT probabilities! Describing A Distribution:* E(x)=Expected Value ($\mu$)= mean of a random variable X * Var(x)= Variance ($\sigma^2$)= how far a set of numbers is spread out from their average value* Skewness=measure of lack of symmetry* Kurtosis=how tall and sharp the central peak is* Median=midpoint  Example 2: Normal (Gaussian) Distribution$$f_X(x)=\frac{1}{{\sigma \sqrt{2\pi } }}  e^{\frac{ - ( x - \mu )^2} {2\sigma^2}} $$where $-\infty<x<\infty$.Mean=$\mu$ and $\mu\in\mathbb{R}$ Variance=$\sigma^2$ and $\sigma^2>0$ * Standard normal distribution has $\mu=0$ and $\sigma=1$<jupyter_code>#Generate N=15 independent normal random variables with mean=20 and sd=8 
N_15=rnorm(15, mean=20,sd=8 ) 
N_15
#What is the mean of our sample?
mean(N_15) #Is this close to E(x)=20?

#What is the variance of our sample? The standard deviation?
var(N_15) 
sd(N_15) 
#What is the skewness and kurtosis of our sample?
skewness(N_15) #skewness of normal distribution is 0!
#negative indicates skewed left, positive indicates skewed right
kurtosis(N_15) #kurtosis of normal distribution should be near 3
#plot a histogram of the distribution of our sample
h<-hist(N_15, breaks=5, col="blue", xlab="Simulation", 
  	main="Histogram with Normal Curve") 
xfit<-seq(min(N_15),max(N_15),length=15) 
yfit<-dnorm(xfit,mean=20,sd=8) 
yfit <- yfit*diff(h$mids[1:2])*length(N_15) 
lines(xfit, yfit, col="green", lwd=2) #normal curve


#Now generate N=10000 independent normal random variables with mean=20 and sd=8
N_big=rnorm(10000, mean=20, sd=8)

#What is the mean of our sample?
mean(N_big) #Is this close to E(x)=20?

### This is called the law of large numbers!  
#The average of the results obtained from a large number of 
#trials should be close to the expected value
#What is the variance of our sample?
var(N_big)
sd(N_big) 
#What is the skewness and kurtosis of our sample?
skewness(N_big) #skewness of normal distribution is 0!
#negative indicates skewed left, positive indicates skewed right
kurtosis(N_big) #kurtosis of normal distribution is near 3
#plot a histogram of the distribution of our sample
h_big<-hist(N_big, breaks=50, col="blue", xlab="Simulation", 
  	main="Histogram with Normal Curve") 
#xfit<-seq(min(N_big),max(N_big),length=10000) 
#yfit<-dnorm(xfit,mean=20,sd=8) 
#yfit <- yfit*diff(h$mids[1:2])*length(N_big) 
#lines(xfit, yfit, col="green", lwd=2) #normal curve<jupyter_output><empty_output><jupyter_text>The Law of Large Numbers1. The average of many independent samples is (with high probability) close to the mean of the underlying distribution.     * We saw this!! The average of the samples was close to E(x)!2. This density histogram of many independent samples is (with high probability) close to the graph of the density of the underlying distribution.    * We also saw this!! The histogram and the blue line match up!  Central Limit Theorem Example (CLT) Central Limit TheoremIf the sample size n is "sufficiently large," then:(1) the sample mean $\bar{X}=\frac{\sum_{i=1}^n}{n} X_i$ follows an approximate normal distribution(2) with mean $E(\bar{X})=\mu$(3) and variance $Var(\bar{X})=\frac{\sigma^2}{n}$We write: $$\bar{X}⟶N(\mu,\frac{\sigma^2}{n})\ as \ n\rightarrow\infty$$   Example Let X be the random variable associated to drawing a fair dice with outcomes 1,2,3,4,5,6. Suppose $X_1, · · · , X_{30}$ be i.i.d as X. a) X is a discrete random variablethe mean of X: $\mu=\sum_{i=1}^6 (i)(\frac{1}{6})=3.5$the variance of X: $\sigma^2=\sum_{i=1}^6 (x_i -\mu)^2 p_i=\frac{1}{6}\sum_{i=1}^6 (x_i -3.5)^2$=3.5By the central limit theorem, for large n:  $$\bar{X}\sim N(\mu,\frac{\sigma^2}{n})=N(3.5,\frac{3.5}{30}=0.117)$$a) Find the distribution of the random variable $\bar{X}$ using CLT.Plot this distribution.<jupyter_code>x=seq(2.5,4.5,0.001)
plot(x,dnorm(x,mean=3.5,sd=0.34),type='l',main='The distribution of the sample mean using CLT')<jupyter_output><empty_output><jupyter_text>b)This time do not use the CLT, instead by running a simulation in R find the mean and variance of $\bar{X}$. You can use the “sample” function in R to roll your dice!<jupyter_code>barX=rep(0,1000)
for (i in 1:1000){barX[i]=mean(sample(1:6,30,replace=T))}
mean(barX)
var(barX)<jupyter_output><empty_output><jupyter_text>c) Plot the distribution found in the first and second part in the same graph.<jupyter_code>hist(barX,freq=NULL, col="Green")
par(new=TRUE)
plot(x,dnorm(x,mean=3.5,sd=0.34),type='l', xaxt="n", yaxt="n", xlab="", ylab="", col="purple")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12185152452496686, 'fraction_numerical': 0.031705700397702166, 'mean_word_length': 5.560144927536232, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '573273', 'license': 'MIT', 'n_tokens_mistral': 3086, 'n_tokens_neox': 2881, 'n_words': 1150, 'path': 'RVDistSamp.ipynb', 'repo_name': 'sarahmul/BioStatsBasics', 'size': '9052'}