# 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.
# 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
# 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.
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.
qnorm(.1,mean=10, sd=5, lower.tail = TRUE)
## [1] 3.592242
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
# 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
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