par(mfrow=c(1,2))
# Option 2: inverse square
x <- seq(1,5,0.1)
y <- 1/(x^2)
plot(y, main = "Inverse Square", type = "l")

# negative exponential movement function (option 5 in CDPOP)
a <- 1
b <- 1
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, main = "Negative Exponential", type = "l")

# negative exponent comparing effects of parameters a and b
par(mfrow=c(3,4))
a <- 1
b <- 1
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=1 b=1')

a <- 2
b <- 1
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=2 b=1')

a <- 4
b <- 1
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=4 b=1')

a <- 8
b <- 1
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=8 b=1')

a <- 1
b <- 2
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=1 b=2')

a <- 1
b <- 4
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=1 b=4')

a <- 1
b <- 8
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=1 b=8')

a <- 2
b <- 2
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=2 b=2')

a <- 4
b <- 2
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=4 b=2')

a <- 8
b <- 2
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=8 b=2')

a <- 2
b <- 4
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=2 b=4')

a <- 2
b <- 8
x <- seq(1,5, 0.1)
y <- a*10^(-b*x)
plot(y, ylim=c(0,0.5), xlim=c(0,20), type = "l", main = 'a=2 b=8')


'''
In this homework, you are supposed to learn how to simulate and estimate AR/MA models
in R, and how to use ACF and PACF to identify models
'''

# 3.1
'''
Consider an AR(1) process f(t) = ø*f(t-1) + z(t), z~N(0,4)   |ø| < 1
Simulate with ø = 0.7, t=1..n, n=10000=10^4

Note: ACF of AR(1): y = ø^h, h >= 1
partial autocorrelation function alpha(h) = 1 if h=1 else 0
'''

#A: Plot the sample autocorrelation function ρ_hat sample partial autocorrelation function α_hat of the simulated process.
#How are these consistent with an AR(1) process? Can you see the relation between ø and ρ_hat(1)

set.seed(1234)
y <- arima.sim(
	model = list(ar = 0.7),
	n = 10000,
	innov = rnorm(10000, sd = 2)
)

par(mfrow=c(1,2)) #create a window with 1 row and 2 columns
AutoCorrelation = acf(x, plot = FALSE)
plot(AutoCorrelation, main = "Auto Corr. Func. AR(1)")
PartialAutoCorr = pacf(x, plot=FALSE)
plot(PartialAutoCorr, main="Partial Auto Corr. Func.")

#The ACF "tails off" to || x_n - x_m || < Epsilon after t>=10
#The PACF "cuts off" at t=2

'''
f\Mdl|      AR(P)     | MA(q)           | ARMA(p,q)
----------------------------------------------
ACF  |Tails off       | cuts off at t=q | Tails off
PACF |cuts off at t=p | Tails off       | Tails off
'''

alpha_1 =pacf(x, plot=FALSE)$acf[1]

#[1] 0.7011247
# alpha(1) approx ø

#B: Make a plot comparing the sample autocorrelation with the theoretical autocorrelation. Do the same for the partial autocorrelation

phi = .7
acf_theo = phi^(0:40) # Numerical Theoretical ACF with up to 40 lags - length 41 y(h) = ø^h , h >= 1
pacf_theo = c(phi, rep(0,39)) # Theoretical PACF with up to 40 lags - length 40: p(h) = ø^h , if h=1 else p(h)=0
# Plotting ACF and PACF
par(mfrow=c(1,2))

acf_empirical = acf(x, plot=FALSE)$acf
pacf_empirical = pacf(x, plot=FALSE)$acf

# length acf(x, plot=FALSE)$acf = 41
plot(0:40, acf_empirical, type = "h", ylab = "ACF", xlab = "h")
lines(0:40, acf_theo , col=2, type = "l", lty=2)
legend("topright", col = 1:2, legend = c(expression(hat(rho)(h)),expression(rho(h))), lty=1:2)

# length( pacf(x,plot=FALSE)$acf ) = 40
plot(1:40, pacf_empirical, type = "h", ylab = "PACF", xlab ="h")
lines(1:40, pacf_theo, col=2, type = "l",lty=2)
legend("topright", col = 1:2, legend = c(expression(hat(alpha)(h)),expression(alpha(h))), lty=1:2)

# length [acf = pacf +1]

# The theoretical and empirical seem to agree.

#C: Estimate an AR(1) and an AR(2) model for the simulated data using the arima function in R.
#   Compare the two models.
#   Is ø_hat_2 signficantly different from zero?
#   Compare the quality of ø_1 for the two models in terms of bias and standard error.

arima(x, order =c(1,0,0), include.mean = FALSE) # AR(1) # order (p,d,q)(P,Q,D) - (non-seasonal) component

'''
Call:
arima(x = x, order = c(1, 0, 0), include.mean = FALSE)

Coefficients:
         ar1
      0.7012 = Ø
s.e.  0.0071 = standard error

sigma^2 estimated as 3.901:  log likelihood = -20996.39,  aic = 41996.78
'''

arima(x, order =c(2,0,0), include.mean = FALSE) # AR(2)

'''
Call:
arima(x = x, order = c(2, 0, 0), include.mean = FALSE)

Coefficients:
         ar1      ar2
      0.7023  -0.0015
s.e.  0.0100   0.0100

sigma^2 estimated as 3.901:  log likelihood = -20996.38,  aic = 41998.76
'''

# Q:Is ø_hat_2 signficantly different from zero?
# A: ø_hat_2 = -0.0015 - close to zero

# Q: Compare the quality of ø_hat_1 for the two models in terms of bias and standard error.
# A: Coefficients for AR(1) -> ø_hat_1=0.712, and AR(2) -> ø_hat_1 =0.7023 There is a small difference
#    Standard error - AR(1) -> s.e    =0.0071, and AR(2) ->s.e.    =0.01   There is a small difference

#Note: ø_hat_2 =0.01 and s.e for it is 0.01 so ø_hat_2 is close to zero
#
# AR(1) =>  log likelihood = -20996.39
# AR(2) =>  log likelihood = -20996.38

# 3.2
# C:  Simulate the  process as an AR(1) model
#     with θ = 0.7 for t = 1, . . . , n with n = 10000 by implementing MA(1)
# Y_t = θ Z_t−1 + Z

y = arima.sim(
	model = list(ma = c(.7)),
	n = 10000,
	innov = rnorm(10000, sd = 2)
)

#D: Plot the sample ACF and PACF for the simulated data. How are these consistent with an MA(1) process
par(mfrow=c(1,2))
acf(y)
pacf(y)

#ACF cuts off after one lag
#PACF tails off - like MA(p) model

# E: Fit an MA(a) and an AR(5) model to the simulated data.
#    Compare the respective AR coefficients with the correspodning powers of θ in (2).
#    How well does it fit?
#    Will an AR(10) improve the result?

theta = 0.7
ma1 = arima(y, order = c(0,0,1), include.mean=FALSE) #MA(1)
'''
Call:
arima(x = y, order = c(0, 0, 1), include.mean = FALSE)

Coefficients:
         ma1
      0.6987
s.e.  0.0072

sigma^2 estimated as 3.992:  log likelihood = -21111.36,  aic = 42226.73
'''

ar5 = arima(y, order = c(5,0,0), include.mean=FALSE)
'''
Call:
arima(x = y, order = c(5, 0, 0), include.mean = FALSE)

Coefficients:
         ar1      ar2     ar3      ar4     ar5
      0.6876  -0.4605  0.2956  -0.1814  0.0764
s.e.  0.0100   0.0120  0.0125   0.0120  0.0100

sigma^2 estimated as 4.028:  log likelihood = -21155.74,  aic = 42323.49
'''

# From (2) we should have:
'''
> -0.7^2
[1] -0.49
> 0.7^3
[1] 0.343
> -0.7^4
[1] -0.2401
> 0.7^5
[1] 0.16807
'''
# For j=1..4 the terms match +-0.05 but is more for j=5.

ar10 = arima(y, order = c(10,0,0), include.mean=FALSE)
rbind(ar10$coef,theta^(1:10)*(-1)^(2:11))

#The terms match very well


 rnorm(10) #normal distribution of 10 numbers
rnorm(10, mean=100, sd=20)
randNorm10 <- rnorm(10)
randNorm10
dnorm(randNorm10) #probability of these numbers occuring
dnorm(c(-1,0,1))

require(ggplot2)
randNorm <- rnorm(30000)
randDensity <- dnorm(randNorm)
ggplot(data.frame(x=randNorm, y=randDensity)) + aes(x=x, y=y) +
  geom_point() +
  labs(x="Random Variables", y="Density")

pnorm(randNorm10)
pnorm(c(-3, 0, 3)) #left tail probability
pnorm(1) - pnorm(0)

randNorm10
pnorm(randNorm10)
qnorm(pnorm(randNorm10))

rbinom(n=1, size=10, prob=0.4)
rbinom(n=5, size=10, prob=0.4)
rbinom(n=10, size=10, prob=0.4)
rbinom(n=1, size=1, prob=0.4)

binomData <- data.frame(Success=rbinom(n=10000, size=10, prob=0.3))
ggplot(binomData, aes(x=Success)) + geom_histogram(binwidth=1)

binom5 <- data.frame(Successes=rbinom(n=10000, size=5, prob=0.3), Size=5)
head(binom5)
binom10 <- data.frame(Successes=rbinom(n=10000, size=10, prob=0.3), Size=10)
binom100 <- data.frame(Successes=rbinom(n=10000, size=100, prob=0.3), Size=100)
binom1000 <- data.frame(Successes=rbinom(n=10000, size=1000, prob=0.3), Size=1000)
binomAll <- rbind(binom5, binom10, binom100, binom1000)
head(binomAll)
tail(binomAll)
ggplot(binomAll, aes(x=Successes)) + geom_histogram() + facet_wrap(~Size, scales="free")

