Repository navigation
Expand file tree
/
Copy path3_plot_AR_function.R
More file actions
152 lines (117 loc) · 6.15 KB
/
Copy path3_plot_AR_function.R
File metadata and controls
152 lines (117 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
plot_AR=function(beta_left,beta_right,AR_sim_coef,AR_obs_coef){
#for presentation
beta_grid=seq(beta_left,beta_right,0.01)
#calculate quantile
alpha_quantile=sapply(beta_grid,find_quantile_inner_value,AR_sim_coef,alpha=0.34)
#simulated AR functions
curves=matrix(0,nsim_permu,length(beta_grid))
#realized AR functions
curves0=rep(0,length(beta_grid))
for (i in 1:length(beta_grid)){
beta = beta_grid[i]
numerator=t(AR_sim_coef[1:3,]) %*% c(beta^2, beta,1 )
denominator=t(AR_sim_coef[4:6,]) %*% c(beta^2, beta,1 )
#curves
curves[,i]=numerator/denominator
#realized AR functions
curves0[i]=(AR_obs_coef[1]*beta^2+AR_obs_coef[2]*beta+AR_obs_coef[3])/(AR_obs_coef[4]*beta^2+AR_obs_coef[5]*beta+AR_obs_coef[6])
}
curves = log(curves)
curves0 = log(curves0)
alpha_quantile=log(alpha_quantile)
beta_grid = log(abs(beta_grid)+1) * sign(beta_grid)
#create data frames for plotting
df1=data.frame(beta_grid=beta_grid,alpha_quantile=alpha_quantile) #the quantiles
df2=data.frame(beta_grid=beta_grid,obs_AR=curves0) #the observed AR
df3=cbind(beta_grid,t(curves)) #the simulated AR
df3=as.data.frame(df3)
#create intersections
intersections = create_intersection(AR_sim_coef)
intersections = log(abs(intersections)+1) * sign(intersections)
#df3=data.frame(beta_grid=beta_grid,permu_AR=permu_AR)
ggplot()+
geom_line(data=df2,aes(x=beta_grid,y=curves0),color='blue',linewidth=5)+
geom_line(data=df3,aes(x=beta_grid,y=curves[1,]),color='orange', linewidth=2)+
geom_line(data=df3,aes(x=beta_grid,y=curves[2,]),color='green',linewidth=2)+
geom_line(data=df3,aes(x=beta_grid,y=curves[3,]),color='purple',linewidth=2)+
geom_line(data=df1,aes(x=beta_grid,y=alpha_quantile),color='red',linewidth=5)+
ylim(-10,10)+
geom_vline(xintercept=intersections,color='black')+
xlab('log(abs(beta)+1)*sign(beta)')+
ylab('log(AR^2)')+
annotate("text", x = 2, xend = 2.5, y = -6.5, yend = -5, colour = "black",label="AR_sim1",size=10)+
annotate("text", x = 2, xend = 2.5, y = -1.5, yend = -0.5, colour = "black",label="AR_sim2",size=10)+
annotate("text", x = 2, xend = 2.5, y =0.5, yend = -6.5, colour = "black",label="AR_sim3",size=10)+
annotate("text", x = -2.2, xend = -1, y = 2.6, yend = 3, colour = "black",label="AR_obs",size=10)
ggsave('AR_plot.png',width=20,height=10)
}
plot_AR2=function(beta_left,beta_right,AR_sim_coef,AR_obs_coef){
beta_grid=seq(beta_left,beta_right,0.01)
#calculate quantile
alpha_quantile=sapply(beta_grid,find_quantile_inner_value,AR_sim_coef,alpha=0.25)
#simulated AR functions
curves=matrix(0,nsim_permu,length(beta_grid))
#realized AR functions
curves0=rep(0,length(beta_grid))
for (i in 1:length(beta_grid)){
beta = beta_grid[i]
numerator=t(AR_sim_coef[1:3,]) %*% c(beta^2, beta,1 )
denominator=t(AR_sim_coef[4:6,]) %*% c(beta^2, beta,1 )
#curves
curves[,i]=numerator/denominator
#realized AR functions
curves0[i]=(AR_obs_coef[1]*beta^2+AR_obs_coef[2]*beta+AR_obs_coef[3])/(AR_obs_coef[4]*beta^2+AR_obs_coef[5]*beta+AR_obs_coef[6])
}
curves = log(curves)
curves0 = log(curves0)
alpha_quantile=log(alpha_quantile)
beta_grid = log(abs(beta_grid)+1) * sign(beta_grid)
#create data frames for plotting
df1=data.frame(beta_grid=beta_grid,alpha_quantile=alpha_quantile) #the quantiles
df2=data.frame(beta_grid=beta_grid,obs_AR=curves0) #the observed AR
df3=cbind(beta_grid,t(curves)) #the simulated AR
df3=as.data.frame(df3)
#create intersections
intersections = create_intersection(AR_sim_coef)
intersections = sort(log(abs(intersections)+1) * sign(intersections))
ei <- as.character(expression('e'[i]))
Vi1 <- as.character(expression('V'[i]))
ei1 <- as.character(expression('e'[i]['+'][1]))
Si1 <- as.character(expression('S'[i]))
unit<-"beta" #note double quotes
xlab <- bquote('log(abs('*.(as.name(unit))*')+1)*sign('*.(as.name(unit))*')')
ylab <- bquote('log('~AR^2~')') #built expression as string
#df3=data.frame(beta_grid=beta_grid,permu_AR=permu_AR)
ggplot()+
geom_line(data=df2,aes(x=beta_grid,y=curves0),color='black',linewidth=1.5,linetype='dotdash')+
geom_line(data=df3,aes(x=beta_grid,y=curves[1,]),color='black', linewidth=1.5,linetype = "dashed")+
geom_line(data=df3,aes(x=beta_grid,y=curves[2,]),color='black',linewidth=1.5,linetype = "dashed")+
geom_line(data=df3,aes(x=beta_grid,y=curves[3,]),color='black',linewidth=1.5,linetype = "dashed")+
geom_line(data=df3,aes(x=beta_grid,y=curves[4,]),color='black',linewidth=1.5,linetype = "dashed")+
geom_line(data=df1,aes(x=beta_grid,y=alpha_quantile),color='black',linewidth=1.5)+
ylim(-5.1,5.1)+
xlim(-2,2)+
geom_vline(xintercept=intersections[6:9],color='black',linewidth=1.5)+
xlab(xlab)+
ylab(ylab)+
annotate("text", x = 2, xend = 2.5, y = -5, yend = -5.5, colour = "black",label="AR_sim1",size=12)+
annotate("text", x = 2, xend = 2.5, y = -1.2, yend = -0.5, colour = "black",label="AR_sim2",size=12)+
annotate("text", x = 2, xend = 2.5, y =0.5, yend = -6.5, colour = "black",label="AR_sim4",size=12)+
annotate("text", x = 2, xend = 2.5, y = -0, yend = 3, colour = "black",label="AR_sim3",size=12)+
annotate("text", x = 2, xend = 2.5, y = 1.5, yend = 3, colour = "black",label="AR_obs",size=12)+
geom_text(aes(x=intersections[6]-0.05, label=ei, y=-5.1), colour="black",size=10,parse=T)+
geom_text(aes(x=intersections[7]-0.05, label=Vi1, y=-5.1), colour="black",size=10,parse=T)+
geom_text(aes(x=intersections[8]-0.05, label=Si1, y=-5.1), colour="black",size=10,parse=T)+
geom_text(aes(x=intersections[9]-0.05, label=ei1, y=-5.1), colour="black",size=10,parse=T)+
theme_bw()+
theme(
axis.title.x = element_text(size = 50),
axis.title.y = element_text(size = 50),
panel.border = element_rect(colour = "black", fill = NA, linewidth = 1.5)
)+ theme(axis.text = element_blank())+ theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
ggsave('AR_plot2.png',width=4.5*5,height=4.5*4)
#ggsave('AR_plot2.png',width = 5, height = 4, units = "in")
}