\[R_t=a_t+0.2a_{t-1},\quad\sigma_a=0.025.\] \[a_{100}=0.01\]
\[\hat{R}_{101}=E[R_{101}]=E[a_{101}+0.2a_{100}]=0.2\cdot0.01=0.002.\] \[\hat{R}_{102}=E[R_{102}]=E[a_{102}+0.2a_{101}]=0.2\cdot E[a_{101}]=0.\]
theta<-0.02
sigma_a<-0.025
r1_101 <- theta * 0.01
r1_102 <- 0
\[\mathrm{Var}(R_{101}-\hat{R}_{101})=\mathrm{Var}(a_{101})=\sigma_a^2=0.025^2=0.000625.\] \[\begin{aligned} & \mathrm{Var}(R_{102}-\hat{R}_{102})=\mathrm{Var}(a_{102}+0.2a_{101})=\sigma_a^2+0.2^2\sigma_a^2=0.025^2(1+0.2^2)=0.000625\cdot1.04= \\ & 0.00065. \end{aligned}\]
sd1_1 <- sigma_a
sd1_2 <- sqrt(sigma_a^2 * (1 + theta^2))
\[\begin{aligned} \rho_1 & =\frac{\gamma_1}{\gamma_0}=\frac{\theta^2\sigma_a^2}{\sigma_a^2(1+\theta^2)}=\frac{0.2^2}{1+0.2^2}=\frac{0.04}{1.04}\approx0.0385. \\ \rho_2 & =0. \end{aligned}\]
rho1_1 <- theta^2 / (1 + theta^2)
rho1_2 <- 0
\[r_t=0.01+0.2r_{t-2}+a_t\]
\[ E[r_t] = 0.01 \] \[\sigma_r^2=(0.2)^2Var(r_{t-2})+Var(a_t)\] \[\sigma_r^2=(0.2)^2\sigma_r^2+0.02\] \[\sigma_r^2=\frac{0.02}{1-(0.2)^2}=\frac{0.02}{0.96}=0.020833\]
avg = 0.01
avg
## [1] 0.01
sigma2_r <- 0.02 / (1 - 0.2^2)
sigma2_r
## [1] 0.02083333
\[\rho_1=0\] \[\rho_2=\frac{Cov(r_t,r_{t-2})}{\sigma_r^2}=\frac{(0.2)\sigma_r^2}{\sigma_r^2}=0.2\]
rho2_1=0
rho2_2=0.2
\[r_{101}=0.01+0.2r_{99}+a_{101}=0.01+0.2\times0.02+a_{101}=0.014+a_{101}\] \[\begin{aligned} r_{102} & =0.01+0.2r_{100}+a_{102}=0.01+0.2\times(-0.01)+a_{102}=0.008+a_{102} \end{aligned}\]
r2_101 <- 0.01 + 0.2 * 0.02
r2_101
## [1] 0.014
r2_102 <- 0.01 + 0.2 * (-0.01)
r2_102
## [1] 0.008
sd2=sqrt(0.02)
# install.packages("tidyverse")
# install.packages("forecast")
# install.packages("lubridate")
# install.packages("xts")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(lubridate)
library(xts)
## 载入需要的程序包:zoo
##
## 载入程序包:'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## 载入程序包:'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
setwd("D:/3-研究生课程/金融计量经济学/课程作业/第二次作业/")
data1 <- read.table("m-unrate.txt", header = T) #m-unrate.txt已删除2009年3月之后的数据
data1$Date <- ymd(paste(data1$Year, data1$mon, "01", sep = "-"))
head(data1)
## Year mon dd rate Date
## 1 1948 1 1 3.4 1948-01-01
## 2 1948 2 1 3.8 1948-02-01
## 3 1948 3 1 4.0 1948-03-01
## 4 1948 4 1 3.9 1948-04-01
## 5 1948 5 1 3.5 1948-05-01
## 6 1948 6 1 3.6 1948-06-01
unemployment_rate_ts <- ts(data1$rate, start = c(min(data1$Year), min(data1$mon)), frequency = 12)
plot(unemployment_rate_ts, main = "美国月度失业率 (1948-2009)", ylab = "失业率 (%)")
fit <- auto.arima(unemployment_rate_ts)
summary(fit)
## Series: unemployment_rate_ts
## ARIMA(2,1,2)(0,0,2)[12]
##
## Coefficients:
## ar1 ar2 ma1 ma2 sma1 sma2
## 1.2194 -0.3574 -1.2297 0.5344 -0.2533 -0.2267
## s.e. 0.2108 0.1913 0.1930 0.1422 0.0373 0.0402
##
## sigma^2 = 0.03723: log likelihood = 167.66
## AIC=-321.32 AICc=-321.17 BIC=-289.13
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.004729229 0.1920246 0.1417211 0.04719491 2.667621 0.1646808
## ACF1
## Training set -0.01274407
forecast_result <- forecast(fit, h = 4)
autoplot(forecast_result, main = "失业率预测 (2009年4月至7月)")
residuals <- residuals(fit)
ggAcf(residuals) + ggtitle("残差自相关图")
ggPacf(residuals) + ggtitle("残差偏自相关图")
print(forecast_result)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Apr 2009 8.836091 8.588821 9.083361 8.457924 9.214257
## May 2009 9.030093 8.682206 9.377980 8.498045 9.562141
## Jun 2009 9.159194 8.709234 9.609154 8.471040 9.847348
## Jul 2009 9.250688 8.689196 9.812180 8.391960 10.109416
library(tidyverse)
library(forecast)
setwd("D:/3-研究生课程/金融计量经济学/课程作业/第二次作业/")
data2 <- read.table("m-dec12910.txt", header = T)
dec2_returns <- data2$dec2
dec10_returns <- data2$dec10
acf_dec2 <- acf(dec2_returns, lag.max = 12, plot = FALSE)
print(acf_dec2)
##
## Autocorrelations of series 'dec2_returns', by lag
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1.000 0.114 -0.057 -0.017 0.031 0.049 -0.051 0.003 -0.049 -0.039 0.027
## 11 12
## 0.007 0.010
acf_dec10 <- acf(dec10_returns, lag.max = 12, plot = FALSE)
print(acf_dec10)
##
## Autocorrelations of series 'dec10_returns', by lag
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1.000 0.227 -0.019 -0.021 0.011 0.003 -0.028 -0.017 -0.049 -0.040 0.013
## 11 12
## 0.061 0.130
par(mfrow = c(1, 2))
acf(dec2_returns, lag.max = 12, main = "ACF of Decile 2 Returns")
acf(dec10_returns, lag.max = 12, main = "ACF of Decile 10 Returns")
lb_dec2 <- Box.test(dec2_returns, lag = 12, type = "Ljung-Box")
print(lb_dec2)
##
## Box-Ljung test
##
## data: dec2_returns
## X-squared = 14.22, df = 12, p-value = 0.2869
lb_dec10 <- Box.test(dec10_returns, lag = 12, type = "Ljung-Box")
print(lb_dec10)
##
## Box-Ljung test
##
## data: dec10_returns
## X-squared = 41.06, df = 12, p-value = 4.789e-05
fit <- auto.arima(dec2_returns)
# 输出模型摘要
summary(fit)
## Series: dec2_returns
## ARIMA(1,0,0) with non-zero mean
##
## Coefficients:
## ar1 mean
## 0.1147 0.0095
## s.e. 0.0438 0.0024
##
## sigma^2 = 0.002353: log likelihood = 830.29
## AIC=-1654.58 AICc=-1654.53 BIC=-1641.84
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -1.671711e-05 0.04841048 0.03734009 103.8472 177.2729 0.7349234
## ACF1
## Training set 0.00782701
checkresiduals(fit)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,0,0) with non-zero mean
## Q* = 8.7243, df = 9, p-value = 0.4631
##
## Model df: 1. Total lags used: 10
forecast_values <- forecast(fit, h = 12)
print(forecast_values)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 517 0.013824178 -0.04833693 0.07598528 -0.08124303 0.1088914
## 518 0.010007999 -0.05256038 0.07257638 -0.08568208 0.1056981
## 519 0.009570439 -0.05300328 0.07214415 -0.08612780 0.1052687
## 520 0.009520269 -0.05305352 0.07209405 -0.08617808 0.1052186
## 521 0.009514516 -0.05305927 0.07208830 -0.08618383 0.1052129
## 522 0.009513857 -0.05305993 0.07208764 -0.08618449 0.1052122
## 523 0.009513781 -0.05306000 0.07208757 -0.08618457 0.1052121
## 524 0.009513772 -0.05306001 0.07208756 -0.08618457 0.1052121
## 525 0.009513771 -0.05306001 0.07208756 -0.08618458 0.1052121
## 526 0.009513771 -0.05306001 0.07208756 -0.08618458 0.1052121
## 527 0.009513771 -0.05306001 0.07208756 -0.08618458 0.1052121
## 528 0.009513771 -0.05306001 0.07208756 -0.08618458 0.1052121
autoplot(forecast_values) +
ggtitle("Forecast of Future Returns for Decile 2") +
xlab("Year") + ylab("Returns")
## install.packages("tseries")
library(tseries)
setwd("D:/3-研究生课程/金融计量经济学/课程作业/第二次作业/")
data3 <- read.table("d-ibm3dx7008.txt", header = T)
abs_rtn <- abs(data3$rtn)
abs_vwretd <- abs(data3$vwretd)
abs_ewretd <- abs(data3$ewretd)
abs_sprtrn <- abs(data3$sprtrn)
acf(abs_rtn, lag.max = 100, main = "ACF of rtn Stock Daily Absolute Returns")
acf(abs_vwretd, lag.max = 100, main = "ACF of vwretd Stock Daily Absolute Returns")
acf(abs_ewretd, lag.max = 100, main = "ACF of ewretd Stock Daily Absolute Returns")
acf(abs_sprtrn, lag.max = 100, main = "ACF of sprtrn Stock Daily Absolute Returns")
library(tidyverse)
library(forecast)
setwd("D:/3-研究生课程/金融计量经济学/课程作业/第二次作业/")
data4 <- read.table("power6.txt", header = FALSE) %>%
rename(Demand = V1)
ts_data4 <- ts(data4$Demand, start = c(2025, 1), frequency = 12)
plot(ts_data4, main = "Monthly Electricity Demand of Manufacturing Sector",
ylab = "Demand (Units)", xlab = "Year")
fit <- auto.arima(ts_data4)
summary(fit)
## Series: ts_data4
## ARIMA(0,1,1)(1,1,0)[12]
##
## Coefficients:
## ma1 sar1
## -0.5014 -0.5313
## s.e. 0.0628 0.0541
##
## sigma^2 = 0.0005049: log likelihood = 596.54
## AIC=-1187.07 AICc=-1186.97 BIC=-1176.49
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0002055022 0.02182204 0.01714808 0.001784199 0.178798 0.4714713
## ACF1
## Training set 0.04213773
forecast_values <- forecast(fit, h = 24)
print(forecast_values)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 2047 9.890679 9.861883 9.919475 9.846639 9.934719
## Feb 2047 9.870136 9.837959 9.902313 9.820925 9.919347
## Mar 2047 9.902651 9.867416 9.937886 9.848763 9.956539
## Apr 2047 9.925680 9.887632 9.963728 9.867490 9.983870
## May 2047 9.948251 9.907584 9.988918 9.886056 10.010446
## Jun 2047 10.027257 9.984129 10.070384 9.961299 10.093215
## Jul 2047 10.059916 10.014461 10.105370 9.990399 10.129433
## Aug 2047 10.088482 10.040813 10.136150 10.015579 10.161384
## Sep 2047 10.098860 10.049076 10.148644 10.022722 10.174998
## Oct 2047 10.036230 9.984417 10.088043 9.956989 10.115471
## Nov 2047 9.976174 9.922409 10.029940 9.893947 10.058402
## Dec 2047 9.954410 9.898761 10.010060 9.869302 10.039519
## Jan 2048 9.951905 9.889673 10.014136 9.856729 10.047080
## Feb 2048 9.927240 9.861533 9.992948 9.826749 10.027732
## Mar 2048 9.964582 9.895573 10.033591 9.859042 10.070122
## Apr 2048 9.979680 9.907521 10.051838 9.869322 10.090037
## May 2048 10.011085 9.935908 10.086262 9.896112 10.126059
## Jun 2048 10.092744 10.014666 10.170823 9.973333 10.212156
## Jul 2048 10.130738 10.049862 10.211614 10.007048 10.254428
## Aug 2048 10.155550 10.071969 10.239130 10.027725 10.283375
## Sep 2048 10.165674 10.079474 10.251874 10.033843 10.297505
## Oct 2048 10.089778 10.001036 10.178520 9.954059 10.225497
## Nov 2048 10.027520 9.936308 10.118733 9.888022 10.167018
## Dec 2048 10.016257 9.922638 10.109876 9.873079 10.159434
autoplot(forecast_values) +
ggtitle("Forecast of Future Electricity Demand") +
xlab("Year") + ylab("Demand (Units)")