使用r语言做garch模型_使用GARCH估计货币波动率

使用r语言做garch模型

Asset prices have a high degree of stochastic trends inherent in the time series. In other words, price fluctuations are subject to a large degree of randomness, and therefore it is very difficult to forecast asset prices using traditional time series models such as ARIMA.

资产价格具有时间序列固有的高度随机趋势。 换句话说,价格波动受到很大程度的随机性影响,因此很难使用诸如ARIMA之类的传统时间序列模型来预测资产价格。

Moreover, with much of the trading done on an algorithmic basis today — prices are constantly adjusting on the basis of such forecasts — making it quite hard to exploit an advantage in the markets.

而且,由于当今许多交易都是基于算法进行的-价格在这种预测的基础上不断调整-因此很难在市场中利用优势。

For instance, suppose I build a time series model to predict rainfall in a city for the next three months. My time series model could have a strong degree of accuracy as the forecasts will not influence rainfall levels in the future. However, if everyone uses an ARIMA model to predict asset price fluctuations for the next three months — then subsequent trading on the basis of those forecasts will directly influence previous forecasts — rendering them invalid in many cases.

例如,假设我建立了一个时间序列模型来预测未来三个月城市的降雨。 我的时间序列模型可能具有很高的准确性,因为预测不会影响将来的降雨量。 但是,如果每个人都使用ARIMA模型来预测未来三个月的资产价格波动-那么根据这些预测进行的后续交易将直接影响先前的预测-在许多情况下使它们无效。

背景 (Background)

As a result, it is common to model projected volatility of an asset price in the financial markets — as opposed to forecasting projected price outright.

因此,通常在金融市场中对资产价格的预计波动率建模,而不是直接预测预测价格。

Let’s see how this can be accomplished using Python. A GARCH model is used to forecast volatility for the EUR/USD and GBP/USD currency pairs, using data from January 2017 — January 2018.

让我们看看如何使用Python来实现。 GARCH模型用于使用2017年1月至2018年1月的数据预测EUR / USD和GBP / USD货币对的波动。

The data is sourced from FRED using the Quandl library:

数据使用Quandl库来自FRED:

eurusd = quandl.get("FRED/DEXUSEU", start_date='2017-01-01', end_date='2018-01-01', api_key='enter_api_key')gbpusd = quandl.get("FRED/DEXUSUK", start_date='2017-01-01', end_date='2018-01-01', api_key='enter_api_key')

The series are converted to logarithmic format to smooth out the time series:

该系列将转换为对数格式,以平滑时间序列:

欧元/美元 (EUR/USD)

英镑/美元 (GBP/USD)

Image for post
Source: Quandl
资料来源:Quandl

The data is then first-differenced to approximate a Gaussian distribution.

然后对数据进行一阶微分以近似高斯分布。

Dickey-Fuller tests show a p-value of 0 for both series — indicating that we reject the null hypothesis that a unit root is present at the 5% level of significance, i.e. stationarity or trend stationarity is indicated as being present in the model.

Dickey-Fuller检验显示两个系列的p值均为0,这表明我们拒绝零假设,即单位根以5%的显着性水平存在,即模型中指示存在平稳性或趋势平稳性。

EUR/USD: Dickey-Fuller Test Results

欧元/美元:迪基-富勒测试结果

>>> result = ts.adfuller(data, 1)
>>> result(-16.26123019770431,
3.564065405943774e-29,
0,
247,
{'1%': -3.457105309726321,
'5%': -2.873313676101283,
'10%': -2.5730443824681606},
-1959.704886024891)

GBP/USD: Dickey-Fuller Test Results

英镑/美元:迪基-富勒测试结果

>>> result = ts.adfuller(data, 1)
>>> result(-12.380335699861567,
5.045829408723097e-23,
1,
246,
{'1%': -3.457215237265747,
'5%': -2.873361841566324,
'10%': -2.5730700760129555},
-1892.8308007824835)

Additionally, a visual screening of QQ plots show that the series now largely follow a normal distribution:

此外,对QQ图的可视化筛选显示,该系列现在基本上遵循正态分布:

EUR/USD: QQ Plot

欧元/美元:QQ情节

Image for post
Source: Jupyter Notebook Output
资料来源:Jupyter Notebook输出

GBP/USD: QQ Plot

英镑/美元:QQ情节

Image for post
Source: Jupyter Notebook Output
资料来源:Jupyter Notebook输出

GARCH建模 (GARCH Modelling)

A GARCH(1,1) model is built to predict the volatility for the last 30 days of trading data for both currency pairs. The previous data is used as the training set for the GARCH model.

建立了GARCH(1,1)模型以预测两种货币对的交易数据的最后30天的波动性。 先前的数据用作GARCH模型的训练集。

# split into train/test
n_test = 30
train, test = data[:-n_test], data[-n_test:]
# define model
model = arch_model(train, mean='Zero', vol='GARCH', p=1, q=1)

The predictions are generated as follows:

