时间序列因果关系_分析具有因果关系的时间序列干预:货币波动
時(shí)間序列因果關(guān)系
When examining a time series, it is quite common to have an intervention influence that series at a particular point.
在檢查時(shí)間序列時(shí),在特定時(shí)間點(diǎn)對(duì)該序列產(chǎn)生干預(yù)影響是很常見的。
Some examples of this could be:
例如:
- An advertising campaign that results in a significant change in sales 導(dǎo)致銷售發(fā)生重大變化的廣告活動(dòng)
- A positive change in traffic policy that results in lower road deaths 交通政策的積極變化,可減少道路死亡人數(shù)
- A change in economic policy that affects asset prices 影響資產(chǎn)價(jià)格的經(jīng)濟(jì)政策變化
The problem with analysing the effects of interventions is that one is then unable to examine how that series would have trended without that intervention.
分析干預(yù)措施效果的問題在于,如果沒有該干預(yù)措施,人們將無法檢查該系列趨勢。
For instance, suppose that a company implements an advertising campaign with the intent of boosting sales. While sales data can be recorded post-intervention — it is not possible to say what the sales would have been without that intervention.
例如,假設(shè)一家公司實(shí)施了旨在促進(jìn)銷售的廣告活動(dòng)。 雖然銷售數(shù)據(jù)可以記錄后干預(yù)-這是不可能說什么銷售本來沒有這種干預(yù)。
我們的例子 (Our Example)
For this example, let us consider the impact of an interest rate change on currency fluctuations.
對(duì)于此示例,讓我們考慮利率變化對(duì)貨幣波動(dòng)的影響 。
When a central bank manipulates interest rates — this is hypothesised to have a significant impact on a currency as it affects the perceived value of that currency relative to others in the market.
當(dāng)中央銀行操縱利率時(shí),假設(shè)這會(huì)對(duì)貨幣產(chǎn)生重大影響,因?yàn)樗鼤?huì)影響該貨幣相對(duì)于市場上其他貨幣的感知價(jià)值。
However, what if we wish to quantify such an impact? Let’s see how this works with the causalimpact package. This package was originally developed by Google for R, but we will use the Python version (pycasualimpact) in this example.
但是,如果我們希望量化這種影響怎么辦? 讓我們看看它如何與causalimpact包一起工作。 該軟件包最初是由Google for R開發(fā)的,但是在此示例中,我們將使用Python版本(pycasualimpact)。
Let’s consider fluctuations in the GBP/USD for the year 2017.
讓我們考慮一下2017年英鎊/美元的波動(dòng)。
In November 2017, the Bank of England decided to raise interest rates.
2017年11月,英格蘭銀行決定提高利率。
We will use causalimpact in an attempt to investigate:
我們將使用因果影響來調(diào)查:
In order to determine whether such an intervention has a significant effect on a time series — we must use a suitable covariate (X). This covariate must not be affected by the intervention.
為了確定此類干預(yù)是否會(huì)對(duì)時(shí)間序列產(chǎn)生重大影響,我們必須使用適當(dāng)?shù)膮f(xié)變量(X)。 此協(xié)變量不得受到干預(yù)的影響。
For instance, we can see that for the year 2017, the European Central Bank left interest rates unchanged (and have done since):
例如,我們可以看到,歐洲中央銀行在2017年維持利率不變(此后一直這樣做):
Source: tradingeconomics.com資料來源:tradingeconomics.comTaking this into account, the EUR/USD will be used as the covariate for this example.
考慮到這一點(diǎn),本示例將使用EUR / USD作為協(xié)變量。
X = eurusdy = gbpusd
While it is hypothesised that the currency will be affected by fluctuations in the greenback as well as many other macroeconomic conditions — it is assumed that the Bank of England intervention does not impact the EUR/USD.
假設(shè)該貨幣將受到美元波動(dòng)以及許多其他宏觀經(jīng)濟(jì)狀況的影響-假設(shè)英格蘭銀行的干預(yù)不會(huì)影響歐元/美元。
分析 (Analysis)
The relevant currency pairs are downloaded from the FRED database using Quandl:
使用Quandl從FRED數(shù)據(jù)庫下載相關(guān)貨幣對(duì):
>>> 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')gbpusd
Here is a plot of the two currency pairs:
這是兩個(gè)貨幣對(duì)的圖表:
英鎊/美元 (GBP/USD)
Source: Federal Reserve Economic Data資料來源:美聯(lián)儲(chǔ)經(jīng)濟(jì)數(shù)據(jù)歐元/美元 (EUR/USD)
Source: Federal Reserve Economic Data資料來源:美聯(lián)儲(chǔ)經(jīng)濟(jì)數(shù)據(jù)With the rate decision by the Bank of England having been taken on 2nd November 2017, t = 0 to 229 is defined as the pre-period (before intervention) and t = 230 to 248 is defined as the post-period (after intervention).
由于英格蘭銀行已于2017年11月2日做出利率決定,因此將t = 0到229定義為前期(干預(yù)之前),將t = 230到248定義為后期(干預(yù)之后) 。
data = pd.DataFrame({'y': y, 'X': X}, columns=['y', 'X'])pre_period = [0, 229]
post_period = [230, 248]
According to the official GitHub for this package, this model “uses a classical Kalman Filter approach for solving the statespace equations”.
根據(jù)該軟件包的官方GitHub的說法,該模型“使用經(jīng)典的Kalman濾波方法來求解狀態(tài)空間方程”。
The posterior inference can be generated as follows:
可以如下生成后驗(yàn)推斷:
>>> ci = CausalImpact(data, pre_period, post_period)>>> print(ci.summary())
>>> print(ci.summary(output='report'))
>>> ci.plot()
Here is the generated output:
這是生成的輸出:
Posterior Inference {Causal Impact}Average Cumulative
Actual 1.34 25.46
Prediction (s.d.) 1.32 (0.0) 25.17 (0.08)
95% CI [1.32, 1.33] [25.01, 25.33]
Absolute effect (s.d.) 0.02 (0.0) 0.29 (0.08)
95% CI [0.01, 0.02] [0.13, 0.45]
Relative effect (s.d.) 1.15% (0.32%) 1.15% (0.32%)
95% CI [0.52%, 1.77%] [0.52%, 1.77%]
Posterior tail-area probability p: 0.0
Posterior prob. of a causal effect: 100.0%
According to the above findings, the actual GBP/USD rate comes in at 1.34, while the rate without the intervention would have been 1.32.
根據(jù)上述發(fā)現(xiàn),英鎊/美元的實(shí)際匯率為1.34 ,而沒有干預(yù)的匯率將為1.32 。
Moreover, the model indicates that there is a 100% posterior probability of a causal effect.
此外,該模型表明因果關(guān)系的后驗(yàn)概率為100%。
When analysing the more detailed report, the output also states:
在分析更詳細(xì)的報(bào)告時(shí),輸出還指出:
The probability of obtaining this effect by chance is very small(Bayesian one-sided tail-area probability p = 0.0).This means the causal effect can be considered statisticallysignificant.
偶然獲得此效應(yīng)的可能性很小(貝葉斯單側(cè)尾部區(qū)域概率p = 0.0)。這意味著因果效應(yīng)在統(tǒng)計(jì)上是有意義的。
From this standpoint, the evidence suggests that the GBP/USD trended higher than it would have done without an interest rate rise. Here is an overview of the actual rate vs predictions:
從這個(gè)角度來看,有證據(jù)表明,英鎊/美元的趨勢要高于沒有加息的情況。 以下是實(shí)際費(fèi)率與預(yù)測的概述:
Source: Jupyter Notebook Output資料來源:Jupyter Notebook輸出As mentioned, Python uses a different approach than R in generating forecasts without an intervention — the latter relying more on a Bayesian approach that emphasises user prior knowledge. Indeed, one could try both methods to verify if the forecasts being generated coincide.
如前所述,Python使用R的方法與R不同,無需干預(yù)即可生成預(yù)測-后者更多地依賴于強(qiáng)調(diào)用戶先驗(yàn)知識(shí)的貝葉斯方法。 確實(shí),可以嘗試兩種方法來驗(yàn)證所生成的預(yù)測是否一致。
結(jié)論 (Conclusion)
This has been an introductory example to the causalimpact library and how the effects of interventions can be examined across a time series.
這是因果影響庫的介紹性示例,以及如何在整個(gè)時(shí)間序列中檢查干預(yù)措施的影響。
Many thanks for your time, and any questions or feedback are greatly welcomed.
非常感謝您的寶貴時(shí)間,我們歡迎您提出任何問題或反饋。
The Jupyter Notebook for this example is available at the MGCodesandStats repository as referenced below.
MGCodesandStats存儲(chǔ)庫中提供了此示例的Jupyter Notebook,如下所述。
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.
免責(zé)聲明:本文按“原樣”撰寫,不作任何擔(dān)保。 本文檔旨在概述數(shù)據(jù)科學(xué)概念,不應(yīng)將其解釋為投資建議或任何其他形式的專業(yè)建議。
翻譯自: https://towardsdatascience.com/analysing-time-series-interventions-with-causalimpact-currency-fluctuations-e6ab14a30768
時(shí)間序列因果關(guān)系
總結(jié)
以上是生活随笔為你收集整理的时间序列因果关系_分析具有因果关系的时间序列干预:货币波动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rstudio 管道符号_R中的管道指南
- 下一篇: 无法从套接字中获取更多数据_数据科学中应