dbinom(x=3, size=10, prob=0.3)
pbinom(q=3, size=10, prob=0.3)
qbinom(p=0.3, size=10, prob=0.3)
qbinom(p=c(.3, .35, .4, .5, .6), size=10, prob=0.3)

pois1 <- rpois(n=10000, lambda=1)
pois2 <- rpois(n=10000, lambda=2)
pois5 <- rpois(n=10000, lambda=5)
pois10 <- rpois(n=10000, lambda=10)
pois20 <- rpois(n=10000, lambda=20)

pois <- data.frame(Lambda.1=pois1, Lambda.2=pois2, Lambda.5=pois5, Lambda.10=pois10, Lambda.20=pois20)
head(pois)

require(reshape2)
pois <- melt(data=pois, variable.name="Lambda", value.name="x")
head(pois)
tail(pois)
require(stringr)
pois$Lambda <- str_extract(string=pois$Lambda, pattern="\\d+")
head(pois)
class(pois$Lambda)
pois$Lambda <- as.factor(as.numeric(pois$Lambda))
tail(pois)

ggplot(pois, aes(x=x)) +
  geom_density(aes(group=Lambda, color=Lambda, fill=Lambda), adjust=4, alpha=1/2) +
  scale_color_discrete() + scale_fill_discrete() +
  ggtitle("Probability Mass function")

x <- sample(x=1:100, size=100, replace=TRUE)
x
mean(x)
#removing nulls while calculating averages
y<-x
y[sample(x=1:100, size=20, replace=FALSE)] <- NA
mean(y)
mean(y,na.rm=TRUE)

#weighted averages
grades <- c(95, 72, 87, 66)
weights <- c(1/2, 1/4, 1/8, 1/8)
mean(grades)
weighted.mean(x=grades, w=weights)

#variance: how spread out the data is? smaller variance, closer the values are to the center
var(x)
mean(x)
sum((x-mean(x))^2)/(length(x)-1)
sqrt(var(x))
sd(x)
sd(y)
sd(y,na.rm=TRUE)

summary(x)
summary(y)

quantile(x, probs=c(.25,.75))
quantile(x, probs=c(.1, .25, .5, .75, .92))

require(ggplot2)
cor(economics$pce, economics$psavert)
cor(economics[, c(2, 4:6)])
econCor <- cor(economics[, c(2, 4:6)])
require(reshape2)
econMelt <- melt(econCor, varnames=c("x", "y"), value.name = "Correlation")
econMelt <- econMelth[order(econMelt$Correlation), ]
econMelt
require(scales)
ggplot(econMelt, aes(x=x, y=y)) +
  geom_tile(aes(fill=Correlation)) +
  scale_fill_gradient2(low=muted("red"), mid="white", high="steelblue",
                       guide=guide_colorbar(ticks=FALSE, barheight=10),
                       limits=c(-1,1)) +
  theme_minimal() + labs(x=NULL, NULL)

m <- c(9, 9, NA, 3, NA, 5, 8, 1, 10, 4)
n <- c(2, NA, 1, 6, 6, 4, 1, 1, 6, 7)
p <- c(8, 4, 3, 9, 10, NA, 3, NA, 9, 9)
q <- c(10, 10, 7, 8, 4, 2, 8, 5, 5, 2)
r <- c(1, 9, 7, 6, 5, 6, 2, 7, 9, 10)
theMat <- cbind(m, n, p, q, r)
theMat
cor(theMat)
cor(theMat, use="everything")
cor(theMat, use="all.obs")
cor(theMat, use="complete.obs")
theMat
cor(theMat, use="na.or.complete")


# Example : 6.4B   Chapter : 6.4     Page No: 336
# Eigen Values and Eigen vectors of given matrices
A3<-matrix(c(2,-1,0,-1,2,-1,0,-1,2),ncol=3)
eigenvalues<-eigen(A3)$values
x<-eigen(A3)$vectors
print("Eigen values of A3 are ")
print(eigenvalues)
print("Eigen vectors of A3 in normalised form are ")
print(x)
#to get eigen vectors in the textbook multiply normalised vectors by scalars
x[,1]<-x[,1]*(1/x[1,1])
x[,2]<-x[,2]*(sqrt(2)/x[1,2])
x[,3]<-x[,3]*(1/x[1,3])
print("Eigen vectors with respect to the above eigen values respectively are")
print(x)
B4<-matrix(c(1,-1,0,0,-1,2,-1,0,0,-1,2,-1,0,0,-1,1),ncol=4)
B4eigenvalues<-eigen(B4)$values
B4x<-eigen(B4)$vectors
print("Eigen values of B4 are ")
print(B4eigenvalues)
print("Eigen vectors of B4 in normalised form are")
print(B4x)
#to get eigen vectors in the textbook multiply normalised vectors by scalars
B4x[,1]<-B4x[,1]*(1/B4x[1,1])
B4x[,2]<-B4x[,2]*(1/B4x[1,2])
B4x[,3]<-B4x[,3]*(1/B4x[1,3])
B4x[,4]<-B4x[,4]*(1/B4x[1,4])
print("Eigen vectors with respect to the above eigen values respectively are")
print(B4x)
#The  answer may slightly vary due to  rounding off values
#The answers provided in the text book may vary because of the computation process
#Both answers are correct , here it is taken -Ax+b=0 , In the text book it is considered as Ax-b=0


pH <- c(8,9,8,10,10,9,10,7,12,12,11,12,9,10,11,11,9,10,11,9,9,10,11,9,8,10,8,9,10,
        8,9,13,13,7,10,10,9,9,9,9,10,13,11,6,8,7,7,10,8,5,11,11,7,10,10,12,8,9,13,
        11,10,11,10,9,9,8,11,12,10,9,7,10,11,10,9)

n <- length(pH)
nj <- table(pH)
print(nj)
nj <- c(0, 0, 0, 0, nj, 0)
names(nj) <- 1:14
# frecuencias relativas
hj <- nj/n
# frecuencias acumuladas
Nj <- cumsum(nj)
Hj <- cumsum(hj)

# b) Diagrama de barras para las frecuencias relativas.
barplot(height = 100*hj, xlab = "pH", ylab = "F. Relativa (%)", density = 16,
        col = "lightblue", border = "blue")

# c) Hacer un diagrama de barras para las frecuencias relativas acumuladas.
barplot(height = 100*Hj, xlab = "pH", ylab = "F. Rel. Acumulada (%)", density = 16,
        col = "lightblue", border = "blue")

# d) Usando la información de la tabla y de los diagramas, responder las siguientes preguntas:
#   * ¿Cuántas observaciones se realizaron en este caso?
print(n) # 75
#   * ¿Cuántas observaciones en la muestra tienen un pH netro? ¿ácido? ¿básico?
sum(nj[7])      # pH Neutro
sum(nj[1:6])    # pH ácido
sum(nj[8:14])   # pH básico

#   * ¿Qué porcentaje de observaciones tienen un pH neutro? ¿ácido? ¿básico?
sum(100*hj[7])
sum(100*hj[1:6])
sum(100*hj[8:14])

#   * ¿Que porcentaje de observaciones tienen un pH entre 9 y 11 unidades, inclusive?
sum(100*hj[9:11])

# Considere la información de la siguiente disitrbución de frecuencias:
# a) Completar la tabla
n <- 80
hj <- c(4/n,0.15,0.25,0.75-0.25-0.15-0.05,1-0.75)
print(hj)
Hj <- cumsum(hj)
print(Hj)
nj <- hj*n
print(nj)
Nj <- cumsum(nj)
print(Nj)

# b.
# b* La cantidad de datos mayores a 15 es 44%.
sum(nj[4:5]) # Falso, nos estan pidiendo es cantidad de datos y no el porcentaje

# b* La cantidad de datos mayores que 5 pero menores que 20 es 36.
sum(nj[2:3]) # Falso, el resultado es 32
# b* La proporción de datos mayores que 10 es 95.
sum(100*hj[3:5]) # Falso la proporción es del 80%
# b* La proporción de datos entre 10 y 20, inclusive, es 70%.
sum(100*hj[2:4]) # Verdadera


# Example : 6.1A   Chapter : 6.1     Page No: 291
# Eigen values and eigen vectors
solution<-function(A){
  sol<-eigen(A)
  lambda<-sol$values
  x<-sol$vectors #these are normalised eigen vectors
  #to get the eigen vectors as in texxt book multiply these normalised vectors with scalars
  x[,1]<-x[,1]*(1/x[1,1])
  x[,2]<-x[,2]*(1/x[1,2])
  print("The eigen values of the matrix are")
  print(lambda)
  print("The eigen vecotrs of the matrix respective to above eigen values are")
  print(x)
}

A<-matrix(c(2,-1,-1,2),ncol=2)
print("For A")
solution(A)
A2<-A%*%A
print("For square of A")
solution(A2)
A1<-solve(A)
print("For inverse of A")
solution(A1)
I<-matrix(c(1,0,0,1),ncol=2)
A4I<-A+4*I
print("For A+4I")
solution(A4I)
#The  answer may slightly vary due to  rounding off values
#The answers provided in the text book may vary because of the computation process
#Both answers are correct , here it is taken -Ax+b=0 , In the text book it is considered as Ax-b=0


					R07

				Principal Components Analysis

options(digits=4, scipen=3)

# Eigenvalues and eigenvectors

A <- matrix(sample(9), nrow=3)
A

A.e <- eigen(A)
A.e

A %*% A.e$vectors[,1]
A.e$values[1] * A.e$vectors[,1]

A %*% A.e$vectors[,2]
A.e$values[2] * A.e$vectors[,2]

A %*% A.e$vectors[,3]
A.e$values[3] * A.e$vectors[,3]

# PCA Example

X <- as.matrix(read.table("H:/S5600/Data sets/ASCII/Ch04/PCA_EXAMPLE.txt"))

apply(X, 2, mean)
apply(X, 2, sd)		# already standardized

pairs(X)

library(rgl)

rgl.open()
bg3d("white")

