forked from loelschlaeger/fHMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
99 lines (75 loc) · 3.49 KB
/
Copy pathREADME.Rmd
File metadata and controls
99 lines (75 loc) · 3.49 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "75%",
fig.align = "center"
)
set.seed(1)
library(fHMM)
```
# {fHMM}: Fitting HMMs to financial time series <img src="man/figures/logo.svg" align="right" alt="" width="120" />
<!-- badges: start -->
[](https://github.com/loelschlaeger/fHMM/actions)
[](https://www.r-pkg.org/badges/version-last-release/fHMM)
[](https://cranlogs.r-pkg.org/badges/grand-total/fHMM)
[](https://app.codecov.io/gh/loelschlaeger/fHMM)
<!-- badges: end -->
With {fHMM} you can detect and characterize financial market regimes in financial time series by applying hidden Markov Models (HMMs). The functionality and the model [is documented in detail here](https://loelschlaeger.de/fHMM/articles/). Below, you can find a first application to the German stock index [DAX](https://en.wikipedia.org/wiki/DAX).
## Installation
You can install the released version of {fHMM} from [CRAN](https://CRAN.R-project.org) with:
```{r, eval = FALSE}
install.packages("fHMM")
```
And the development version from [GitHub](https://github.com/) with:
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("loelschlaeger/fHMM")
```
## Contributing
We welcome contributions! Please submit [bug reports](https://github.com/loelschlaeger/fHMM/issues/new?assignees=&labels=bug&template=bug.md) and [feature requests](https://github.com/loelschlaeger/fHMM/issues/new?assignees=&labels=future&template=suggestion.md) as issues and extensions as pull request from a branch forked from main.
## Example: Fitting an HMM to the DAX
We fit a 2-state HMM with state-dependent t-distributions to the DAX log-returns from 2000 to 2020. The states can be interpreted as proxies for bearish and bullish markets.
The package has a build-in function to download the data from [Yahoo Finance](https://finance.yahoo.com/):
```{r data download}
path <- paste0(tempdir(),"/dax.csv")
download_data(symbol = "^GDAXI", file = path, verbose = FALSE)
```
We first need to define the model by setting some `controls`:
```{r controls}
controls <- list(
states = 2,
sdds = "t",
data = list(file = path,
date_column = "Date",
data_column = "Close",
logreturns = TRUE,
from = "2000-01-01",
to = "2020-12-31")
)
controls <- set_controls(controls)
```
The function `prepare_data()` prepares the data for estimation:
```{r data}
data <- prepare_data(controls)
summary(data)
```
We fit the model and subsequently decode the hidden states:
```{r fit}
model <- fit_model(data, ncluster = 7)
model <- decode_states(model)
summary(model)
```
Having estimated the model, we can visualize the state-dependent distributions and the decoded time series:
```{r plots, fig.dim = c(10,6), fig.align='center'}
events <- fHMM_events(
list(dates = c("2001-09-11", "2008-09-15", "2020-01-27"),
labels = c("9/11 terrorist attack", "Bankruptcy Lehman Brothers", "First COVID-19 case Germany"))
)
plot(model, plot_type = c("sdds","ts"), events = events)
```