-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-uebungsblatt_4.R
167 lines (130 loc) · 4.54 KB
/
2-uebungsblatt_4.R
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
######
#A
#Erzeugen Sie 4 Realisierungen (st) eines Random Walks
#der Länge N = 100 mit Drift µ = 0.01 und var = 1
#skript 2-2 bis 2-3 für White Noise und 2-8 bis 2-12 für Random Walk
#Random Walk ohne drift: EW=0
#EW(1)+EW(const=0.01)=0+0.01= RW mit drift dh. EW=0.01
#erstmal install quantmod and astsa package!!!
#Random-Walk Simulation
rm(list=ls())
par(mfrow=c(1,1), mar=c(3,2,1,0)+.5, mgp=c(1.6,.6,0)) #praparing for 4 graph
WN_d<-rnorm(100,0.01,1) # 100 Beobachtungen EW=0.01 Var=1, mit drift
WN_d
RW_d<-cumsum(WN_d)
RW_d
plot(RW_d, type="l", main="Random Walk", xlab="Perioden", ylab="")
######
#Passen Sie jeweils eine lineare Regression st = ??t + ut
#an jede Realisierung mittels der Methode der kleinsten Quadrate an.
#skript 3-12 bis 3-22
#st = bt + ut direkt mit lm()n funktion
#Plotten Sie für jede Realisierung jeweils die Daten,
#den wahren Trend (mt = 0.01t)
#und die Regressionsgerade (insgesamt 4 Bilder).
rm(list=ls())
par(mfrow=c(4,1), mar=c(3,2,1,0)+.5, mgp=c(1.6,.6,0)) #praparing for 4 graph
WN1<-rnorm(100,0.01,1) # 100 Beobachtungen EW=0.01 Var=1
WN2<-rnorm(100,0.01,1)# 100 Beobachtungen EW=0.01 Var=1
WN3<-rnorm(100,0.01,1) # 100 Beobachtungen EW=0.01 Var=1
WN4<-rnorm(100,0.01,1) # 100 Beobachtungen EW=0.01 Var=1
RW1<-cumsum(WN1)
RW2<-cumsum(WN2)
RW3<-cumsum(WN3)
RW4<-cumsum(WN4)
t<-c(1:(100))
t
plot(RW1, type="l", main="Random Walk 1", xlab="Perioden", ylab="")
fit1<-lm(RW1~t)
abline(fit1,col="2")
trend1<-0.01*(1:length(1:100))
trend1
abline(a=0,b=trend1,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
plot(RW2, type="l", main="Random Walk 2", xlab="Perioden", ylab="")
fit2<-lm(RW2~t)
abline(fit2,col="2")
trend2<-0.01*(1:length(1:100))
trend2
abline(a=0,b=trend2,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
plot(RW3, type="l", main="Random Walk 3", xlab="Perioden", ylab="")
fit3<-lm(RW3~t)
abline(fit3,col="2")
trend3<-0.01*(1:length(1:100))
trend3
abline(a=0,b=trend3,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
plot(RW4, type="l", main="Random Walk 4", xlab="Perioden", ylab="")
fit4<-lm(RW4~t)
abline(fit4,col="2")
trend4<-0.01*(1:length(1:100))
trend4
abline(a=0,b=trend4,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
summary(fit1)
summary(fit2)
summary(fit3)
summary(fit4)
AIC(fit1,fit2,fit3,fit4)
BIC(fit1,fit2,fit3,fit4)
#########################################################################
#B
#Erzeugen Sie 4 Realisierungen (xt)
#eines Trends plus Rauschen xt = 0.01t+??t
#der Länge N = 100 mit Drift µ = 0.01 und ??2
#skript 2-9
rm(list=ls())
par(mfrow=c(4,1), mar=c(3,2,1,0)+.5, mgp=c(1.6,.6,0)) #praparing for 4 graph
WN1<-rnorm(100,0,1) # 100 Beobachtungen EW=0 Var=1
WN2<-rnorm(100,0,1) # 100 Beobachtungen EW=0 Var=1
WN3<-rnorm(100,0,1) # 100 Beobachtungen EW=0 Var=1
WN4<-rnorm(100,0,1) # 100 Beobachtungen EW=0 Var=1
t<-c(1:100)
t
xt1<-0.01*t+WN1
xt2<-0.01*t+WN2
xt3<-0.01*t+WN3
xt4<-0.01*t+WN4
plot(xt1, type="l", main="Trend + Rauschen 1", xlab="Perioden", ylab="")
fit1<-lm(xt1~t)
abline(fit1,col="2")
trend1<-0.01*(1:length(1:100))
trend1
abline(a=0,b=trend1,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
plot(xt2, type="l", main="Trend + Rauschen 2", xlab="Perioden", ylab="")
fit2<-lm(xt2~t)
abline(fit2,col="2")
trend2<-0.01*(1:length(1:100))
trend2
abline(a=0,b=trend2,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
plot(xt3, type="l", main="Trend + Rauschen 3", xlab="Perioden", ylab="")
fit3<-lm(xt3~t)
abline(fit3,col="2")
trend3<-0.01*(1:length(1:100))
trend3
abline(a=0,b=trend3,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
plot(xt4, type="l", main="Trend + Rauschen 4", xlab="Perioden", ylab="")
fit4<-lm(xt4~t)
abline(fit4,col="2")
trend4<-0.01*(1:length(1:100))
trend4
abline(a=0,b=trend4,col="green")
legend("topleft",legend=c("lin. reg","Trend"),lty=rep(1,4),col=c("red","green"),cex=0.7, ncol=2)
#abline(h=1)
summary(fit1)
summary(fit2)
summary(fit3)
summary(fit4)
AIC(fit1,fit2,fit3,fit4)
BIC(fit1,fit2,fit3,fit4)