rgl.spheres(X[,1],X[,2],X[,3],radius=.1, color=rainbow(300)[rank(X[,1])])

X.cor <- cor(X)
X.cor

X.e <- eigen(X.cor)

X.e

X.svd <- svd(X.cor)
X.svd

U <- X.e$vectors	# Get rotation matrix of eigenvectors

zapsmall(t(U) %*% U)

Z <- X %*% U		# Rotated data matrix

pairs(Z)
zapsmall(cor(Z))

rgl.clear()		# All three principal components
rgl.spheres(Z[,1],Z[,2],Z[,3],radius=.1, color=rainbow(300)[rank(X[,1])])

rgl.clear()		# First two principal components
rgl.spheres(Z[,1],Z[,2],rep(0,300),radius=.1, color=rainbow(300)[rank(X[,1])])

rgl.clear()		# First principal component
rgl.spheres(Z[,1],rep(0,300),rep(0,300),radius=.1, color=rainbow(300)[rank(X[,1])])

# Rotate back to X-space

Z1 <- cbind(Z[,1], rep(0,300), rep(0,300))
X1 <- Z1 %*% t(U)

rgl.clear()
rgl.spheres(X[,1],X[,2],X[,3],radius=.1, color=rainbow(300)[rank(X[,1])], alpha=.3)
	# Original data
rgl.spheres(X1[,1],X1[,2],X1[,3],radius=.1, color=rainbow(300)[rank(X[,1])])
	# First PC

Z2 <- cbind(rep(0,300), Z[,2], rep(0,300))
X2 <- Z2 %*% t(U)

rgl.spheres(X2[,1],X2[,2],X2[,3],radius=.1, color=rainbow(300)[rank(X[,1])])
	# Second PC

Z3 <- cbind(rep(0,300),rep(0,300), Z[,3])
X3 <- Z3 %*% t(U)

rgl.spheres(X3[,1],X3[,2],X3[,3],radius=.1, color=rainbow(300)[rank(X[,1])])
	# Third PC

Z12 <- cbind(Z[,1:2],rep(0, 300))
X12 <- Z12 %*% t(U)

rgl.clear()
rgl.spheres(X[,1],X[,2],X[,3],radius=.1, color=rainbow(300)[rank(X[,1])], alpha=.3)
rgl.spheres(X12[,1],X12[,2],X12[,3],radius=.1, color=rainbow(300)[rank(X[,1])])
	# First two PCs

# Fractions of variance

lambda <- X.e$values	# Eigenvalues of cor(X) = variances of PCs
lambda

zapsmall(cov(Z))

zapsmall(cor(Z))

diag(cov(Z))
sum(diag(cov(Z)))	# Note: sum = number of dimensions = sum of original correlations

# The prcomp( ) function

X.pc <- prcomp(X, scale=T)
X.pc

X.pc$rotation	# Compare to U above - same except for possible sign reversal

U

X.pc$sdev	# Component sds
X.pc$sdev^2	# Component Variances - compare to lambda above
lambda

pairs(X.pc$x)	# Same as Z above, again except for possible sign change

# New example: State economic data
data(state)

gsp.raw<-read.table("H:/S5600/GSP_RAW.txt",header=T)
rownames(gsp.raw) <- state.abb
pairs(gsp.raw)
stars(gsp.raw, key.loc=c(15, 1.5))
cor(gsp.raw)	# Strong correlation

gsp.raw.pc <- prcomp(gsp.raw, scale=T)
gsp.raw.pc$sdev^2	# Variances - almost everything in PC 1

gsp.raw.pc$rotation[,1:2]	# Loadings for PCs 1 and 2
	# Loadings for PC 1 almost all equal - simply a measure of economic size

# COnvert each state's earnings into a percentage for each sector

gsp.share <- gsp.raw/apply(gsp.raw, 1, sum)*100

pairs(gsp.share)
stars(gsp.share, key.loc=c(15, 1.5))
cor(gsp.share)

gsp.share.pc <- prcomp(gsp.share, scale=T)
zapsmall(gsp.share.pc$sdev^2)	# Variances - much more balanced

pairs(gsp.share.pc$x[,1:6]) 	# First 6 PCs
plot(gsp.share.pc$x[,1:2], pch=16)	# First 2
	# identify outliers
identify(gsp.share.pc$x[,1], gsp.share.pc$x[,2], state.abb)
	# Or label all points
plot(gsp.share.pc$x[,1:2], type='n')
text(gsp.share.pc$x[,1:2], state.abb)

# Loadings - first 6 PCs

gsp.share.pc$rotation[,1:6]

# Values of some outliers in original units

gsp.share[c(2,8,11,28,50),]

# biplot - loadings on 1st 2 PCs and plot of first 2 PCs

biplot(gsp.share.pc)

# How many components?

plot(gsp.share.pc)	# Scree plot
plot(gsp.share.pc$sdev^2, type="o", pch=16)	# Alternate Version

zapsmall(gsp.share.pc$sdev^2)
sum(gsp.share.pc$sdev>1)	# Kaiser's Rule - keep components with variance > 1
abline(h=1,col="grey")

summary(gsp.share.pc)

# Horn's procedure - compare to randomly permuted (bootstrapped) data

gsp.boot <- gsp.share
for (i in 1:13) {
	gsp.boot[,i] <- sample(gsp.share[,i], replace=T) }
gsp.boot.pc <- prcomp(gsp.boot, scale=T)
points(gsp.boot.pc$sdev^2, type="o")	# Add to alternate scree plot; compare
					# Repeat; add different runs in different colors

# Another example - planetary data

planets1<-read.table("H:/S5600/planets.txt")
planets1
pairs(planets1)

# Log transform values except rings (add one to number Moons)

planets <- cbind(logDist=log(planets1$Dist), logRadius = log(planets1$Radius),
	logMass =log(planets1$Mass), logDensity=log(planets1$Density), 		logMoons=log(planets1$Moons+1), Rings=unclass(planets1$Rings)-1)
rownames(planets)<-rownames(planets1)
planets
pairs(planets)

planets.pc <- prcomp(planets, scale=T)
planets.pc$sdev^2

plot(planets.pc$sdev^2, type="o", pch=16)
abline(h=1,col="grey")

pairs(planets.pc$x)
plot(planets.pc$x[,1:2], pch=16)

identify(planets.pc$x[,1], planets.pc$x[,2], rownames(planets))
	# Note the three clusters

planets.pc$rotation[,1:2]

biplot(planets.pc)


## Linear algebra
1:10

x=seq(from=3.1, to=8.9, length.out=10)
x[1]
x[2]

x.t=t( x ) # transpose of x
length(x.t)
length(x)

x
x.t

y=x^2
y

x/y
y-x*x

dim(x %o% y) # outer product

x%*%y # inner product

sum(x*y)

### Create a matrix with 2 rows and 3 columns
M=matrix(0,2,3);
M

Xmat1=matrix(x,2,5)
Xmat1
Xmat2=matrix(x,2,5,byrow=T)
Xmat2

M<-Xmat1;
dimnames(M)

rownames(M)<-c("first","second")
M
colnames(M)<-c('a','b','c','d','e')
M

identical(M[,1],M[,'a'])

identical(M[2,],M['second',])

M[,1:3]
M[,c("a","d","e")]

M2=rbind(M, c(1,2,3,4,5))
rownames(M2)[3]<-'third'
M2
?cbind

length(M)
dim(M)

### Operations on matrices

M.t=t(M)
M.t
identical(t(M.t),M)

### Diagonal and identity matrix
diag(5) # identity matrix
diag(c(1,2,3,4,5)) # diagonal matrix

### Symmetric matrix
dim(M.t)
dim(M)

A=matrix(rnorm(9),3,3)
identical(A,t(A))
A= A+t(A);
identical(A,t(A))
# A is a symmetric matrix!

### Trace of a matrix
# ?tr()
diag(diag(c(1,2,3,4,5)))

A
sum(diag(A))
### Create our own function to calculate the trace
tr<-function(A){
  # Need to check whether A is a squared matrix or not
  out=sum(diag(A))
  return(out)
}
tr(A)

## Verify the properties of trace
B=matrix(rnorm(9),3,3)
B
A

tr(A)
tr(t(A))

tr(A+B)
tr(A)+tr(B)

A
B
A+B # A-B?

tr(A%*%B)
tr(B%*%A)

tr(A)
sum(eigen(A)$values)

## Matrix product:

## Matrix-vector product
dim(M)
xvec=rnorm(5)
xvec
M%*%xvec
# M: 2 by 5 matrix
# xvec: 5 by 1 vector (matrix)
# (2 by 5) %*% (5 by 1)
# 2 by 1

xvec2=rnorm(7)
M%*%xvec2

### Matrix-matrix product
dim(M)

C<- matrix(rnorm(15),5,3)
M%*%C # a 2 (by 5) by (5 by) 3 matrix

M2<- matrix(rnorm(10),2,5)

M*M2 # elementwise product between two matrices
dim(M)
dim(M2)

M%*%M2
M%*%t(M2)

t(M%*%t(M2))

M2%*%t(M)


# Example : 5   Chapter : 4.4     Page No: 233
# Gram-Schmidt method to convert matrix into its orthogonal form
magnitude<-function(a){
  x<-0
  for(i in 1:length(a)){
    x<-x+a[i]*a[i]
  }
  x<-sqrt(x)
  return(x)
}
a<-c(1,-1,0)
b<-c(2,0,-2)
c<-c(3,-3,3)
A<-a
B<-b-(sum(A*b)/sum(A*A))*A
C<-c-(sum(A*c)/sum(A*A))*A-(sum(B*c)/sum(B*B))*B
print("Orthogonal vectors corresponding to a,b,c are")
print(A)
print(B)
print(C)
q1<-(1/magnitude(A))*A
q2<-(1/magnitude(B))*B
q3<-(1/magnitude(C))*C
Q<-matrix(c(q1,q2,q3),ncol=3)
print("Orthogonal matrix with orthonormal vectors of a,b,c ")
print(Q)
#The  answer may slightly vary due to  rounding off values


