getfactormodels 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of getfactormodels might be problematic. Click here for more details.
- getfactormodels-0.0.1/.gitattributes +23 -0
- getfactormodels-0.0.1/.gitignore +43 -0
- getfactormodels-0.0.1/LICENSE +21 -0
- getfactormodels-0.0.1/PKG-INFO +234 -0
- getfactormodels-0.0.1/README.md +199 -0
- getfactormodels-0.0.1/getfactormodels/__init__.py +43 -0
- getfactormodels-0.0.1/getfactormodels/__main__.py +168 -0
- getfactormodels-0.0.1/getfactormodels/models/__init__.py +24 -0
- getfactormodels-0.0.1/getfactormodels/models/ff_models.py +141 -0
- getfactormodels-0.0.1/getfactormodels/models/models.py +462 -0
- getfactormodels-0.0.1/getfactormodels/utils/__init__.py +0 -0
- getfactormodels-0.0.1/getfactormodels/utils/cli.py +28 -0
- getfactormodels-0.0.1/getfactormodels/utils/utils.py +174 -0
- getfactormodels-0.0.1/noxfile.py +19 -0
- getfactormodels-0.0.1/pyproject.toml +79 -0
- getfactormodels-0.0.1/requirements.txt +9 -0
- getfactormodels-0.0.1/tests/__init__.py +0 -0
- getfactormodels-0.0.1/tests/test_inputs.py +67 -0
- getfactormodels-0.0.1/tests/test_models.py +152 -0
- getfactormodels-0.0.1/tests/test_models_ff.py +39 -0
- getfactormodels-0.0.1/tests/test_utils.py +182 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Binary files
|
|
2
|
+
*.db binary
|
|
3
|
+
*.p binary
|
|
4
|
+
*.pkl binary
|
|
5
|
+
*.pickle binary
|
|
6
|
+
*.pyc binary export-ignore
|
|
7
|
+
*.pyo binary export-ignore
|
|
8
|
+
*.pyd binary
|
|
9
|
+
|
|
10
|
+
# Jupyter notebook
|
|
11
|
+
*.ipynb text eol=lf
|
|
12
|
+
|
|
13
|
+
# GitHub: treat .ipynb files as vendored code.
|
|
14
|
+
*.ipynb linguist-vendored
|
|
15
|
+
|
|
16
|
+
# Python: use Python's built-in diff
|
|
17
|
+
*.pxd text diff=python
|
|
18
|
+
*.py text diff=python
|
|
19
|
+
*.py3 text diff=python
|
|
20
|
+
*.pyw text diff=python
|
|
21
|
+
*.pyx text diff=python
|
|
22
|
+
*.pyz text diff=python
|
|
23
|
+
*.pyi text diff=python
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# .gitignore
|
|
2
|
+
__pycache__/
|
|
3
|
+
__pypackages__
|
|
4
|
+
_build
|
|
5
|
+
.coverage
|
|
6
|
+
.direnv
|
|
7
|
+
.eggs
|
|
8
|
+
.eggs/
|
|
9
|
+
.env
|
|
10
|
+
.installed.cfg
|
|
11
|
+
.mypy_cache
|
|
12
|
+
.pytest_cache/
|
|
13
|
+
.Python
|
|
14
|
+
.pytype
|
|
15
|
+
.svn
|
|
16
|
+
.venv
|
|
17
|
+
*.egg
|
|
18
|
+
*.egg-info/
|
|
19
|
+
build/
|
|
20
|
+
develop-eggs/
|
|
21
|
+
dist/
|
|
22
|
+
eggs/
|
|
23
|
+
env.bak/
|
|
24
|
+
env/
|
|
25
|
+
ENV/
|
|
26
|
+
lib/
|
|
27
|
+
lib64/
|
|
28
|
+
MANIFEST
|
|
29
|
+
sdist/
|
|
30
|
+
var/
|
|
31
|
+
venv.bak/
|
|
32
|
+
venv/
|
|
33
|
+
wheels/
|
|
34
|
+
|
|
35
|
+
.nox/
|
|
36
|
+
.tox/
|
|
37
|
+
.vscode/
|
|
38
|
+
.ruff_cache
|
|
39
|
+
.cache/
|
|
40
|
+
*_venv/
|
|
41
|
+
|
|
42
|
+
**/*.csv
|
|
43
|
+
**/*.xlsx
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 S. Martin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: getfactormodels
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Retreive data for various multifactor asset pricing models.
|
|
5
|
+
Keywords: finance,pricing models,financial analysis,econometrics,asset pricing,multifactor models
|
|
6
|
+
Author-email: "S. Martin" <x512@pm.me>
|
|
7
|
+
Requires-Python: >=3.7
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
10
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Environment :: Console
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
19
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
20
|
+
Requires-Dist: numpy >=1.18.5
|
|
21
|
+
Requires-Dist: pandas >=1.4
|
|
22
|
+
Requires-Dist: requests >=2.20.0
|
|
23
|
+
Requires-Dist: pyarrow >=14.0.1
|
|
24
|
+
Requires-Dist: openpyxl >=3.0.3
|
|
25
|
+
Requires-Dist: tabulate >=0.8.7
|
|
26
|
+
Requires-Dist: ruff ; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov ; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest ; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-randomly ; extra == "dev"
|
|
30
|
+
Requires-Dist: isort ; extra == "dev"
|
|
31
|
+
Requires-Dist: nox ; extra == "dev"
|
|
32
|
+
Project-URL: Homepage, https://github.com/x512/getfactormodels
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
|
|
35
|
+
<a name="readme-top"></a>
|
|
36
|
+
|
|
37
|
+
# getfactormodels
|
|
38
|
+
|
|
39
|
+

