GithubHelp home page GithubHelp logo

fagan2888 / mixture_models Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blackarbsceo/mixture_models

0.0 1.0 0.0 19.75 MB

Jupyter (IPython) notebooks for exploring mixture models

Jupyter Notebook 92.65% Python 7.35%

mixture_models's Introduction

Can We Use Mixture Models to Predict Market Bottoms?

This repo contains the code and supporting data to the webinar presented via QuantInsti.com April 25, 2017

Clone or download the repo


To import the GMM prediction dataframe and output

model_data = data/GMM_Results_TidyData.h5

import pandas as pd

df = pd.read_hdf(model_data, 'table')
print(df.head())

tidydf_head_github

lookback of 999 indicates an expanding lookback window.


To import the raw return data:

market_data = data/mixture_model_merged_data_03.h5

import pandas as pd

df = pd.read_hdf(market_data, 'table')
print(df.head())

mktdata_head_github


To run the model yourself and experiment with parameters:

import ModelRunner
import ResultEval 
import ModelPlots

DIR = '/YOUR/PROJECT/DIR/'

# Model Params
# ------------
f1 = 'TEDRATE' # ted spread
f2 = 'T10Y2Y' # constant maturity ten yer - 2 year
f3 = 'T10Y3M' # constant maturity 10yr - 3m
factors = [f1, f2, f3]

a, b = (.2, .7) # found via coarse parameter search
alpha = 0.99
max_iter = 50

init = 'random' # or 'kmeans'
nSamples = 1_000
year = 2007 # cutoff
lookback = 1 # years

# k = 3 # n_components
# step_fwd = 3 # days
step_fwds = [1, 2, 3, 5, 7, 10, 21]
ks = [2,3,5,7,9,13,17,21]

chosen_syms = ['SPY', 'QQQ', 'TLT', 'GLD', 'EFA', 'EEM']
for k in ks:
    for step in step_fwds:
        for mkt in chosen_syms:
            p('-'*79)
            p('fitting:', mkt)
            p(f'params: k = {k} | step = {step} | lookback = {lookback}')
            p('...')
            ft_cols = [mkt + '_lret'] + factors

            MR_kwds = dict(ft_cols=ft_cols, k=k, init=init, max_iter=max_iter)
            MR = ModelRunner.ModelRunner(data, **MR_kwds)
            dct = MR.prediction_cycle(year, alpha, a, b,
                                        nSamples, lookback, mkt)

            res = ResultEval.ResultEval(dct, step_fwd=step)
            event_dict = res.get_event_states()
            event = list(event_dict.keys())[1] #[1] # TOO_LOW

            post_events = res.get_post_events(event_dict[event])
            end_vals = res.get_end_vals(post_events)
            smry = res.create_summary(end_vals)

            p()
            p('*'*25)
            p(mkt, event.upper())
            p(smry.T)  

            mp = ModelPlots.ModelPlots(mkt, post_events, event, DIR, year)
            agg_tmp_df = mp._agg_temp_event_returns()            
            mp.plot_pred_results(dct['pred'], dct['year'], dct['a'], dct['b'])
            mp.plot_equity_curve(agg_tmp_df, benchmark=data['SPY_lret'])
            mp.plot_distplot(end_vals, smry)
            break
        break
    break

spy-too-low-summary-github-sample

jsu -spy-predictionplot--2017-04-25 17 14

jsu -spy-too_low-equitycurve-2017-04-25 17 14

jsu -spy-too_low-distplot--2017-04-25 17 14

mixture_models's People

Contributors

blackarbsceo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.