#Stegarescu Ana-Maria 341

library(cubature)
library(pracma)

#Ex4
#Integrala dubla, de la 1 la 2, si de la 3 la 4
g4 <- function(x, y){
  x*y^2
}

#teta <- adaptIntegrate(g4, lowerLimit = c(1,3), upperLimit=c(2,4))$integral

teta <- integral2(g4, 1, 2, 3, 4)$Q

n <- 10^6
x <- runif(n, 1, 2)
y <- runif(n, 3, 4)

g4U <- g4(x, y)
teta_est <- sum(g4U)/n

#Ex5
#Integrala dubla de la 0 la Inf si de la 0 la x
g5 <- function(x, y){
  3*x*exp(-x-y)
}

teta <- integrate(function(x) {sapply(x, function(x)
{integrate(function(y) 3*x*exp(-x-y), 0, x)$value})}, 0, Inf)$value

#Schimbare de variabila, pentru x de la 0 la Inf, t va fi de la 0 la 1
#x = t/(1-t)
#t = x/(x-1)

#dx = 1/(1+t)^2

g5t <- function(t, y){
  x <- t/(1-t)
  g5(x, y)*1/(1-t)^2

}

n <- 10^6

t <- runif(n)
med_t <- sum(t)/n
y <- runif(n, 0, t/(1-t))

g5U <- g5t(t, y)
teta_est <- sum(g5U * t/(1-t))/n

#Caprita Catalin 341
#1.
# g(x) = 4 * x ^ 3 * exp(-x ^ 2 / 8) de integrat de la -Inf la Inf
# f(x) = 1 / 2 * sqrt(2 * pi) * exp(-x ^ 2 / 8
# h(x) = 4 * x ^ 3
n <-  1e7
X <- rnorm(n,0,2)

g <- function(x){
  4 * x ^ 3 * exp(-x ^ 2 / 8)S
}
h <- function(x){
  4 * x ^ 3
}
teta_aprox <- 2 * sqrt(2 * pi) * sum(h(X)) / n
teta <- integral(g,-Inf,Inf)
#TEMA: Aceeasi problema pornind de la o Uniforma(Schimbare de variabila)
#si Exponentiala

#Memenduf Alen 341
require(plotrix)

n <- 10^4
x <- runif(n, -1, 1)
y <- runif(n, -1, 1)

# asp => aspect ratio
plot(x, y,asp = 1, col = ifelse(x^2 + y^2 < 1, 'white', 'gray'))
draw.circle(0, 0, 1, border = "black", lwd = 3)

# Chirut Veronica
N <- c()
for (i in 1:10 ^ 5) {
  U <- c()

  sum <- 0
  n <- 0
  while(sum <= 1) {
    U <- runif(1, 0, 1)
    sum <- sum + U
    n <- n + 1
  }

  N <- append(N, n)
}

covarianta <- function(x, y) {
  mean(x * y) - mean(x) * mean(y)
}

# a
E_N <- mean(N)

# ASA NU!
# Folosim Monte Carlo - Lab 3
Var_N <- covarianta(N, N)

# b
cazuri_favorabile <- sum(N == 2)
cazuri_posibile <- length(N)

P_2 = cazuri_favorabile / cazuri_posibile

# c
# E(N) este o aproximare a numarului lui Euler

Tehnici de Simulare (TS)/Laborator/3/Laborator 3.r


library(tidyverse)
library(readr)
library(xts)
library(tseries)
library(astsa)

####CHAPTER 3

####ARIMA - Plug and Play####
x <- arima.sim(model = list(order = c(1, 1, 0),
                            ar = .9), n = 200)
# Plot x
plot(x)
# Plot the P/ACF pair of x
plot(acf2(x))
# Plot the differenced data
plot(diff(x))
# Plot the P/ACF pair of the differenced data
plot(acf2(diff(x)))
####

####Simulated ARIMA####
# Plot sample P/ACF of differenced data and determine model
y <- diff(x)
plot(acf2(y))
# Estimate parameters and examine output
sarima(x,2,1,0)
####

####Global Warming####
# Plot the sample P/ACF pair of the differenced data
plot(acf2(diff(globtemp)))
# Fit an ARIMA(1,1,1) model to globtemp
sarima(globtemp, 1,1,1)
# Fit an ARIMA(0,1,2) model to globtemp. Which model is better?
sarima(globtemp, 0,1,2)
#0,1,2
####

####Diagnostics - Simulated Overfitting####
# Plot sample P/ACF pair of the differenced data
plot(acf2(diff(x)))
# Fit the first model, compare parameters, check diagnostics
sarima(x, 0,1,1)
# Fit the second model and compare fit
sarima(x, 0,1,2)
# As you can see from the t-table, the second MA parameter is not significantly
# different from zero and the first MA parameter is approximately the same
# in each run. Also, the AIC and BIC both increase when the parameter is added.
# In addition, the residual analysis of your ARIMA(0,1,1) model is fine.
# All of these facts together indicate that you have a successful model fit.
####

####Diagnostics - Global Temperatures####
# Fit ARIMA(0,1,2) to globtemp and check diagnostics
sarima(globtemp, 0,1,2) #-3.58 , -4.51
# Fit ARIMA(1,1,1) to globtemp and check diagnostics
sarima(globtemp, 1,1,1) #-3.57 , -4.51
# Which is the better model?
# "ARIMA(___,___,___)"
"ARIMA(0,1,2)"
####

####Forecasting Simulated ARIMA####
# Plot P/ACF pair of differenced data
plot(acf2(diff(x)))
# Fit model - check t-table and diagnostics
sarima(x, 1,1,0)
# Forecast the data 20 time periods ahead
# sarima.for(x, n.ahead = ___, p = ___, d = ___, q = ___)
sarima.for(x, n.ahead =20, p = 1, d = 1, q = 0)
lines(y)
####

####Forecasting Global Temperatures####
# Fit an ARIMA(0,1,2) to globtemp and check the fit
sarima(globtemp, 0,1,2)
# Forecast data 35 years into the future
sarima.for(globtemp, 35, 0,1,2)
####

####CHAPTER 4

####Fit a Pure Seasonal Model####
# Plot sample P/ACF to lag 60 and compare to the true values
acf2(x, max.lag = 60)
# Fit the seasonal model to x
#sarima(x, p = 0, d = 0, q = 0,
#       P = ___, D = 0, Q = ___, S = ___)
sarima(x, p = 0, d = 0, q = 0,
       P = 1, D = 0, Q = 1,
       S = 12)
####

####Fit a Mixed Seasonal Model####
# Plot sample P/ACF pair to lag 60 and compare to actual
plot(acf2(x, max.lag=60))
# Fit the seasonal model to x
sarima(x, 0,0,1, 0,0,1, 12)
####

####Data Analysis - Unemployment I####
par(mfrow = c(3,1))
# Plot unemp
plot(unemp)
# Difference your data and plot it
d_unemp <-diff(unemp)
plot(d_unemp)
# Seasonally difference d_unemp and plot it
#dd_unemp <- diff(___, lag = 12)
dd_unemp <- diff(d_unemp, lag = 12)
plot(dd_unemp)
####

####Data Analysis - Unemployment II####
# Plot P/ACF pair of fully differenced data to lag 60
dd_unemp <- diff(diff(unemp), lag = 12)
plot(acf2(dd_unemp, max.lag=60))
# Fit an appropriate model
sarima(unemp, 2,1,0 , 0,1,1, 12)
####

####Data Analysis - Commodity Prices####
# Plot differenced chicken
chicken
plot(diff(chicken))
# Plot P/ACF pair of differenced data to lag 60
plot(acf2(diff(chicken), max.lag=60))
# Fit ARIMA(2,1,0) to chicken - not so good
sarima(chicken, 2,1,0)
# Fit SARIMA(2,1,0,1,0,0,12) to chicken - that works
sarima(chicken, 2,1,0, 1,0,0, 12)
####

####Data Analysis - Birth Rate####
# Plot P/ACF to lag 60 of differenced data
d_birth <- diff(birth)
plot(acf2(d_birth, max.lag=60))
# Plot P/ACF to lag 60 of seasonal differenced data
dd_birth <- diff(d_birth, lag = 12)
plot(acf2(dd_birth, max.lag=60))
# Fit SARIMA(0,1,1)x(0,1,1)_12. What happens?
sarima(birth, 0,1,1, 0,1,1, 12)
# Add AR term and conclude
sarima(birth, 1,1,1, 0,1,1, 12)
####

####Forecasting Monthly Unemployment####
# Fit your previous model to unemp and check the diagnostics
sarima(unemp, 2,1,0, 0,1,1, 12)
# Forecast the data 3 years into the future
sarima.for(unemp,n.ahead = 3*12, 2,1,0,
           P=0,D=1,Q=1,
           S=12)
####

####How Hard is it to Forecast Commodity Prices?####
# Fit the chicken model again and check diagnostics
sarima(chicken, 2,1,0, 1,0,0, 12)
# Forecast the chicken data 5 years into the future
sarima.for(chicken, n.ahead=5*12, 2,1,0, 1,0,0, 12)
####


# Inverse of a matrix
set.seed(1)
A=matrix(rnorm(9),3,3)
B=matrix(rnorm(9),3,3)
# Pseudorandomness

dim(A)

A.inv =solve(A)
A.inv

A.inv %*% A
mat.tmp=A%*%A.inv
mat.tmp[2,3]==0
abs(mat.tmp[2,3])< 1e-10

AB.inv = solve(A%*%B)
B.inv=solve(B);

identical(AB.inv,B.inv%*%A.inv)
AB.inv-B.inv%*%A.inv
max(abs( AB.inv-B.inv%*%A.inv))<1e-10

