You are on page 1of 3

Term 2, 2023/2024

DSA211 Statistical Learning with R


Lab Lesson 3:
# Example 1: chi-square distribution
c1 <- dchisq(1:40, 15) # chi-square pdf with df=15
c2 <- pchisq(1:40, 15) # chi-square cdf with df=15
c3 <- qchisq(c(0.01, 0.05, 0.1, 0.9, 0.95, 0.99), 15) # chi-square percentiles
c10 <- dchisq(1:40, 10) # chi-square pdf with df=10
plot(1:40, c10, col="green", type="l", xlab="X values", ylab="density",
main="Chi-square pdf with 10 and 15 df ")
lines(1:40, c1, col="red", type="l")
legend(25, 0.08, lty=1, col=c("green", "red"), legend= c("df=10", "df=15"))

# Example 2: t-distribution with df=5


xval <- seq(from = -4, to =4, by=0.01 )
t1 <- dt(xval, 5)
t2 <- pt(xval, 5)
t3 <- qt(c(0.01, 0.05, 0.1, 0.9, 0.95, 0.99), 5)
n1 <- dnorm(xval, 0, 1)
plot(xval, n1, col="blue", type="l", xlab="X values", ylab="density",
main="t-distribution with 5 df and standard normal distribution ")
lines(xval, t1, col="red", type="l")
legend(1.2, 0.35, lty=1, col=c("red", "blue"), legend= c("t with df=5", "N(0,1)"))

1
Term 2, 2023/2024

# Example 3: Poisson modeling (London death data)


#download the package function; only need to do it one time
library(fitdistrplus) # call the package function every time you use it
death <- c(rep(0,484), rep(1,391), rep(2, 164), rep(3,45), rep(4,11), rep(5,1)) # generate the
data set
fpois <- fitdist(death, distr="pois") # fit the data set to Poisson distribution
result1 <- gofstat(fpois, chisqbreaks=c(0:3), discrete=TRUE,
fitnames=c("Poisson")) # run the goodness of fit test
plot(fpois)
summary(fpois)
result1

# Example 4: Exponential modeling (volcano data)


# you may try some other distributions: gamma, log normal, exponential
volcano <- c(126, 73, 3, 6, 37, 23, 73, 23, 2, 65,
94, 51, 26, 21, 6, 68, 16,
20, 6, 18, 6, 41, 40, 18, 41, 11, 12,
38, 77, 61, 26, 3, 38, 50, 91, 12)
fexp <- fitdist(volcano, distr= "exp")
fnorm <- fitdist(volcano, distr= "norm")
summary(fexp)
summary(fnorm)
plot(fexp)
plot(fnorm)

2
Term 2, 2023/2024

#Example 5: Log normal stock price model (SingTel data)


# input data set (put all the data files in working directory (WD))
# getwd() -- you can find your current working directory
# setwd(<path>) – set your working directory
# Or use Session ==> Set Working Directory è to locate your WD in your computer
Z74SI <- read.csv("Z74SI.csv", stringsAsFactors=TRUE) #input a data file to R platform
price <- Z74SI$Adj.Close # put the closing price to object "price"
volume <- Z74SI$Volume
rate <- NULL # define a new object "rate" without any elements
# calculate the rates of daily change and put them to object "rate"
for (i in 1:(length(price)-1))
{rate[i] <- (price[i]-price[i+1])/price[i+1]}
frate <- fitdist(rate, "norm")
summary(frate)
plot(frate)

#Example 6: Historical 99% VaR of one day for SingTel investment 4000 shares at $3.30
AbsVaR <- -1*quantile(rate, 0.01)*4000*3.30
meanR <- mean(rate)*4000*3.30
SingVaR1 <- meanR+AbsVaR
SingVaR1

#Example 7: Parametric 99% VaR of one day for SingTel investment 4000 shares at $3.30
SingVaR2 <- sd(rate)*4000*3.30*qnorm(0.99, 0, 1)
SingVaR2

You might also like