Fecon235
Notebooks for financial economics. Keywords: Jupyter notebook pandas Federal Reserve FRED Ferbus GDP CPI PCE inflation unemployment wage income debt Case-Shiller housing asset portfolio equities SPX bonds TIPS rates currency FX euro EUR USD JPY yen XAU gold Brent WTI oil Holt-Winters time-series forecasting statistics econometrics
Install / Use
/learn @rsvp/Fecon235README
fecon235 :: Notebooks for financial economics
/ [CHANGELOG] / [BSD License and TOS][235li]
Spin-off Notice: the modules and functions used by our research notebooks
have been refactored into another repository, [fecon236].
The home for our Jupyter notebooks will remain here at [fecon235]
in the nb directory.
What is this repository for?
fecon235 provides an interface for financial economics to the Python ecosystem, especially packages for mathematics, statistics, science, engineering, and data analysis. Complex packages such as numpy, pandas, statsmodels, scipy, and matplotlib are seamlessly integrated at a high-level with APIs of various data hosts for:
-
Essential commands which correctly handle annoying low-level pitfalls.
-
Retrieval of economic and financial data, both historical and the most current.
-
Data munging, for example, resampling and alignment of time-series data from hosts using mutually incompatible formats.
-
Analysis using techniques from econometrics, time-series analysis, and statistical machine learning.
-
Abstraction and software optimization of mathematical operators, for example, linear algebra used in portfolio analysis.
-
Visualization of data using graphical packages.
-
Reproducible research which is collaborative and openly accessible at zero cost.
To practically test theoretical ideas interactively, our code can employed with any Python IDE interactive development environment, IPython console, or with a Jupyter notebook. The code has been tested against both python27 and python3 since 2014, and works across major platforms: Linux, Mac, and Windows.
Database: the primary source is [FRED], the U.S. Federal Reserve
Economic Data bank which is accessed directly online using our interface.
Other specialized data can be directly retrieved using our [Quandl] API
module, for example, futures prices. Data for stocks, mutual funds, and
ETFs is retrieved from the best available sources using pandas_datareader.
Data is designed to be accessible free of charge,
and interoperable in different time frequencies.
How does one get started?
-
Good introductory lectures for economists to Python and its ecosystem: Quantitative Economics by Thomas [Sargent].
-
For the fecon235 and fecon236 installation FAQ, please see https://git.io/econ which also covers external dependencies.
-
For the older pre-2016 notebooks, please use import style discussed in https://git.io/fecon-intro
-
Docker container (optional): instantly run fully-configured programs and interactive notebooks; start by:
docker pull rsvp/fecon235# see our [Docker] image for details.
Some basic commands like get() and plot()
will do a lot of the heavy lifting to get you started immediately.
The commands are very easy to customize,
producing sophisticated results quickly
without tweaking the underlying numerical packages.
The docs directory should be gradually adding tutorials. The source code, in the meantime, is thoroughly annotated.
Examples of code
The best way to see the code in action is to
run the notebooks in the nb directory
which are described further below.
Note that GitHub can render Jupyter notebooks directly in the browser,
however, they will not be executable.
Here is a rendering of a notebook at GitHub for Housing economy, home prices and affordibility https://git.io/housing If you had executed that notebook locally, it would have also retrieved the latest available data and recomputed the results.
How is worker's wage correlated with GDP output? See https://git.io/gdpwage How much Federal debt must each worker assume? And how fast is the US government debt increasing? https://git.io/debtpop
To score the Federal Reserve's performance under its dual mandate for inflation and unemployment, see https://git.io/fed (where tangentially the Phillips curve is discredited by constructing heat map scatter plots). Notebook https://git.io/infl gives an in-depth analysis of inflation, including a combined forecast using three orthogonal methods.
Elevated default risk across bond markets is indicative of a weak economy. But how can a policy maker calibrate credit spreads to assess changes in interest rates? We consider mortgage and corporate credit spreads to construct a robust Unified Credit Profile (a tutorial on MAD, Median Absolute Deviation, in rescaling non-Gaussian time-series), see https://git.io/creditprof
Please see https://git.io/fedfunds to forecast the Fed Funds rate using futures contracts on LIBOR.
The notebook https://git.io/cotr discerns how various asset classes are positioned in the market. In contrast, an overview of asset prices is given in https://git.io/georet using geometric mean returns.
In https://git.io/gold we make a conjecture that real gold prices is a stationary time-series bound by real interest rates. In https://git.io/xbt Bitcoin is statistically analyzed as a financial asset. We examine the crude oil markets, specifically the Brent over WTI spread, and construct an optimal portfolio, in https://git.io/oil .
SEC 13F filings can be easily parsed, see https://git.io/13F where we track asset managers Stanley Druckenmiller and John Paulson.
In https://git.io/equities we examine the separable components of total return for equities, especially due to enterprise earnings and market speculation, using S&P data assembled by Robert Shiller which goes back to the year 1871. In https://git.io/gdpspx we examine the close relationship between the real economy and the equities market, while demonstrating the Holt-Winters time-series model for predictions.
In https://git.io/gmix we analytically and visually show how a Gaussian Mixture model handles "fat tail" risk of leptokurtotic financial assets under small-sample conditions. Markowitz portfolios, designed in the arithmetic mean-variance framework for a static period, are notoriously fragile when markets change. In contrast, our Boltzmann portfolios are adaptive over multi-periods to geometrically maximize wealth using techniques from reinforcement learning. Part 1: https://git.io/boltz1 Part 2: https://git.io/boltz2
Bootstrapping has two benefits: small-sample statistics and simulation from controlled population. In https://git.io/bootspx we simulate alternate histories for leptokurtotic SPX equity returns: to visualize sample price paths, and for estimating probabilities of events such as investment loss.
Development and contacts
-
Guidelines: we welcome your [pull request] to improve our code. Details are outlined in [Development].
-
Lead developer is Adriano [rsvp.github.com][admin]. Please join our chat with fellow users and developers at [Gitter].
This project is a derivative from the seminar series held at the University of California at Berkeley, jointly sponsored by the Department of Economics and the Haas School of Business. We are also grateful to [BIDS], Berkeley Institute for Data Science, and the [Mathematical Sciences Group][MathSci] for their technical support.
Partial contents of nb directory
gauss-mix-kurtosis.ipynb : Gaussian Mixture and Leptokurtotic Assets
Gaussian Mixture GM(n) can create distributions with leptokurtosis ("fat tails"). Our problem is the inverse: from observable statistics, deduce the model parameters analytically. We demonstrate how a GM(2) model can synthesize Gaussian risk-equivalence for leptokurtotic financial assets. A numerical solution provides accurate probabilities which can be used to experimentally understand how kurtosis itself is distributed under small-sample conditions. The non-Gaussian distributions are visualized through quantile-quantile probability plots. Shortcut: https://git.io/gmix
boots-eq-spx.ipynb : Bootstrap leptokurtotic SPX equity returns
Bootstrapping has two benefits: small-sample statistics and simulation from controlled population. We simulate alternate histories: to visualize sample price paths, and for estimating probabilities of events such as investment loss. Bootstrapping facilitates study of small-sample behaviour for which asymptotic statistical theory is unsuitable, or where closed-form mathematical analysis is intractable, for example, geovolatility which is the volatility of the geometric mean rate. Shortcut: https://git.io/bootspx
prtf-boltzmann-1.ipynb : Boltzmann portfolios
We develop an alternative to the Markowitz framework called Boltzmann portfolios which handle uncertainty from the standpoint of cross-entropy and optimal sequential decisions. The improved result is a faster online algorithm which is more robust. Markowitz portfolios are designed in the arithmetic mean-variance framework for a static period, and are fragile to changing market conditions. In contrast, Boltzmann portfolios are adaptive over multi-periods to geometrically maximize wealth using techniques from reinforcement learning. Part 1: https://git.io/boltz1 Part 2: https://git.io/boltz2
qdl-spx-earn-div.ipynb : Separable components of total return for equities
We specify a model for equity returns by decomposition into enterprise and speculative returns, plus dividend yield. That model is then tested using stock market data going back to the year 1871 (well-known database assembled by Robert Shiller). An understanding of their respective contributions helps