# AB.inv satisfies that
# AB%*%AB.inv = I
# AB=A%*%B
#  AB%*%(B.inv)%*%(A.inv)
# = A%*%B%*%(B.inv)%*%(A.inv)
# = A%*% (B%*%B.inv)%*%(A.inv)
# = A%*%I%*%A.inv
# = A%*%A.inv = I

set.seed(1)
X=matrix(rnorm(6),3,2)

P=X%*%solve(t(X)%*%X)%*%t(X)

P%*%P%*%P%*%P%*%P-P
(diag(3)-P)%*%P

P%*%X-X
(diag(3)-P)%*%X

dim(P)
dim(X)
dim(P%*%X)
P%*%t(X)

# Rank of a matrix
qr(X)$rank
dim(X)

#
XXt=X%*%t(X)
qr(XXt)$rank
dim(XXt) # Not full rank!

solve(XXt)

# Determinant of a matrix
det(A)
1/det(solve(A))
det(A%*%B)
det(A)*det(B)

det(2*A)/det(A)
2^3

tr<-function(A){
  # Need to check whether A is a squared matrix or not
  out=sum(diag(A))
  return(out)
}
tr(A)

A.symm= (A+t(A))/2;
identical(A.symm,t(A.symm))

sum(eigen(A.symm)$values)
tr(A.symm)

det(A.symm)
prod(eigen(A.symm)$values)

# Eigenvalue and eigenvector
A.symm.eigenvalues = eigen(A.symm)$values
A.symm.eigenvectors = eigen(A.symm)$vectors

(A.symm%*%A.symm.eigenvectors[,1])/A.symm.eigenvectors[,1]
A.symm.eigenvalues[1]

eigen(XXt)
qr(XXt)$rank

eigen(P)

# Data set
library(AER)

#install.package('AER')

data('Fatalities')
?Fatalities

data('CreditCard')
?CreditCard

head(CreditCard)

dat.CC=CreditCard;
Y.CC=dat.CC[,1]=='yes';
X.CC=as.matrix(dat.CC[,c('reports','age','income','share','expenditure','dependents','months','majorcards','active')])

qr(X.CC)$rank
dim(X.CC) # Full rank, i.e., no multicolinearity!

fit.CC=lm(Y.CC~X.CC-1)
summary(fit.CC)

beta.hat=solve(t(X.CC)%*%X.CC)%*%t(X.CC)%*%Y.CC;
fit.CC$coefficients-beta.hat

P.CC=X.CC%*%solve(t(X.CC)%*%X.CC)%*%t(X.CC)

Yhat.CC=P.CC%*%Y.CC
max(abs(fit.CC$fitted.values-Yhat.CC))


notestreak=function(song){
# song is a vector of 0's and 1's that represents the hits (0) and
#   misses (1) of the notes in order

  temp=diff(c(0,which(song==1),length(song)+1))-1
  streak_lengths=subset(temp,temp>0)

  # About these commands:
  # which(song==1) returns a vector indicating which on which notes (indices) a miss occurs
  #                this is important for you because it will indicate the end of a streak
  # c(0,which(song==1),length(song)+1) creates a vector starting with 0 (indicates the beginning of the song),
  #                 then the locations of misses, and then the length of the song + 1 (indicates when the song has ended)
  # diff(above vector) calculates the difference between consecutive entries in this vector; we don't actually care about
  #			 the locations of the misses but how many hits occur between misses; we start the process of finding this
  #			 by finding the difference between consecutive indices in the vector (note that this is not the length of
  #			 the streak, but actually the length + 1, which is why the last part of the first line subtracts 1); the
  #			 resulting lengths are stored in "temp"

  m = median(streak_lengths)
  return (m)

}

# Hypothesis test based on the note streak method
notestreak_test = function(song,B=1000){
    # song is a vector of 0's and 1's that represents the hits (0)
	# and misses (1) of the notes in order
	# B is the number of new samples to generate

	test_stat = notestreak(song)
	n = length(song)
	tstar_b = numeric(B)
	for(b in 1:B){
		song_tmp = sample(song)  # for permutation test
#		song_tmp = sample(song,replace=T) # for non-parametric bootstrap
#		song_tmp = rbinom(n,1,mean(song)) # for parametric bootstrap
		tstar_b[b] = notestreak(song_tmp)
	}

	# calculate the two-sided p-value from the sampling distribution
	pval = 2*min(sum(tstar_b >= test_stat),sum(tstar_b <= test_stat))/B
		# calculate a one sided p-value
#	pval = sum(tstar_b >= test_stat)/B
	return(pval)
}

# Examples using Songs A and B from Table 1

songA = c(1,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,1,0,0,0)
notestreak(songA)
notestreak_test(songA,5000)

songB = c(0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,0,0)
notestreak(songB)
notestreak_test(songB,5000)


## 1. The Range ##

library(readr)
# Heads up that the following instruction can generate a column specification warnings, you can ignore that.
# These warnings are related to that `readr` has misguidedly guessed the type of values in the column `Pool QC`.
# To avoid this warning we can manually specify the type of this column the `col_types` parameter.
houses  <-  read_tsv('AmesHousing_1.txt', col_types = cols(`Pool QC` = col_character()))
library(dplyr)

sale_price_range <- houses %>%
    group_by(`Yr Sold`) %>%
    summarize(range_by_year = max(SalePrice) - min(SalePrice))

one  <-  FALSE
two  <-  TRUE

## 2. The Average Distance ##

C  <-  c(1,1,1,1,1,1,1,1,1,21)
average_distance <- function(vector) {
    distances  <-  vector - mean(vector)
    sum(distances) / length(distances)
}

avg_distance  <-  average_distance(C)

## 3. Mean Absolute Deviation ##

C  <-  c(1,1,1,1,1,1,1,1,1,21)

#average_distance <- function(vector) {
#    distances  <-  vector - mean(vector)
#    sum(distances) / length(distances)
#}
mean_absolute_deviation <- function(vector) {
    distances  <-  abs(vector - mean(vector)) #we only add abs function here
    sum(distances) / length(distances)
}

mad  <-  mean_absolute_deviation(C)

## 4. Variance ##

C  <-  c(1,1,1,1,1,1,1,1,1,21)

#mean_absolute_deviation <- function(vector) {
#    distances  <-  abs(vector - mean(vector))
#    sum(distances) / length(distances)
#}
variance <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sum(distances) / length(distances)
}

variance_C  <-  variance(C)

## 5. Standard Deviation ##

C  <-  c(1,1,1,1,1,1,1,1,1,21)

#variance <- function(vector) {
#    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
#    sum(distances) / length(distances)
#}
standard_deviation <- function(vector) {
    distances  <-  (vector - mean(vector))**2
    sqrt(sum(distances) / length(distances)) #adding sqrt at this line
}

standard_deviation_C  <-  standard_deviation(C)

## 6. Average Variability Around the Mean ##

library(dplyr)

standard_deviation <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sqrt(sum(distances) / length(distances))
}
# Measure first the variability for each year
houses_years_std <- houses %>%
    group_by(`Yr Sold`) %>%
    summarize(st_dev = standard_deviation(SalePrice)) %>%
    arrange(st_dev)

# Get years of max and min variability
greatest_variability  <-  houses_years_std %>%
  filter(st_dev == max(st_dev)) %>%
  pull(`Yr Sold`)

lowest_variability  <-  houses_years_std %>%
  filter(st_dev == min(st_dev)) %>%
  pull(`Yr Sold`)

## 7. A Measure of Spread ##

set.seed(10)

sample1  <-  sample(x = houses$`Year Built`, size = 50)
sample2  <-  sample(x = houses$`Year Built`, size = 50)

standard_deviation <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sqrt(sum(distances) / length(distances))
}
bigger_spread  <-  'sample 2'
st_dev1  <-  standard_deviation(sample1)
st_dev2  <-  standard_deviation(sample2)

## 8. The Sample Standard Deviation ##

standard_deviation <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sqrt(sum(distances) / length(distances))
}

library(ggplot2)
set.seed(1)
std_points  <-  replicate(n = 5000, expr = standard_deviation(sample(x = houses$SalePrice, size = 10)))

std_points_tibble <- tibble::tibble(std_points)

ggplot(data = std_points_tibble, aes(x = std_points)) +
    geom_histogram(bins = 10, position = "identity") +
    geom_vline(aes(xintercept = standard_deviation(houses$SalePrice))) +
    xlab("Sample standard deviation") +
    ylab("Frequency")

## 10. Bessel's Correction ##

standard_deviation <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sqrt(sum(distances) / length(distances))
}

library(ggplot2)
set.seed(1)

std_points  <-  replicate(n = 5000, expr = standard_deviation(sample(x = houses$SalePrice, size = 10)))

#ggplot(data = tibble::tibble(std_points), aes(x = std_points)) +
#    geom_histogram(bins = 10, position = "identity") +
#    geom_vline(aes(xintercept = standard_deviation(houses$SalePrice))) +
#    xlab("Sample standard deviation") +
#    ylab("Frequency")

population_stdev  <-  standard_deviation(houses$SalePrice)
standard_deviation_bessel_correction <- function(vector) {
    distances  <-  (vector - mean(vector))**2
    sqrt(sum(distances) / (length(distances) - 1) ) #Only this line has to change
}

library(ggplot2)
set.seed(1)

std_points  <-  replicate(n = 5000, expr = standard_deviation_bessel_correction(sample(x = houses$SalePrice, size = 10)))

std_points_tibble <- tibble::tibble(std_points)

ggplot(data = std_points_tibble, aes(x = std_points)) +
    geom_histogram(bins = 10, position = "identity") +
    geom_vline(aes(xintercept = population_stdev)) +
    xlab("Sample standard deviation") +
    ylab("Frequency")

## 11. Standard Notation ##

set.seed(1)

sample_sales  <-  sample(x=houses$SalePrice, size=100)

variance_bessel_correction <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sum(distances) / (length(distances) - 1)
}

