# changing the directory 
# this is the path that contains the excel file you want to load
setwd('/Users/stp48131/Library/CloudStorage/Dropbox/WKU/Teaching/ECON_307/Class_Materials/Honors/Normal_Distribution')

The process for computing probabilities in R is similar to Excel. We will use the pnorm() function and specify the x value, mean, and standard deviation. We can also add an option to tell R if we want the probability to the left or right of x. The numbered examples below follow the ``Lab_Normal_Distribution.pdf” file on Blackboard.

  1. Assume mu=10 and sigma=5. Find P(X<6)
# lower.tail=TRUE means we want the area to the left of 6
pnorm(6, mean=10, sd=5, lower.tail=TRUE)
## [1] 0.2118554
  1. Assume mu=10 and sigma=5. Find P(X>15)
# lower.tail=FALSE gives the area to the right of 15
pnorm(15, mean=10, sd=5, lower.tail=FALSE)
## [1] 0.1586553

We could have estimated the previous probability using the following:

1-pnorm(15, mean=10, sd=5, lower.tail=TRUE)
## [1] 0.1586553

This is the equivalent of using 1-norm.dist(\(x,\mu,\sigma,TRUE\)) in Excel.

  1. Assume mu=10 and sigma=5. Find P(6<X<15)
pnorm(15, mean=10, sd=5, lower.tail=TRUE) - 
  pnorm(6, mean=10, sd=5, lower.tail=TRUE)
## [1] 0.6294893

The previous problems were similar to the norm.dist() problems in Excel. The following are similar to the norm.inv() problems. The qnorm() function returns x values that satisfy a probability statement.

  1. Assume mu is 10 and sigma=5. Find x such that P(X < x) = 0.10.
qnorm(.1,mean=10, sd=5, lower.tail = TRUE)
## [1] 3.592242
  1. Assume mu is 10 and sigma=5. Find x such that P(X>x) = 0.10.
qnorm(.1,mean=10, sd=5, lower.tail = FALSE)
## [1] 16.40776
# this is the same as
qnorm(.9,mean=10, sd=5, lower.tail = TRUE)
## [1] 16.40776
  1. Assume you have data on June returns for all companies listed in the S&P 500. The average return is 3% with a standard deviation of 3%. Find the return that puts a firm in the top 1% of earnings.
# lower.tail=FALSE tells R that the area under the curve to the right of x is 0.01.
qnorm(.01,3,3,lower.tail = FALSE)
## [1] 9.979044
# this is the same as:
qnorm(.99,3,3,lower.tail = TRUE)
## [1] 9.979044
  1. Assume you have data on June returns for all companies listed in the S&P 500. The average return is 3% with a standard deviation of 3%. Find the return that puts a firm in the bottom 5% of earnings.
qnorm(.05,3,3,lower.tail = TRUE)
## [1] -1.934561
# this is the same as: 
qnorm(.95,3,3,lower.tail = FALSE)
## [1] -1.934561

The following examples will compute probabilities using observed returns for Apple. This is similar to what you would be expected to do in a homework assignment.

## loading libraries to load the excel file
## and generate the returns
library(tidyverse) # this contains the lag function we need
library(readxl) # this is for loading the Excel file
#loading the excel file
data <- read_excel("Normal_Distribution_R.xlsx")
# computing returns and attaching the data
data$AAPL_return <-  data$adjusted_price/lag(data$adjusted_price) -1
attach(data)
# compute the mean and standard deviation for the returns
# we store these values so we can reference them later
average <- mean(AAPL_return,na.rm=TRUE)
stddev <- sd(AAPL_return,na.rm=TRUE)

We can use the the mean and standard deviation from above to find the relevant probabilities for AAPL returns.

Assume AAPL returns are normally distributed with the mean and standard deviation estimated above. Find the probability the return is less than 5%.

pnorm(.05,mean = average,sd = stddev, lower.tail = TRUE)
## [1] 0.6202857

Assume AAPL returns are normally distributed with the mean and standard deviation estimated above. Find the probability the return is greater than than 15%

pnorm(.15,mean = average,sd = stddev, lower.tail = FALSE)
## [1] 0.05738237

Assume AAPL returns are normally distributed with the mean and standard deviation estimated above. Find the probability the return is greater than than 2% but less than 8%.

pnorm(.08,mean = average,sd = stddev, lower.tail = TRUE) -
  pnorm(.02,mean = average,sd = stddev, lower.tail = TRUE)
## [1] 0.2840252