预测生成如下:

# fit model
model_fit = model.fit()
# forecast the test set
yhat = model_fit.forecast(horizon=n_test)

Now, let’s compare the predicted variance with the actual 5-day rolling variance across the test set.

现在,让我们将预测方差与整个测试集的实际5天滚动方差进行比较。

欧元/美元 (EUR/USD)

Predicted Variance

预测方差

test.rolling(window=5).var().plot(style='g')
pyplot.title("5-day Rolling Variance")
Image for post
Source: Jupyter Notebook Output
资料来源:Jupyter Notebook输出

5-day Rolling Variance

5天滚动差异

pyplot.plot(yhat.variance.values[-1, :])
pyplot.title("Predicted Variance")
pyplot.show()
Image for post
Source: Jupyter Notebook Output
资料来源:Jupyter Notebook输出

We see that the GARCH model predicts a drop in volatility for the last 30 days (as measured by variance) — this is confirmed by a visual inspection of the actual 5-day rolling variance.

我们看到,GARCH模型预测了最近30天的波动性下降(以方差衡量),这通过对实际5天滚动方差的目视检查得到确认。

Let’s have a look at the comparisons across the GBP/USD.

让我们看一下英镑/美元之间的比较。

英镑/美元 (GBP/USD)

Predicted Variance

预测方差

Image for post
Source: Jupyter Notebook Output
资料来源:Jupyter Notebook输出

5-day Rolling Variance

5天滚动差异

Image for post
Source: Jupyter Notebook Output
资料来源:Jupyter Notebook输出

We can see that while the scale for the predicted variance is much narrower than the actual 5-day rolling variance — both instances are predicting a decrease in variance across the 30-day test period.

我们可以看到,尽管预测方差的标度比实际5天滚动方差要窄得多,但两个实例都预测了30天测试期间方差的减少。

This corresponds with what we actually observe — there was little movement in both the EUR/USD and GBP/USD currency pairs in December 2017 relative to that of other months in the trading year.

这与我们实际观察到的情况相对应-与交易年度的其他月份相比,2017年12月的EUR / USD和GBP / USD货币对几乎没有变化。

结论 (Conclusion)

This has been an illustration of how GARCH can be used to model time series volatility.

这说明了如何使用GARCH对时间序列波动性进行建模。

Hope you found the article useful, and any questions or feedback are greatly appreciated. The GitHub repository for this example, as well as other relevant references are available below.

希望您觉得这篇文章对您有用,对您的任何问题或反馈都深表感谢。 下面提供了此示例的GitHub存储库以及其他相关参考。

Disclaimer: This article is written on an “as is” basis and without warranty. It was written with the intention of providing an overview of data science concepts, and should not be interpreted as investment advice, or any other sort of professional advice.

免责声明:本文按“原样”撰写,不作任何担保。 本文档旨在概述数据科学概念,不应将其解释为投资建议或任何其他形式的专业建议。

翻译自: https://towardsdatascience.com/estimating-currency-volatility-using-garch-e373cf82179d

使用r语言做garch模型

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391348.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ARC下的内存泄漏

##ARC下的内存泄漏 ARC全称叫 ARC(Automatic Reference Counting)。在编译期间,编译器会判断对象的使用情况,并适当的加上retain和release,使得对象的内存被合理的管理。所以,从本质上说ARC和MRC在本质上是一样的,都是…

python:校验邮箱格式

# coding:utf-8import redef validateEmail(email):if re.match("^.\\(\\[?)[a-zA-Z0-9\\-\\.]\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) ! None:# if re.match("/^\w[a-z0-9]\.[a-z]{2,4}$/", email) ! None:print okreturn okelse:print failret…

cad2019字体_这些是2019年最有效的简历字体

cad2019字体When it comes to crafting the perfect resume to land your dream job, you probably think of just about everything but the font. But font is a key part of your first impression to recruiters and employers.当制作一份完美的简历来实现理想的工作时&…

Go语言实战 : API服务器 (4) 配置文件读取及连接数据库

读取配置文件 1. 主函数中增加配置初始化入口 先导入viper包 import (..."github.com/spf13/pflag""github.com/spf13/viper""log")在 main 函数中增加了 config.Init(*cfg) 调用,用来初始化配置,cfg 变量值从命令行 f…

方差偏差权衡_偏差偏差权衡:快速介绍

方差偏差权衡The bias-variance tradeoff is one of the most important but overlooked and misunderstood topics in ML. So, here we want to cover the topic in a simple and short way as possible.偏差-方差折衷是机器学习中最重要但被忽视和误解的主题之一。 因此&…

win10 uwp 让焦点在点击在页面空白处时回到textbox中

原文:win10 uwp 让焦点在点击在页面空白处时回到textbox中在网上 有一个大神问我这样的问题:在做UWP的项目,怎么能让焦点在点击在页面空白处时回到textbox中? 虽然我的小伙伴认为他这是一个 xy 问题,但是我还是回答他这个问题。 首…