|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
Reliably retrieve data for various multi-factor asset pricing models.
|
|
43
|
+
|
|
44
|
+
## Models
|
|
45
|
+
|
|
46
|
+
- The 3-factor, 5-factor, and 6-factor models of Fama & French <sup>[[1]](#1) [[3]](#3) [[4]](#4)</sup>
|
|
47
|
+
- Mark Carhart's 4-factor model <sup>[[2]](#2)</sup>
|
|
48
|
+
- Pastor and Stambaugh's liquidity factors <sup>[[5]](#5)</sup>
|
|
49
|
+
- Mispricing factors of Stambaugh and Yuan<sup>[[6]](#6)</sup>
|
|
50
|
+
- The $q$*-factor* model of Hou, Mo, Xue and Zhang<sup>[[7]](#7)</sup>
|
|
51
|
+
- The augmented $q^5$*-factor* model of Hou, Mo, Xue and Zhang<sup>[[8]](#8)</sup>
|
|
52
|
+
- *Intermediary Capital Ratio* (ICR) of He, Kelly & Manela<sup>[[9]](#9)</sup>
|
|
53
|
+
- The *DHS behavioural factors* of Daniel, Hirshleifer & Sun<sup>[[10]](#10)</sup>
|
|
54
|
+
- The *HML* $^{DEVIL}$ factor of Asness & Frazzini<sup>[[11]](#11)</sup>
|
|
55
|
+
- The 6-factor model of Barillas and Shanken<sup>[[12]](#12)</sup>
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
_Thanks to: Kenneth French, Robert Stambaugh, Lin Sun, Zhiguo He, AQR Capital Management (AQR.com) and Hou, Xue and Zhang (global-q.org), for their research and for the datasets they publically provide._
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
`getfactormodels` requires Python ``>=3.7``
|
|
64
|
+
|
|
65
|
+
* Install with pip:
|
|
66
|
+
```shell
|
|
67
|
+
$ pip install getfactormodels
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Usage
|
|
71
|
+
|
|
72
|
+
#### Python
|
|
73
|
+
|
|
74
|
+
After installing, import ``getfactormodels`` and call ``get_factors()`` with the ``model`` and ``frequency`` parameters. Optionally, specify a ``start_date`` and ``end_date``
|
|
75
|
+
* For example, to retrieve the daily q-factor model data:
|
|
76
|
+
|
|
77
|
+
```py
|
|
78
|
+
import getfactormodels as getfactormodels
|
|
79
|
+
|
|
80
|
+
df = getfactormodels.get_factors(model='q', frequency='d')
|
|
81
|
+
```
|
|
82
|
+
> _Trimmed output:_
|
|
83
|
+
```txt
|
|
84
|
+
> df
|
|
85
|
+
Mkt-RF R_ME R_IA R_ROE R_EG RF
|
|
86
|
+
date
|
|
87
|
+
1967-01-03 0.000778 0.004944 0.001437 -0.007118 -0.008563 0.000187
|
|
88
|
+
1967-01-04 0.001667 -0.003487 -0.000631 -0.002044 -0.000295 0.000187
|
|
89
|
+
1967-01-05 0.012990 0.004412 -0.005688 0.000838 -0.003075 0.000187
|
|
90
|
+
1967-01-06 0.007230 0.006669 0.008897 0.003603 0.002669 0.000187
|
|
91
|
+
1967-01-09 0.008439 0.006315 0.000331 0.004949 0.002979 0.000187
|
|
92
|
+
... ... ... ... ... ... ...
|
|
93
|
+
2022-12-23 0.005113 -0.001045 0.004000 0.010484 0.003852 0.000161
|
|
94
|
+
2022-12-27 -0.005076 -0.001407 0.010190 0.009206 0.003908 0.000161
|
|
95
|
+
2022-12-28 -0.012344 -0.004354 0.000133 -0.010457 -0.004953 0.000161
|
|
96
|
+
2022-12-29 0.018699 0.008568 -0.008801 -0.012686 -0.002162 0.000161
|
|
97
|
+
2022-12-30 -0.002169 0.001840 0.001011 -0.004151 -0.003282 0.000161
|
|
98
|
+
|
|
99
|
+
[14096 rows x 6 columns]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
* or, retreive the monthly liquidity factors of Pastor and Stambaugh for the 1990s:
|
|
103
|
+
|
|
104
|
+
```py
|
|
105
|
+
import getfactormodels as getfactormodels
|
|
106
|
+
|
|
107
|
+
df = getfactormodels.get_factors(model='liquidity', frequency='m', start_date='1990-01-01', end_date='1999-12-31')
|
|
108
|
+
```
|
|
109
|
+
> If you don't have time to type `liquidity`, type `liq`, or `ps`--there's a handy regex.
|
|
110
|
+
|
|
111
|
+
* or, saving the monthly 3-factor model of Fama & French to a file:
|
|
112
|
+
|
|
113
|
+
```py
|
|
114
|
+
import getfactormodels as gfm
|
|
115
|
+
|
|
116
|
+
df = gfm.get_factors(model='ff3', frequency='m', output="ff3_data.csv")
|
|
117
|
+
```
|
|
118
|
+
>The output parameter accepts a filename, path or directory, and can be one of csv, md, txt, xlsx, pkl.
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
You can also import just the models that you need.
|
|
122
|
+
|
|
123
|
+
* For example, to import only the *ICR* and *q*-factor models:
|
|
124
|
+
|
|
125
|
+
```py
|
|
126
|
+
from getfactormodels import icr_factors, q_factors
|
|
127
|
+
|
|
128
|
+
# Passing a model function with no params defaults to monthly.
|
|
129
|
+
df = icr_factors()
|
|
130
|
+
|
|
131
|
+
# The 'q' models, and the 3-factor model of Fama-French also have weekly data.
|
|
132
|
+
df = q_factors(frequency="W", start_date="1992-01-01)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
* If using ``ff_factors()``, then an additional ``model`` parameter should be specified:
|
|
136
|
+
|
|
137
|
+
```py
|
|
138
|
+
from getfactormodels import ff_factors
|
|
139
|
+
|
|
140
|
+
# To get annual data for the 5-factor model:
|
|
141
|
+
data = ff_factors(model="5", frequency="Y", output=".xlsx")
|
|
142
|
+
|
|
143
|
+
# Daily 3-factor model data, since 1970 (not specifying an end date
|
|
144
|
+
# will return data up until today):
|
|
145
|
+
data = ff_factors(model="3", frequency="D", start_date="1970-01-01")
|
|
146
|
+
```
|
|
147
|
+
> Output allows just an extension to be specified.
|
|
148
|
+
|
|
149
|
+
* or import all the models:
|
|
150
|
+
|
|
151
|
+
```py
|
|
152
|
+
from getfactormodels import models
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
* There's also the `FactorExtractor` class that the CLI uses (it doesn't really do a whole lot yet):
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
from getfactormodels import FactorExtractor
|
|
159
|
+
|
|
160
|
+
fe = FactorExtractor(model='carhart', frequency='m', start_date='1980-01-01', end_date='1980-05-01')
|
|
161
|
+
fe.get_factors()
|
|
162
|
+
fe.to_file('carhart_factors.md')
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
* _The resulting ``carhart_factors.md`` file will look like this:_
|
|
166
|
+
|
|
167
|
+
| date | Mkt-RF | SMB | HML | MOM | RF |
|
|
168
|
+
|:--------------------|---------:|--------:|--------:|--------:|-------:|
|
|
169
|
+
| 1980-01-31 00:00:00 | 0.0551 | 0.0162 | 0.0175 | 0.0755 | 0.008 |
|
|
170
|
+
| 1980-02-29 00:00:00 | -0.0122 | -0.0185 | 0.0061 | 0.0788 | 0.0089 |
|
|
171
|
+
| 1980-03-31 00:00:00 | -0.129 | -0.0664 | -0.0101 | -0.0955 | 0.0121 |
|
|
172
|
+
| 1980-04-30 00:00:00 | 0.0397 | 0.0105 | 0.0106 | -0.0043 | 0.0126 |
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
#### Using the CLI
|
|
176
|
+
* You can also use getfactormodels from the command line.
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
$ getfactormodels -h
|
|
180
|
+
|
|
181
|
+
usage: getfactormodels [-h] -m MODEL [-f FREQ] [-s START] [-e END] [-o OUTPUT] [--no_rf]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
* An example of how to use the CLI to retrieve the Fama-French 3-factor model data:
|
|
185
|
+
```bash
|
|
186
|
+
getfactormodels --model ff3 --frequency M --start-date 1960-01-01 --end-date 2020-12-31 --output "filename.csv"
|
|
187
|
+
```
|
|
188
|
+
> Accepted file extensions are .csv, .txt, .xlsx, and .md. If no extension is given, the output file will be .csv. The --output flag allows a filename, filepath or a directory. If only an extension is provided (including the . else it'll be passed as a filename), a name will be generated.
|
|
189
|
+
|
|
190
|
+
* Here's another example that retrieves the annual Fama-French 5-factor data without the RF column:
|
|
191
|
+
|
|
192
|
+
```sh
|
|
193
|
+
getfactormodels -m 5 -f Y -s 1960-01-01 -e 2020-12-31 --no_rf -o ~/some_dir/filename.xlsx
|
|
194
|
+
```
|
|
195
|
+
> `--no_rf` will return the factor model without an RF column.
|
|
196
|
+
|
|
197
|
+
## References
|
|
198
|
+
1. <a id="1"></a> E. F. Fama and K. R. French, ‘Common risk factors in the returns on stocks and bonds’, *Journal of Financial Economics*, vol. 33, no. 1, pp. 3–56, 1993. [PDF](https://people.duke.edu/~charvey/Teaching/BA453_2006/FF_Common_risk.pdf)
|
|
199
|
+
2. <a id="2"></a> M. Carhart, ‘On Persistence in Mutual Fund Performance’, *Journal of Finance*, vol. 52, no. 1, pp. 57–82, 1997. [PDF](https://onlinelibrary.wiley.com/doi/full/10.1111/j.1540-6261.1997.tb03808.x)
|
|
200
|
+
3. <a id="3"></a> E. F. Fama and K. R. French, ‘A five-factor asset pricing model’, *Journal of Financial Economics*, vol. 116, no. 1, pp. 1–22, 2015. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2287202)
|
|
201
|
+
4. <a id="4"></a> E. F. Fama and K. R. French, ‘Choosing factors’, *Journal of Financial Economics*, vol. 128, no. 2, pp. 234–252, 2018. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2668236)
|
|
202
|
+
5. <a id="5"></a>L. Pastor and R. Stambaugh, ‘Liquidity Risk and Expected Stock Returns’, *Journal of Political Economy*, vol. 111, no. 3, pp. 642–685, 2003. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=279804)
|
|
203
|
+
6. <a id="6"></a>R. F. Stambaugh and Y. Yuan, ‘Mispricing Factors’, *The Review of Financial Studies*, vol. 30, no. 4, pp. 1270–1315, 12 2016. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2626701)
|
|
204
|
+
7. <a id="7"></a>K. Hou, H. Mo, C. Xue, and L. Zhang, ‘Which Factors?’, *National Bureau of Economic Research, Inc*, 2014. [PDF](https://academic.oup.com/rof/article/23/1/1/5133564)
|
|
205
|
+
8. <a id="8"></a>K. Hou, H. Mo, C. Xue, and L. Zhang, ‘An Augmented q-Factor Model with Expected Growth*’, *Review of Finance*, vol. 25, no. 1, pp. 1–41, 02 2020. [PDF](https://academic.oup.com/rof/article/25/1/1/5727769)
|
|
206
|
+
9. <a id="9"></a>Z. He, B. Kelly, and A. Manela, ‘Intermediary asset pricing: New evidence from many asset classes’, *Journal of Financial Economics*, vol. 126, no. 1, pp. 1–35, 2017. [PDF](https://cpb-us-w2.wpmucdn.com/voices.uchicago.edu/dist/6/2325/files/2019/12/jfepublishedversion.pdf)
|
|
207
|
+
10. <a id="10"></a>K. Daniel, D. Hirshleifer, and L. Sun, ‘Short- and Long-Horizon Behavioral Factors’, *Review of Financial Studies*, vol. 33, no. 4, pp. 1673–1736, 2020. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3086063)
|
|
208
|
+
11. <a id="11"></a>C. Asness and A. Frazzini, ‘The Devil in HML’s Details’, *The Journal of Portfolio Management*, vol. 39, pp. 49–68, 2013. [PDF](https://stockmarketmba.com/docs/Asness_Frazzini_AdjustHML.pdf)
|
|
209
|
+
12. <a id="12"></a>F. Barillas and J. Shanken, ‘Comparing Asset Pricing Models’, *Journal of Finance*, vol. 73, no. 2, pp. 715–754, 2018. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2700000)
|
|
210
|
+
|
|
211
|
+
**Data sources:**
|
|
212
|
+
|
|
213
|
+
* K. French, "Data Library," Tuck School of Business at Dartmouth.
|
|
214
|
+
[Link](https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html)
|
|
215
|
+
* R. Stambaugh, "Liquidity" and "Mispricing" factor datasets, Wharton School, University of Pennsylvania.
|
|
216
|
+
[Link](https://finance.wharton.upenn.edu/~stambaug/)
|
|
217
|
+
* Z. He, "Intermediary Capital Ratio and Risk Factor" dataset, University of Chicago.
|
|
218
|
+
[Link](https://voices.uchicago.edu/zhiguohe/data-and-empirical-patterns/intermediary-capital-ratio-and-risk-factor/)
|
|
219
|
+
* K. Hou, G. Xue, R. Zhang, "The Hou-Xue-Zhang q-factors data library," at global-q.org.
|
|
220
|
+
[Link](http://global-q.org/factors.html)
|
|
221
|
+
* AQR Capital Management's Data Sets.
|
|
222
|
+
* Lin Sun, DHS Behavioural factors [Link](https://sites.google.com/view/linsunhome)
|
|
223
|
+
|
|
224
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+

|
|
229
|
+
|
|
230
|
+
*The code in this project is released under the [MIT License]().*
|
|
231
|
+
|
|
232
|
+
[](https://pycqa.github.io/isort/)
|
|
233
|
+
[](https://simpleicons.org/?q=ruff)
|
|
234
|
+
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
<a name="readme-top"></a>
|
|
2
|
+
|
|
3
|
+
# getfactormodels
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Reliably retrieve data for various multi-factor asset pricing models.
|
|
9
|
+
|
|
10
|
+
## Models
|
|
11
|
+
|
|
12
|
+
- The 3-factor, 5-factor, and 6-factor models of Fama & French <sup>[[1]](#1) [[3]](#3) [[4]](#4)</sup>
|
|
13
|
+
- Mark Carhart's 4-factor model <sup>[[2]](#2)</sup>
|
|
14
|
+
- Pastor and Stambaugh's liquidity factors <sup>[[5]](#5)</sup>
|
|
15
|
+
- Mispricing factors of Stambaugh and Yuan<sup>[[6]](#6)</sup>
|
|
16
|
+
- The $q$*-factor* model of Hou, Mo, Xue and Zhang<sup>[[7]](#7)</sup>
|
|
17
|
+
- The augmented $q^5$*-factor* model of Hou, Mo, Xue and Zhang<sup>[[8]](#8)</sup>
|
|
18
|
+
- *Intermediary Capital Ratio* (ICR) of He, Kelly & Manela<sup>[[9]](#9)</sup>
|
|
19
|
+
- The *DHS behavioural factors* of Daniel, Hirshleifer & Sun<sup>[[10]](#10)</sup>
|
|
20
|
+
- The *HML* $^{DEVIL}$ factor of Asness & Frazzini<sup>[[11]](#11)</sup>
|
|
21
|
+
- The 6-factor model of Barillas and Shanken<sup>[[12]](#12)</sup>
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_Thanks to: Kenneth French, Robert Stambaugh, Lin Sun, Zhiguo He, AQR Capital Management (AQR.com) and Hou, Xue and Zhang (global-q.org), for their research and for the datasets they publically provide._
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
`getfactormodels` requires Python ``>=3.7``
|
|
30
|
+
|
|
31
|
+
* Install with pip:
|
|
32
|
+
```shell
|
|
33
|
+
$ pip install getfactormodels
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
#### Python
|
|
39
|
+
|
|
40
|
+
After installing, import ``getfactormodels`` and call ``get_factors()`` with the ``model`` and ``frequency`` parameters. Optionally, specify a ``start_date`` and ``end_date``
|
|
41
|
+
* For example, to retrieve the daily q-factor model data:
|
|
42
|
+
|
|
43
|
+
```py
|
|
44
|
+
import getfactormodels as getfactormodels
|
|
45
|
+
|
|
46
|
+
df = getfactormodels.get_factors(model='q', frequency='d')
|
|
47
|
+
```
|
|
48
|
+
> _Trimmed output:_
|
|
49
|
+
```txt
|
|
50
|
+
> df
|
|
51
|
+
Mkt-RF R_ME R_IA R_ROE R_EG RF
|
|
52
|
+
date
|
|
53
|
+
1967-01-03 0.000778 0.004944 0.001437 -0.007118 -0.008563 0.000187
|
|
54
|
+
1967-01-04 0.001667 -0.003487 -0.000631 -0.002044 -0.000295 0.000187
|
|
55
|
+
1967-01-05 0.012990 0.004412 -0.005688 0.000838 -0.003075 0.000187
|
|
56
|
+
1967-01-06 0.007230 0.006669 0.008897 0.003603 0.002669 0.000187
|
|
57
|
+
1967-01-09 0.008439 0.006315 0.000331 0.004949 0.002979 0.000187
|
|
58
|
+
... ... ... ... ... ... ...
|
|
59
|
+
2022-12-23 0.005113 -0.001045 0.004000 0.010484 0.003852 0.000161
|
|
60
|
+
2022-12-27 -0.005076 -0.001407 0.010190 0.009206 0.003908 0.000161
|
|
61
|
+
2022-12-28 -0.012344 -0.004354 0.000133 -0.010457 -0.004953 0.000161
|
|
62
|
+
2022-12-29 0.018699 0.008568 -0.008801 -0.012686 -0.002162 0.000161
|
|
63
|
+
2022-12-30 -0.002169 0.001840 0.001011 -0.004151 -0.003282 0.000161
|
|
64
|
+
|
|
65
|
+
[14096 rows x 6 columns]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
* or, retreive the monthly liquidity factors of Pastor and Stambaugh for the 1990s:
|
|
69
|
+
|
|
70
|
+
```py
|
|
71
|
+
import getfactormodels as getfactormodels
|
|
72
|
+
|
|
73
|
+
df = getfactormodels.get_factors(model='liquidity', frequency='m', start_date='1990-01-01', end_date='1999-12-31')
|
|
74
|
+
```
|
|
75
|
+
> If you don't have time to type `liquidity`, type `liq`, or `ps`--there's a handy regex.
|
|
76
|
+
|
|
77
|
+
* or, saving the monthly 3-factor model of Fama & French to a file:
|
|
78
|
+
|
|
79
|
+
```py
|
|
80
|
+
import getfactormodels as gfm
|
|
81
|
+
|
|
82
|
+
df = gfm.get_factors(model='ff3', frequency='m', output="ff3_data.csv")
|
|
83
|
+
```
|
|
84
|
+
>The output parameter accepts a filename, path or directory, and can be one of csv, md, txt, xlsx, pkl.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
You can also import just the models that you need.
|
|
88
|
+
|
|
89
|
+
* For example, to import only the *ICR* and *q*-factor models:
|
|
90
|
+
|
|
91
|
+
```py
|
|
92
|
+
from getfactormodels import icr_factors, q_factors
|
|
93
|
+
|
|
94
|
+
# Passing a model function with no params defaults to monthly.
|
|
95
|
+
df = icr_factors()
|
|
96
|
+
|
|
97
|
+
# The 'q' models, and the 3-factor model of Fama-French also have weekly data.
|
|
98
|
+
df = q_factors(frequency="W", start_date="1992-01-01)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
* If using ``ff_factors()``, then an additional ``model`` parameter should be specified:
|
|
102
|
+
|
|
103
|
+
```py
|
|
104
|
+
from getfactormodels import ff_factors
|
|
105
|
+
|
|
106
|
+
# To get annual data for the 5-factor model:
|
|
107
|
+
data = ff_factors(model="5", frequency="Y", output=".xlsx")
|
|
108
|
+
|
|
109
|
+
# Daily 3-factor model data, since 1970 (not specifying an end date
|
|
110
|
+
# will return data up until today):
|
|
111
|
+
data = ff_factors(model="3", frequency="D", start_date="1970-01-01")
|
|
112
|
+
```
|
|
113
|
+
> Output allows just an extension to be specified.
|
|
114
|
+
|
|
115
|
+
* or import all the models:
|
|
116
|
+
|
|
117
|
+
```py
|
|
118
|
+
from getfactormodels import models
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
* There's also the `FactorExtractor` class that the CLI uses (it doesn't really do a whole lot yet):
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from getfactormodels import FactorExtractor
|
|
125
|
+
|
|
126
|
+
fe = FactorExtractor(model='carhart', frequency='m', start_date='1980-01-01', end_date='1980-05-01')
|
|
127
|
+
fe.get_factors()
|
|
128
|
+
fe.to_file('carhart_factors.md')
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
* _The resulting ``carhart_factors.md`` file will look like this:_
|
|
132
|
+
|
|
133
|
+
| date | Mkt-RF | SMB | HML | MOM | RF |
|
|
134
|
+
|:--------------------|---------:|--------:|--------:|--------:|-------:|
|
|
135
|
+
| 1980-01-31 00:00:00 | 0.0551 | 0.0162 | 0.0175 | 0.0755 | 0.008 |
|
|
136
|
+
| 1980-02-29 00:00:00 | -0.0122 | -0.0185 | 0.0061 | 0.0788 | 0.0089 |
|
|
137
|
+
| 1980-03-31 00:00:00 | -0.129 | -0.0664 | -0.0101 | -0.0955 | 0.0121 |
|
|
138
|
+
| 1980-04-30 00:00:00 | 0.0397 | 0.0105 | 0.0106 | -0.0043 | 0.0126 |
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
#### Using the CLI
|
|
142
|
+
* You can also use getfactormodels from the command line.
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
$ getfactormodels -h
|
|
146
|
+
|
|
147
|
+
usage: getfactormodels [-h] -m MODEL [-f FREQ] [-s START] [-e END] [-o OUTPUT] [--no_rf]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
* An example of how to use the CLI to retrieve the Fama-French 3-factor model data:
|
|
151
|
+
```bash
|
|
152
|
+
getfactormodels --model ff3 --frequency M --start-date 1960-01-01 --end-date 2020-12-31 --output "filename.csv"
|
|
153
|
+
```
|
|
154
|
+
> Accepted file extensions are .csv, .txt, .xlsx, and .md. If no extension is given, the output file will be .csv. The --output flag allows a filename, filepath or a directory. If only an extension is provided (including the . else it'll be passed as a filename), a name will be generated.
|
|
155
|
+
|
|
156
|
+
* Here's another example that retrieves the annual Fama-French 5-factor data without the RF column:
|
|
157
|
+
|
|
158
|
+
```sh
|
|
159
|
+
getfactormodels -m 5 -f Y -s 1960-01-01 -e 2020-12-31 --no_rf -o ~/some_dir/filename.xlsx
|
|
160
|
+
```
|
|
161
|
+
> `--no_rf` will return the factor model without an RF column.
|
|
162
|
+
|
|
163
|
+
## References
|
|
164
|
+
1. <a id="1"></a> E. F. Fama and K. R. French, ‘Common risk factors in the returns on stocks and bonds’, *Journal of Financial Economics*, vol. 33, no. 1, pp. 3–56, 1993. [PDF](https://people.duke.edu/~charvey/Teaching/BA453_2006/FF_Common_risk.pdf)
|
|
165
|
+
2. <a id="2"></a> M. Carhart, ‘On Persistence in Mutual Fund Performance’, *Journal of Finance*, vol. 52, no. 1, pp. 57–82, 1997. [PDF](https://onlinelibrary.wiley.com/doi/full/10.1111/j.1540-6261.1997.tb03808.x)
|
|
166
|
+
3. <a id="3"></a> E. F. Fama and K. R. French, ‘A five-factor asset pricing model’, *Journal of Financial Economics*, vol. 116, no. 1, pp. 1–22, 2015. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2287202)
|
|
167
|
+
4. <a id="4"></a> E. F. Fama and K. R. French, ‘Choosing factors’, *Journal of Financial Economics*, vol. 128, no. 2, pp. 234–252, 2018. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2668236)
|
|
168
|
+
5. <a id="5"></a>L. Pastor and R. Stambaugh, ‘Liquidity Risk and Expected Stock Returns’, *Journal of Political Economy*, vol. 111, no. 3, pp. 642–685, 2003. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=279804)
|
|
169
|
+
6. <a id="6"></a>R. F. Stambaugh and Y. Yuan, ‘Mispricing Factors’, *The Review of Financial Studies*, vol. 30, no. 4, pp. 1270–1315, 12 2016. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2626701)
|
|
170
|
+
7. <a id="7"></a>K. Hou, H. Mo, C. Xue, and L. Zhang, ‘Which Factors?’, *National Bureau of Economic Research, Inc*, 2014. [PDF](https://academic.oup.com/rof/article/23/1/1/5133564)
|
|
171
|
+
8. <a id="8"></a>K. Hou, H. Mo, C. Xue, and L. Zhang, ‘An Augmented q-Factor Model with Expected Growth*’, *Review of Finance*, vol. 25, no. 1, pp. 1–41, 02 2020. [PDF](https://academic.oup.com/rof/article/25/1/1/5727769)
|
|
172
|
+
9. <a id="9"></a>Z. He, B. Kelly, and A. Manela, ‘Intermediary asset pricing: New evidence from many asset classes’, *Journal of Financial Economics*, vol. 126, no. 1, pp. 1–35, 2017. [PDF](https://cpb-us-w2.wpmucdn.com/voices.uchicago.edu/dist/6/2325/files/2019/12/jfepublishedversion.pdf)
|
|
173
|
+
10. <a id="10"></a>K. Daniel, D. Hirshleifer, and L. Sun, ‘Short- and Long-Horizon Behavioral Factors’, *Review of Financial Studies*, vol. 33, no. 4, pp. 1673–1736, 2020. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3086063)
|
|
174
|
+
11. <a id="11"></a>C. Asness and A. Frazzini, ‘The Devil in HML’s Details’, *The Journal of Portfolio Management*, vol. 39, pp. 49–68, 2013. [PDF](https://stockmarketmba.com/docs/Asness_Frazzini_AdjustHML.pdf)
|
|
175
|
+
12. <a id="12"></a>F. Barillas and J. Shanken, ‘Comparing Asset Pricing Models’, *Journal of Finance*, vol. 73, no. 2, pp. 715–754, 2018. [PDF](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2700000)
|
|
176
|
+
|
|
177
|
+
**Data sources:**
|
|
178
|
+
|
|
179
|
+
* K. French, "Data Library," Tuck School of Business at Dartmouth.
|
|
180
|
+
[Link](https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html)
|
|
181
|
+
* R. Stambaugh, "Liquidity" and "Mispricing" factor datasets, Wharton School, University of Pennsylvania.
|
|
182
|
+
[Link](https://finance.wharton.upenn.edu/~stambaug/)
|
|
183
|
+
* Z. He, "Intermediary Capital Ratio and Risk Factor" dataset, University of Chicago.
|
|
184
|
+
[Link](https://voices.uchicago.edu/zhiguohe/data-and-empirical-patterns/intermediary-capital-ratio-and-risk-factor/)
|
|
185
|
+
* K. Hou, G. Xue, R. Zhang, "The Hou-Xue-Zhang q-factors data library," at global-q.org.
|
|
186
|
+
[Link](http://global-q.org/factors.html)
|
|
187
|
+
* AQR Capital Management's Data Sets.
|
|
188
|
+
* Lin Sun, DHS Behavioural factors [Link](https://sites.google.com/view/linsunhome)
|
|
189
|
+
|
|
190
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+

|
|
195
|
+
|
|
196
|
+
*The code in this project is released under the [MIT License]().*
|
|
197
|
+
|
|
198
|
+
[](https://pycqa.github.io/isort/)
|
|
199
|
+
[](https://simpleicons.org/?q=ruff)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# MIT License
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2023 S. Martin
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
__version__ = "0.0.1"
|
|
24
|
+
|
|
25
|
+
from .__main__ import FactorExtractor, get_factors
|
|
26
|
+
from .models import models # noqa: F401
|
|
27
|
+
from .models.models import (barillas_shanken_factors, carhart_factors,
|
|
28
|
+
dhs_factors, ff_factors, hml_devil_factors,
|
|
29
|
+
icr_factors, liquidity_factors, mispricing_factors,
|
|
30
|
+
q_classic_factors, q_factors)
|
|
31
|
+
|
|
32
|
+
__all__ = ["FactorExtractor",
|
|
33
|
+
"ff_factors",
|
|
34
|
+
"icr_factors",
|
|
35
|
+
"q_factors",
|
|
36
|
+
"q_classic_factors",
|
|
37
|
+
"mispricing_factors",
|
|
38
|
+
"dhs_factors",
|
|
39
|
+
"liquidity_factors",
|
|
40
|
+
"hml_devil_factors",
|
|
41
|
+
"barillas_shanken_factors",
|
|
42
|
+
"carhart_factors",
|
|
43
|
+
"get_factors", ]
|