standard_deviation_bessel_correction <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sqrt(sum(distances) / (length(distances)-1) )
}
computed_stdev  <-  standard_deviation_bessel_correction(sample_sales)
stdev_r  <-  sd(sample_sales)
equal_stdevs  <-  computed_stdev == stdev_r

computed_var  <-  variance_bessel_correction(sample_sales)
var_r  <-  var(sample_sales)
equal_vars  <-  computed_var == var_r

## 12. Sample Variance — Unbiased Estimator ##

population  <-  c(0, 3, 6)

samples  <-  list(c(0,3), c(0,6),
               c(3,0), c(3,6),
               c(6,0), c(6,3)
)

variance <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sum(distances) / length(distances)
}

standard_deviation <- function(vector) {
    distances  <-  (vector - mean(vector))**2 #we only need to compute the squared distances here
    sqrt(sum(distances) / length(distances) )
}
population_var  <-  variance(population)
population_std  <-  standard_deviation(population)

st_devs  <-  purrr::map_dbl(samples,sd)
variances  <-  purrr::map_dbl(samples,var)

mean_std  <-  mean(st_devs)
mean_var  <-  mean(variances)

equal_stdev  <-  population_std == mean_std
equal_var  <-  population_var == mean_var

Data Analyst in R/Step 5 - Probability and Statistics/2. Statistics Intermediate in R Averages and Variability/4. Measures of Variability.r


#integrate is a function from package stats which is included in R by default
#if not install package stats
# Example : 2   Chapter : 8.5     Page No: 448
# length of function sinx and sinx and cosx are orthogonal in function space
f1<-function(x){
  return (sin(x)*sin(x))
}
f2<-function(x){
  return (sin(x)*cos(x))
}
print("Inner product of sinx and sinx is ")
lsin2x<-integrate(f1,0,2*pi)
print(lsin2x)
print("Length of sinx is square root of above inner product")
print(sqrt(lsin2x$value))
print("Cos x and sinx are orthogonal in functional space because their inner product equals zero")
lsinxcosx<-integrate(f2,0,2*pi)
print(lsinxcosx)
#The  answer may slightly vary due to  rounding off values


# Example : 5.3B   Chapter : 5.3     Page No: 278
# Solve by crammers rule and inverse of the matrix

solve_by_crammersrule<-function(A,b){
  B1<-A
  B2<-A
  B3<-A
  B1[,1]<-b
  B2[,2]<-b
  B3[,3]<-b
  x1<-det(B1)/det(A)
  x2<-det(B2)/det(A)
  x3<-det(B3)/det(A)
  x<-c(x1,x2,x3)
  return(x)
}
inverse<-function(A){
  C<-matrix(c(1:9),ncol=3)
  for(i in 1:3){
    for(j in 1:3){
      if((i+j)%%2==0){
        x<-1
      }
      else{
        x<--1
      }
      C[i,j]<-x*det(A[-i,-j])
    }
  }
  CT<-t(C)
  A1<-(1/det(A))*CT
  return(A1)
}

A<-matrix(c(2,1,5,6,4,9,2,2,0),ncol=3)
b<-c(0,0,1)
x<-solve_by_crammersrule(A,b)
print("x is ")
print(x)
A1<-inverse(A)
print("Inverse of A is ")
print(A1)
print("A * inverse of A is Identity matrix")
I<-A%*%A1
print(I)
#The  answer may slightly vary due to  rounding off values


library(data.table)
library(ggplot2)
library(gtools)

data.dir <- "D:/Project/Statistical-Computing/datasets/"

### Beer/Wings Consumption

Beerwings <- data.table(read.csv(paste0(data.dir, "Beerwings.csv"),
                                 header = T))

with(Beerwings, {
  tapply(Hotwings, Gender, mean)
})

observed <- 14.5333 - 9.3333 # store observed mean difference

hotwings <- Beerwings$Hotwings

N <- 10^5 - 1

result <- numeric(N)

