install.packages("learnr")
install.packages("tidyverse")
rm(list = ls());
graphics.off();
options("width"=300);
x <- c(1,0.02,0.05,-0.09,0.08,-0.02,0.00,0.12,0.06,0.02,-0.08);
a <- acf(x=rnorm(400),lag.max=10,type="correlation",plot=F);
a$acf[,,1] <- x;
par(mfrow=c(1,1));
par(oma=c(0,2,4,2));
plot(x=a,xlab=expression(paste("lag ",k,sep="")),ylab=expression(paste("acf(",k,")",sep="")),main="Correlogram",col="green4",main="");
legend(x="topright",legend=c("acf","95% confidence bands "),col=c("green4","blue"),lty=c(1,2));
mtext(text="Correlogram",side=3,line=0,outer=T);
par(mfrow=c(1,1));
par(oma=c(0,2,4,2));
plot(x=a,xlab=expression(paste("lag ",k,sep="")),ylab=expression(paste("acf(",k,")",sep="")),main="Correlogram",col="green4",main="");
legend(x="topright",legend=c("acf","95% confidence bands "),col=c("green4","blue"),lty=c(1,2));
par(mfrow=c(1,1));
par(oma=c(0,2,4,2));
plot(x=a,xlab=expression(paste("lag ",tau,sep="")),ylab=expression(paste("acf(",tau,")",sep="")),col="green4",main="");
par(mfrow=c(1,1));
par(oma=c(0,2,4,2));
plot(x=a,xlab=expression(paste("lag ",tau,sep="")),ylab=expression(paste("acf(",tau,")",sep="")=col="green4",main="");
legend(x="topright",legend=c("acf","95% confidence bands "),col=c("green4","blue"),lty=c(1,2));
mtext(text="Correlogram",side=3,line=0,outer=T);
par(mfrow=c(1,1));
par(oma=c(0,2,4,2));
plot(x=a,xlab=expression(paste("lag ",tau,sep="")),ylab=expression(paste("acf(",tau,")",sep="")=col="green4",main="");
legend(x="topright",legend=c("acf","95% confidence bands "),col=c("green4","blue"),lty=c(1,2));
mtext(text="Correlogram",side=3,line=0,outer=T);
2207 + 1550 + 1125 + 322 + 148 + 120 + 117 + 56 + 29
Sys.getenv()
Sys.setenv(LANG = "en")
q()
q()
n
1 + 0.25
(1 - 0.5^4)/(1-0.5^2)
(1 - 0.5^8)/(1-0.5^2)
1.25 + 0.5^3
1.25 + 0.5^4
1 + 0.5^2 + 0.5^4
(1 - 0.5^3)/(1-0.5^2)
(1 - 0.5^6)/(1-0.5^2)
load("class.RData")
q()
help("poly.root")
help("polyroot")
polyroot(c(1,-0.8,-0.15,0.09,-0.15))
x=polyroot(c(1,-0.8,-0.15,0.09,-0.15))
abs(x)
x=polyroot(c(1,-0.8,-0.35,0.15,-0.02))
abs(x)
x=polyroot(c(1,-0.8,-0.35,0.15,0.15))
abs(x)
x=polyroot(c(1,-0.8,-0.35,0.15,0.15))
abs(x)
x=polyroot(c(1,-0.8,-0.35,0.15,0.2))
abs(x)
x=polyroot(c(1,-0.8,-0.35,0.15,-0.2))
abs(x)
x=polyroot(c(1,-0.8,-0.35,0.15,0.2))
x
900 + 1800 + 3600 + 1800 + 10000 + 10500 + 15000
900 + 1800 + 3600 + 1800 + 10000 + 10500 + 15000 + 126240
900 + 1800 + 3600 + 1800 + 10000 + 10500 + 15000 + 1262.40
q()
cal.ret.c.q1
ppois(5, 1)
1-ppois(3, 6.5)
1-ppois(2, 6.5)
1-ppois(4, 6.5)
1-ppois(5, 6.5)
a = 10000 * 0.95
b = 0.05 / (999/1000)
a / (a +b)
a = (1/10000) * 0.95
b = (999/1000) * 0.05
a / (a +b)
a = (1/1000) * 0.95
a / (a +b)
x = 22.9 - 18.6
x / 1.8
x / 1.8 + 32
x * 1.8
x * 1.8 + 32
a = 10 * 0.25
b = y = 2.92
4 * a +  4 * b
2 * a + 2 * b
sqrt(4*a + 4*b)
2 * a^0.5 + 2*b^0.5
(2/3) * (b^3 - a^3) / (b^2 - a^2)
a = 1
b = 2
(2/3) * (b^3 - a^3) / (b^2 - a^2)
0.08 * 0.92 / 500
(2/3) * (b^3 - a^3) / (b^2 - a^2)
(0.15 * 0.85 / 500)
(0.15 * 0.85 / 500)^0.5
(0.15 * 0.85 / 1000)
(0.15 * 0.85 / 1000)^0.5
sqrt(120) * 0.1 / 1.645
sqrt(120) * 0.1 / 1.96
save.image()
quit()
setwd("~/Research/PhuongAnh/Code/SingleFirmPermR")
load("SingleFirmPerm.RData")
ls()
rm(list=ls())
load("SingleFirmPerm.RData")
perm.test.caar
perm.test.car
perm.test.car <- function(ar.est, ar.ev, B = 9999, stud = TRUE, one.sided = FALSE, rund = 3) {
#
# function to carry out the permuation test of Section 4.1 of the paper Nguyen and Wolf (2024)
#
# Inputs:
# ar.est:    vector of ARs in estimation window
# ar.ev:     vector of ARs in event window
# B:         number of permutations, default = 99999 (can be reduced to 9999, if needed)
# stud:      logical variable whether to use studentized test statistic, default = T
# one.sided: logical variable whether to use one-sided (upper) test. default = F
# rund:      number of decimals for rounding of reported results, default = 3
#
# returns the value of the test statistic and the corresponding p-value
#
n.est <- length(ar.est)
n.ev <- length(ar.ev)
sig <- 1
if (stud)
sig <- sqrt(var(ar.est))
t <- sqrt(n.ev) * mean(ar.ev) / sig # test statistic
count <- 1
ar.comb <- c(ar.est, ar.ev) # combined sample
n.comb <- length(ar.comb)
# can 'speed up' the routine if event window has size 1,
# since then the number of distinct permutations equals n.est
if (1 == n.ev) {
B <- n.est
for (i in (1:B)) {
ar.ev.perm <- ar.est[i]
ar.est.perm <- ar.est
ar.est.perm[i] <- ar.ev
sig.perm <- 1
if (stud)
sig.perm <- sqrt(var(ar.est.perm))
t.perm <- sqrt(n.ev) * mean(ar.ev.perm) / sig.perm
if (one.sided) {
if (t.perm >= t)
count = count + 1
}
else {
if (abs(t.perm) >= abs(t))
count = count + 1
}
}
}
# generic routine for event windows with size > 1
else {
for (i in (1:B)) {
ar.comb.perm <- sample(ar.comb, n.comb, replace = FALSE)
ar.est.perm <- ar.comb.perm[1:n.est]
sig.perm <- 1
if (stud)
sig.perm <- sqrt(var(ar.est.perm))
ar.ev.perm <- ar.comb.perm[(n.est+1):n.comb]
t.perm <- sqrt(n.ev) * mean(ar.ev.perm) / sig.perm
if (one.sided) {
if (t.perm >= t)
count = count + 1
}
else {
if (abs(t.perm) >= abs(t))
count = count + 1
}
}
}
p.value <- count / (B + 1)
# return test statistic and permutation-test p-value
output <- list(t.stat = round(t, rund), p.value = round(p.value, rund))
return(output)
}
ls()
save.image()
quit()