python:当文件中出现特定字符串时执行robot用例

#coding:utf-8 import os import datetime import timedef execute_rpt_db_full_effe_cainiao_city():flag Truewhile flag:# 判断该文件是否存在# os.path.isfile("/home/ytospid/opt/docker/jsc_spider/jsc_spider/log/call_proc.log")# 存在则获取昨天日期字符串…

MySQL分库分表方案

1. MySQL分库分表方案 1.1. 问题:1.2. 回答: 1.2.1. 最好的切分MySQL的方式就是:除非万不得已,否则不要去干它。1.2.2. 你的SQL语句不再是声明式的(declarative)1.2.3. 你招致了大量的网络延时1.2.4. 你失去…

linux创建sudo用户_Linux终极指南-创建Sudo用户

linux创建sudo用户sudo stands for either "superuser do" or "switch user do", and sudo users can execute commands with root/administrative permissions, even malicious ones. Be careful who you grant sudo permissions to – you are quite lit…

重学TCP协议(1) TCP/IP 网络分层以及TCP协议概述

1. TCP/IP 网络分层 TCP/IP协议模型(Transmission Control Protocol/Internet Protocol),包含了一系列构成互联网基础的网络协议,是Internet的核心协议,通过20多年的发展已日渐成熟,并被广泛应用于局域网和…

分节符缩写p_p值的缩写是什么?

分节符缩写pp是概率吗? (Is p for probability?) Technically, p-value stands for probability value, but since all of statistics is all about dealing with probabilistic decision-making, that’s probably the least useful name we could give it.从技术…

Spring-----AOP-----事务

xml文件中&#xff1a; 手动处理事务&#xff1a; 设置数据源 <bean id"dataSource" class"com.mchange.v2.c3p0.ComboPooledDataSource"> <property name"driverClass" value"com.mysql.jdbc.Driver"></property…

[测试题]打地鼠

Description 小明听说打地鼠是一件很好玩的游戏&#xff0c;于是他也开始打地鼠。地鼠只有一只&#xff0c;而且一共有N个洞&#xff0c;编号为1到N排成一排&#xff0c;两边是墙壁&#xff0c;小明当然不可能百分百打到&#xff0c;因为他不知道地鼠在哪个洞。小明只能在白天打…

在PHP服务器上使用JavaScript进行缓慢的Loris攻击[及其预防措施!]

Forget the post for a minute, lets begin with what this title is about! This is a web security-based article which will get into the basics about how HTTP works. Well also look at a simple attack which exploits the way the HTTP protocol works.暂时忘掉这个帖…

三星为什么要卖芯片?手机干不过华为小米,半导体好挣钱!

据外媒DigiTimes报道&#xff0c;三星有意向其他手机厂商出售自家的Exynos芯片以扩大市场份额。知情人士透露&#xff0c;三星出售自家芯片旨在提高硅晶圆工厂的利用率&#xff0c;同时提高它们在全球手机处理器市场的份额&#xff0c;尤其是中端市场。 三星为什么要卖芯片&…

重学TCP协议(2) TCP 报文首部

1. TCP 报文首部 1.1 源端口和目标端口 每个TCP段都包含源端和目的端的端口号&#xff0c;用于寻找发端和收端应用进程。这两个值加上IP首部中的源端IP地址和目的端IP地址唯一确定一个TCP连接 端口号分类 熟知端口号&#xff08;well-known port&#xff09;已登记的端口&am…

linux:vim中全选复制

全选&#xff08;高亮显示&#xff09;&#xff1a;按esc后&#xff0c;然后ggvG或者ggVG 全部复制&#xff1a;按esc后&#xff0c;然后ggyG 全部删除&#xff1a;按esc后&#xff0c;然后dG 解析&#xff1a; gg&#xff1a;是让光标移到首行&#xff0c;在vim才有效&#xf…

机器学习 预测模型_使用机器学习模型预测心力衰竭的生存时间-第一部分

机器学习 预测模型数据科学 &#xff0c; 机器学习 (Data Science, Machine Learning) 前言 (Preface) Cardiovascular diseases are diseases of the heart and blood vessels and they typically include heart attacks, strokes, and heart failures [1]. According to the …

程序2:word count

本程序改变自&#xff1a;http://blog.csdn.net/zhixi1050/article/details/72718638 语言&#xff1a;C 编译环境&#xff1a;visual studio 2015 运行环境&#xff1a;Win10 做出修改的地方&#xff1a;在原码基础上修改了记录行数的功能&#xff0c;删去了不完整行数的记录&…

重学TCP协议(3) 端口号及MTU、MSS

1. 端口相关的命令 1.1 查看端口是否打开 使用 nc 和 telnet 这两个命令可以非常方便的查看到对方端口是否打开或者网络是否可达。如果对端端口没有打开&#xff0c;使用 telnet 和 nc 命令会出现 “Connection refused” 错误 1.2 查看监听端口的进程 使用 netstat sudo …