for( i in 1:N)
{ #sample of size 15, from 1 to 30, without replacement
  index <- sample(30, size = 15, replace = F)
  result[i] <- mean(hotwings[index]) - mean(hotwings[-index])
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Beer/Wings Resampling")

p <- (sum(result >= observed) + 1) / (N + 1) # p-value
v <- p*(1 - p) / N # variance of the samples

### Verizon Repair Times

Verizon <- data.table(read.csv(paste0(data.dir, "Verizon.csv"),
                                 header = T))

with(Verizon, {
  tapply(Time, Group, mean)
})

Time.CLEC <- Verizon[Group == "CLEC"]$Time
Time.ILEC <- Verizon[Group == "ILEC"]$Time
Time <- c(Time.CLEC, Time.ILEC)

observed <- mean(Time.ILEC) - mean(Time.CLEC) # store the observed mean difference

N <- 10e4 - 1

result <- numeric(N)

sample.size <- length(Time.ILEC) + length(Time.CLEC)

for( i in 1:N)
{
  index <- sample(sample.size, length(Time.ILEC), replace = F)
  result[i] <- mean(Time[index]) - mean(Time[-index])
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Verizon Repair Times - Resampling")

p <- ( sum(result <= observed) + 1 ) / ( N + 1 ) # p-value
p * 100

v <- p*(1 - p) / N # variance of p

### Verizon, Median

with(Verizon, {
  tapply(Time, Group, median)
})

observed <- median(Time.ILEC) - median(Time.CLEC)

N <- 10e4 - 1

result <- numeric(N)
sample.size <- length(Time.ILEC) + length(Time.CLEC)

for( i in 1:N)
{
  index <- sample(sample.size, length(Time.ILEC), replace = F)
  result[i] <- median(Time[index]) - median(Time[-index])
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Verizon Repair Times - Resampling")

p <- ( sum( result <= observed ) + 1) / ( N + 1)
p * 100
v <- p*(1 - p)/N

### Verizon, Trimmed Mean

with(Verizon, {
  tapply(Time, Group, function(x) { mean(x, trim = .25)})
})

observed <- mean(Time.ILEC, trim = .25) - mean(Time.CLEC, trim = .25)

N <- 10e4 - 1

result <- numeric(N)
sample.size <- length(Time.ILEC) + length(Time.CLEC)

for( i in 1:N)
{
  index <- sample(sample.size, length(Time.ILEC), replace = F)
  result[i] <- mean(Time[index], trim = .25) - mean(Time[-index], trim = .25)
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Verizon Repair Times - Resampling")

p <- ( sum( result <= observed ) + 1) / ( N + 1)
p * 100
v <- p*(1 - p)/N

### Verizon, repair time differences

observed <- mean(Time.ILEC > 10) - mean(Time.CLEC > 10)
observed

N <- 10e4 - 1

result <- numeric(N)
sample.size <- length(Time.ILEC) + length(Time.CLEC)

for( i in 1:N)
{
  index <- sample(sample.size, length(Time.ILEC), replace = F)
  result[i] <- mean(Time[index] > 10) - mean(Time[-index] > 10)
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Verizon Repair Times - Resampling")

p <- ( sum( result <= observed ) + 1) / ( N + 1)
p * 100
v <- p*(1 - p)/N

### Verizon, repair time varances

observed <- var(Time.ILEC) - var(Time.CLEC)
observed

N <- 10e4 - 1

result <- numeric(N)
sample.size <- length(Time.ILEC) + length(Time.CLEC)

for( i in 1:N)
{
  index <- sample(sample.size, length(Time.ILEC), replace = F)
  result[i] <- var(Time[index]) - var(Time[-index])
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Verizon Repair Times - Resampling")

p <- ( sum( result <= observed ) + 1) / ( N + 1)
p * 100
v <- p*(1 - p)/N

### Recidivism Data

Recid <- data.table(read.csv(paste0(data.dir, "Recidivism.csv"),
                             header = T))

k <- complete.cases(Recid$Age25)

Recid2 <- Recid[k]
Recid2[, .(Recid = sum(Recid == "Yes"), Pct = sum(Recid == "Yes") / .N), by = Age25]

observed <- .365 - 0.306
N <- 10e4 - 1

result <- numeric(N)

RecidR <- ifelse(Recid2$Recid == "Yes", 1, 0)

for(i in 1:N)
{
  index <- sample(nrow(Recid2), 3077, replace = F)
  result[i] <- mean(RecidR[index]) - mean(RecidR[-index])
}

2 * (sum(result >= observed) + 1) / (N + 1)

### Diving Scores

Diving <- data.table(read.csv(paste0(data.dir, "Diving2017.csv"),
                              header = T))

Diff <- Diving$Final - Diving$Semifinal # Difference in two scores
observed <- mean(Diff)                  # mean of differences

N <- 10e5-1
result <- numeric(N)

for(i in 1:N)
{
  Sign <- sample(c(-1,1), 12, replace = T) # Random vector of 1's or -1's
  Diff2 <- Sign*Diff # random pairs (a-b) -> (b-a)
  result[i] <- mean(Diff2)
}

ggplot(data.table(result), aes(result)) +
  geom_histogram(aes(fill = ..count..)) +
  geom_vline(xintercept = observed, col = "darkorange", linetype = 3, lwd = 1.3) +
  scale_y_continuous(labels = comma) +
  labs(title = "Diving Times - Resampling")

p <- 2 * (sum(result >= observed + 1)) / (N + 1)
v <- p*(1 - p) / N

# Table 3.1

drug.t <- c(30, 25, 20)
drug.c <- c(18, 21, 22)

choose(6, 3)

drug <- c(drug.t, drug.c)

treatment <- combinations(n = 6, r = 3, drug, repeats.allowed = F)

control <- matrix(nrow = 20, ncol = 3)
for( i in 1:nrow(c))
{
  control[i,] <- drug[!drug %in% treatment[i,]]
}
control

perms <- data.table(cbind(treatment, control))

stopifnot(nrow(perms) == choose(6, 3))

colnames(perms) <- c("D1", "D2", "D3", "C1", "C2", "C3")

perms$Xd <- (perms$D1 + perms$D2 + perms$D3) / 3
perms$Xc <- (perms$C1 + perms$C2 + perms$C3) / 3
perms$Diff <- round(perms$Xd - perms$Xc, 2)

perms[Diff >= 4.67]


# Example : 6.3B   Chapter : 6.3     Page No: 322
# Eigen Values and Eigen vectors of A
A<-matrix(c(-2,1,0,1,-2,1,0,1,-2),ncol=3)
eigenvalues<-eigen(A)$values
x<-eigen(A)$vectors
print("Eigen values of A are ")
print(eigenvalues)
print("Eigen vectors of A in normalised form")
print(x)
#to get eigen vectors in the textbook multiply normalised vectors by scalars
x[,1]<-x[,1]*(1/x[1,1])
x[,2]<-x[,2]*(1/x[1,2])
x[,3]<-x[,3]*(1/x[1,3])
print("Eigen vectors with respect to the above eigen values respectively are")
print(x)
#The  answer may slightly vary due to  rounding off values
#The answers provided in the text book may vary because of the computation process
#Both answers are correct , here it is taken -Ax+b=0 , In the text book it is considered as Ax-b=0


# fit multiple regression in R

brain <- read.table('brain.txt',as.is=T, header=T)

brain$loggest <- log(brain$gest)
brain$logbrain <- log(brain$brain)
brain$logbody <- log(brain$body)
brain$loglitter <- log(brain$litter)

brain.lm <- lm(logbrain ~ logbody + loglitter + loggest, data=brain)

summary(brain.lm)
# print out coefficients, se's, individual tests, and other info

# need to use explicit model comparison to construct F tests

brain.lm2 <- lm(logbrain ~ logbody  + loggest, data=brain)
 # model with loglitter = 0

anova(brain.lm2,brain.lm)
 # gives F test of loglitter = 0

# can also get F test for each term using drop1()
drop1(brain.lm, .~., test='F')

brain.lm3 <- lm(logbrain ~ logbody, data=brain)
 # model with log gest= 0 and loglitter = 0
anova(brain.lm3,brain.lm)

brain$littgest <- brain$loglitter - brain$loggest
brain.lm4 <- lm(logbrain ~ logbody + littgest, data=brain)
 # model with loglitter + loggest = 0
 # reasoning behind littgest described in lecture

anova(brain.lm4,brain.lm)

# to test loggest + loglitter = 1, need to expand prev. approach
#  substituting the constraint and simplifying, you see you need
#  Y = logbrain - loggest

brain$braingest <- brain$logbrain - brain$loggest
brain.lm5 <- lm(braingest ~ logbody + littgest, data=brain)

# can't use anova() because Y has changed.
#  could compare error SS by hand

# can fit desired model, keeping logbrain as response by
#  specifying loggest as an offset (a fixed constant)

brain.lm5 <- lm(logbrain ~ logbody + littgest, offset = loggest, data=brain)
anova(brain.lm5)

# same as SAS test3 from proc reg.

# N.B. if H0 was something like loggest + loglitter = 2, would need
#  compute a new variable with 2*loggest, can not specify a formula
#  as an offset

# residuals from lm()
#  obtained as before

brain.resid <- resid(brain.lm)

# to get lag residuals, use appropriate indexing
n <- length(brain.resid)

plot(brain.resid[1:(n-1)], brain.resid[2:n])
# index deletion is a bit shorter but more opaque
# plot(brain.resid[-n], brain.resid[-1])

# can get just the lag-1 correlation
cor(brain.resid[-n], brain.resid[-1])

# or a bunch of  lag correlations,
#  what is called an autocorrelation function
acf(brain.resid)

# demonstration of partial regression
brain.plm1 <- lm(logbrain~logbody + loglitter, data=brain)
brain.plm2 <- lm(loggest~logbody + loglitter, data=brain)

brain.plm3 <- lm(resid(brain.plm1) ~ -1 + resid(brain.plm2) )

summary(brain.plm3)

RFrontEndSolution/Tests Repo/Rscripts All (163 files)/brain.r


mle <- function(x) max(x) # W1
unbiased <- function(x) mle(x) * (n+1)/(n) # W2, unbiased
moments <- function(x) 2*mean(x)
f_M <- function(t) n/theta * (t/theta)^(n-1)
f_M <- Vectorize(f_M)
#

theta <- pi^2/6
n <- 10
M <- 10000
###########

data.sets <- matrix(runif(n = n*M, min = 0, max = theta), ncol = n, nrow = M)

##### MLE
W1 <- apply(data.sets, 1, mle)
hist(W1, probability = TRUE)
mean(W1)
n/(n+1) * theta ## the actual E[W1]
var(W1)
c <- n/(n+1)
( VarW1 <- (n/(n+2) - c^2)*theta^2   )
( MSE1 <- mean((theta-W1)^2) )
(2/((n+1)*(n+2)))*theta^2 ## true MSE1

##### Unbiased

W2 <- apply(data.sets, 1, unbiased)
hist(W2, probability = TRUE)
mean(W2)
theta ## the actual E[W2]
var(W2)
( VarW2 <- (1/(n*(n+2)))*theta^2   )
( MSE2 <- mean((theta-W2)^2) )

#####

W3 <- apply(data.sets, 1, moments)
hist(W3, probability = TRUE)
mean(W3)
theta ## the actual E[W2]
var(W3)
( VarW3 <- theta^2/(3*n)   )
( MSE3 <- mean((theta-W3)^2) )

###

MSE1
MSE2
MSE3


# Example : 6.1B   Chapter : 6.1     Page No: 292
# Eigen values and eigen vectors
solution<-function(A){
  sol<-eigen(A)
  lambda<-round(sol$values)
  x<-sol$vectors #these are normalised eigen vectors
  #to get eigen vectors in text book multiply normalised eigen vectors with scalars
  x[,1]<-x[,1]*(1/x[1,1])
  x[,2]<-round(x[,2]*(1/x[1,2]))
  x[,3]<-x[,3]*(1/x[1,3])
  print("The eigen values of the matrix are")
  print(lambda)
  print("The eigen vectors of the matrix respective to above eigen values are")
  print(x)
}

A<-matrix(c(1,-1,0,-1,2,-1,0,-1,1),ncol=3)
solution(A)
#The  answer may slightly vary due to  rounding off values


library(invgamma)

computa_emv <- function(x){
  1/mean(x)
}
alpha_emv <- function(x, c){
 exp(-c*x)
}

theta0 <- 2
cc <- 1
alpha.vdd <- exp(-cc*theta0)
M <- 10000
n <- 100

amostras <- matrix(NA, ncol = n, nrow = M)
for (j in 1:M){
  amostras[j, ] <- rexp(n = n, rate = theta0)
}

### Estimating theta
EMVs <- apply(amostras, 1, computa_emv)

mean(EMVs)
n/(n-1) * theta0

theo_eqm <- function(n, theta){
  theta^2 * (n^2+ n -2)/((n-1)^2*(n-2))
}
mean((EMVs-theta0)^2)
theo_eqm(n = n, theta = theta0)

var(EMVs)
(mean(EMVs)-theta0)^2
1/(n * theta0^2)

hist(EMVs, probability = TRUE)
abline(v = theta0, lwd = 2, lty = 2)
curve(invgamma::dinvgamma(x, shape = n, rate = n*theta0),
      min(EMVs), max(EMVs), add = TRUE, lwd = 2)

## Estimating of alpha
alpha.EMVs <- sapply(EMVs, alpha_emv, c = cc)
hist(alpha.EMVs)
abline(v = alpha.vdd, lwd = 2, lty = 2)

mean((alpha.EMVs-alpha.vdd)^2)
var(alpha.EMVs)
(mean(alpha.EMVs)-alpha.vdd)^2

(alpha.vdd * log(alpha.vdd)^2)/(n *cc)


#unsupervised learning

## Dimension reduction with pca
data("iris")
X = iris[,-5]
Y = as.numeric(iris$Species)

install.packages("class")
library(class)

Y = predict(pca)[,1:2]
# rhis is equivalent to these actions:
U = pca$loadings[,1:2]
dim(U)
Y = as.matrix(X) %*% U
dim(Y)

X[1,]
Y[1,]

plot(Y,col=cls)

# another example of PCA with decathlon data

#let's first load data from internet:

decathlon = read.table("http://math.agrocampus-ouest.fr/infoglueDeliverLive/digitalAssets/108286_decathlon.csv",header=TRUE,sep=';')

dim(decathlon)

# let's now focus on athlete's performance data
X = decathlon[,2:11] # we focus on the data that is related to competitions
rownames(X) = decathlon$X

#exercise: perform a PCA and visualize the data in 2-dimentional space and
#identify the most performant athlets.

pca = princomp(X)
biplot(pca)

# we can guess that Serble or Clay are the most performant athlets
Y=predict(pca)[,1:2]

#coordinates of the 1st individual in the original space (X)
X[1,]

# coordinates of the 1st individual in the projection space (Y = X*U)
Y[1,]

plot(Y, type='n')

text(Y, labels = rownames(X))

#selection of the numbers of axes to retrain
# one of the rules is to retrain the nb of axes s.t. the percentage of explained
#variance is larger than 90%

summary(pca)

#here 2 PCA components is enough to explain 93% of the variance
#the other rule is to look for a break in the eigenvalue scree:

screeplot(pca)
# on this example perhaps we would prefer $d=3$.

#############

#Kmeans and hierarchical clustering
#In order to highlight the main feautures of kmeans and HC, let's consider the
# 'swiss' data
data(swiss)
?swiss
#let's first try to apply HC on this data. A general call to HC in R can be done
#as follows:
library(class)
?hclust()

D = dist(swiss)
hc = hclust(D, method = "complete")
plot(hc)
#remark: we observe here that 'hclust' can be applied to any type of data
# if we are able to compute the distance for this data. Conversly, kmeans is mostly
#restricted to continuous data due to the need to compute centers

hc_centroid = hclust(D, method = "centroid")
plot(hc_centroid)

hc_ward = hclust(D, method = "ward.D")
plot(hc_ward)

#remark: we can see here that the different methods lead to quite different
#dendogram, leading in turn to different choices of numbers of clusters

#let's remember that clustering aims to help the analyst to understand data.
#The choice of a method should be done according to this idea.

cl4 = cutree(hc_ward, k=2)
cl4

plot(swiss,col=cl4,pch=19)

clcompl = cutree(hc, k=2)
clcompl
plot(swiss,col=clcompl, pch=19)

clcentroid = cutree(hc_centroid, k=2)
clcentroid
plot(swiss,col=clcentroid,pch=19)

# Kmeans

# let's have a look at the same data with Kmeans

?kmeans
#a basic call to kmeans would be

data(swiss)
out = kmeans(swiss, centers =2)
out

#ex: write a short script that plot the curve of J(k) for the dataset at hand

J = rep(NA, 15)
for (k in 1:15){
  out = kmeans(swiss, centers=k)
  J[k] = out$betweenss/out$totss
  }
plot(J, type='b')

#we observe on this small script that the curve can change with different initialization
#and we can even observe some non-increasing curves for 'unlucky' initialization
# a way to avoid this init effect we can average on several runs for each value of k

J = matrix(NA, 10, 15)
for (k in 1:15){
  for(l in 1:10){
    out = kmeans(swiss, centers=k)
    J[l,k] = out$betweenss/out$totss
  }
}
boxplot(J,type='b')

# on this example I'd probably pick $k^*=5$ as the most appropriate nb of clusters
#ex: visualize the clustering result with a pair plot, and compare to the one
#obtained with hclust

out5 = kmeans(swiss, centers= 5, nstart =10)
plot(swiss, col=out5$cluster,pch=19)

# The GMM and the EM algorithm

# we use 'mclust' packsge:
install.packages("mclust")
library(mclust)

#the help of the pkg is available as usual:
?Mclust
# a basic call can be performed as follows:
data(swiss)
out_m = Mclust(swiss, G =3)
out_m

summary(out_m)

out_m$parameters

out_m$classification


#  Artificial Intelligence for Humans
## Volume 1: Fundamental Algorithms
## R Version
## http://www.aifh.org
## http://www.jeffheaton.com
##
## Code repository:
## https://github.com/jeffheaton/aifh
##
## Copyright 2013 by Jeff Heaton
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## For more information on Heaton Research copyrights, licenses
## and trademarks visit:
## http://www.heatonresearch.com/copyright

## Chapter 4 Example: Inscribed Circle
##
## This example shows how random numbers can be used to estimate pi
## using random sampling (Monte Carlo method)
##
## Area of a circle = pi/(r^2)
## Area of a square = (2r)^2
## circle/square = (pi/(r^2))/(2r)^2 = pi/4

estimate_pi <- function(samples, r){
	tries <- 0
	successes <- 0
	for (i in 1:samples){

		# pick a point randomly
		x = r*runif(1)
		y = r*runif(1)
		tries <- tries + 1
		# check if point is in the circle
		if (x*x + y*y <= r*r){
			successes <- successes + 1
		}
	}
	pi_est <- 4*successes/tries
	return(pi_est)
}

## After a million samples, Monte Carlo comes pretty close!
## Returns: 3.144756 (but will be different unless seed is the same)
print(estimate_pi(1000000,4))


##########  Function to calculate the Bootstrapped Distribuition  ##########

new.boot = function(data,func.name,R)
{
  t = numeric(R)
  n = length(data)
  for(i in 1:R)
  {
    indices = sample(1:n,n,replace=T)
    t[i] = func.name(data,indices)
  }
  t0 = func.name(data,1:n)
  weights = numeric(n)
  for(i in 1:n)
  {
    weights[i] = 1/n
  }
  print(paste0("ORDINARY NONPARAMETRIC BOOTSTRAP"))
  print(paste0("Original: ",(t0)))
  print(paste0("Bias: ",format(round((mean(t)-t0),4))))
  print(paste0("Std. error: ",format(round((sd(t)),4))))
  return(list(t0=t0,t=t,R=R,data=data,statistic=func.name,weights=weights))
}

##########  Function to find the Confidence Intervels  ##########

new.boot.ci = function(x)
{
  conf = 0.95
  alpha = 1 - conf
  N = c(x[[1]]-(mean(x[[2]])-x[[1]])-qnorm(1-(alpha/2))*sd(x[[2]]),
        x[[1]]-(mean(x[[2]])-x[[1]])-qnorm((alpha/2))*sd(x[[2]]))
  l = ((x[[3]]+1)*(alpha/2))
  u = ((x[[3]]+1)*(1-(alpha/2)))
  P = sort(x[[2]])[c(l,u)]
  B = c(2*x[[1]]-sort(x[[2]])[c(u)],2*x[[1]]-sort(x[[2]])[c(l)])
  print(paste0("Normal approximation: (",format(round(N[1],4)),"  ",format(round(N[2],4)),")"))
  print(paste0("Basic approximation: (",B[1],"  ",B[2],")"))
  print(paste0("Percentile approximation: (",P[1],"  ",P[2],")"))
  return(list(R=x[[3]],t0=x[[1]],Normal=(N),Basic=(B),Percentile=(P)))
}

########## Function to calculate the 90-th percentile of a samlpe data ##########

ninty.npar = function(x,indices)
{
  result = quantile(x[indices],prob=0.90,type=1)
  return(result)
}

# To compute the 90-th percentile from the draws
# First we load the given data and store it into variable
cpu = scan(file="cputime.txt")

# we call the new.boot function that gives the bootstrap draws with
#           bias and standard deviation of the draws
ninty.npar.boot = new.boot(cpu, ninty.npar, 999)

# to get the confidence intervals of the draws call the new.boot.ci function
#       with the result stored in 'ninty.npar.boot' variable
conf = new.boot.ci(ninty.npar.boot)


xbar <- 8.307849
s2bar <- 7.930452 ## 1/n * Delta^2; Delta^2 = sum((x-xbar)^2)
n <- 1000

gama <- .95
cc <- qt(p = (1  + gama)/2,
         df = n-1) ## inv_cdf_T_{n-1}((1 + gamma)/2)
sigma.prime <- sqrt(n*s2bar/(n-1))
d <- cc * sigma.prime/sqrt(n)

a <- xbar - d
b <- xbar + d

c(a, b)
c(xbar, a, b)

### Supondo que sigma é conhecida
truesd <- 2

# (xbar - mu)/sqrt(sigma^2/n) ~ N(0, 1)

# Pr( -k < Z < k) = gamma
# Pr( xbar - k*sigma/sqrt(n) < mu < xbar + k*sigma/sqrt(n)) = gamma

# a > 0
# 2*Phi(k)-1 = gamma => a = Phi_inv((1+gamma)/2)

k <- qnorm(p = (1  + gama)/2, mean = 0, sd = 1)

d2 <- k*truesd/sqrt(n)
a2 <- xbar - d2
b2 <- xbar + d2

c(xbar, a, b)
c(xbar, a2, b2)

# Função poder

mu0 <- 7

poder_pamonha <- function(mu){
  x <- sqrt(n) * (mu0 + cc - mu)/truesd
  y <- sqrt(n) * (mu0 - cc - mu)/truesd
  res <- pnorm(x, mean = 0, sd = 1,
               lower.tail = FALSE) +
    pnorm(y, mean = 0, sd = 1)
  return(res)
}
poder_pamonha <- Vectorize(poder_pamonha)

curve(poder_pamonha, 0, 14,
      xlab = expression(mu),
      ylab = expression(pi(mu*"|"*delta)), lwd = 3)
abline(v = mu0, lty = 2, lwd = 2)

### Testando hipóteses (APG)
#### Unilateral
## H0: mu >= 7
## H1: mu < 7
### U > c => reijeito H0
### U < c => não rejeito H0
# X ~n(mu0, sigma^2)
U <- (sqrt(n) * (xbar-mu0))/sigma.prime
## Se H0 = vdd então U ~ T(n-1)
( p.valor <- pt(q = U, df = n-1) ) ## T^{-1}(U; n-1)

## install.packages("BSDA")
BSDA::tsum.test(mean.x = xbar,
                s.x = sigma.prime,
                n.x = n,  mu = mu0,
                alternative = "less")

BSDA::tsum.test(mean.x = xbar,
                s.x = sigma.prime,
                n.x = n,  mu = mu0)


# Group members (Name, Student ID, E-Mail):
# 1. Baldomero Valdez,  Valenzuela, 2905175, baldmer.w@gmail.com
# 2. Omar Trinidad Gutierrez Mendez, 2850441, omar.vpa@gmail.com
# 3. Shinho Kang, 2890169, wis.shinho.kang@gmail.com

#=========================================
# TASK 1
#=========================================
sample.mean <- 4
sample.sd <- 0.7
n <- 5

# a) Compute the standard error of the mean
se <- sample.sd / sqrt(n)
# => se : 0.3130495

# b) Compute the 95% confidence interval
# 2.5% quantile of t-distribution with df n-1
t.qt <- qt(0.025, n-1)
interval <- c(sample.mean + se*t.qt, sample.mean - se*t.qt)
size <- interval[2] - interval[1]
# => interval: [3.130835, 4.869165]
# => size: [1.73833]

# c) half the size of the confidence interval
n2 <- 12 # or 13
se2 <- sample.sd / sqrt(n2)
t.qt2 <- qt(0.025, n2-1)
interval2 <- c(sample.mean + se2*t.qt2, sample.mean - se2*t.qt2)
size2 <- interval2[2] - interval2[1]
# => interval2: [3.555241, 4.444759]
# => size2: 0.8895176 (when n=12)
# => size2: 0.8460115 (when n=13)