Ejemplo n.º 1
0
## needed for Granger causality test
from statsmodels.tsa.stattools import grangercausalitytests
from statsmodels.tsa.stattools import adfuller
#from statsmodels.tsa.stattools import kpss
## import the csv reader for Yahoo finance data
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/home/jjwalker/Desktop/finance/codes/data_cleaning')
from yahoo_csv_reader import yahoo_csv_reader
## import the csv reader for FRED data
from fred_csv_reader import fred_csv_reader

## path where the various data files are located.
spx_path = '/home/jjwalker/Desktop/finance/data/stocks/'
fed_path = '/home/jjwalker/Desktop/finance/data/us_economic_data/'

fed_bs = fred_csv_reader(fed_path + 'WALCL')
spx = yahoo_csv_reader(spx_path + '^GSPC', '^GSPC')

## Want to execute in python shell? then use:
#execfile('/home/jjwalker/Desktop/finance/codes/stocks/granger_causality.py')

## concatenate the close price for the two (or more) securities and find the correlation.
common_values = pd.concat([fed_bs.WALCL, spx.Close], axis=1)
## get rid of any nans?? Or, resample based on WALCL?
common_values.dropna(inplace=True)
## choose a startdate?
start_date = pd.to_datetime('2009-01-02')
## calculate pearson correlation coefficient, easy peasy:
corr_coef = common_values[common_values.index > start_date].corr(
    method='pearson')
Ejemplo n.º 2
0
import numpy as np
import matplotlib.pyplot as plt
import sys
#from scipy import interpolate
from scipy.interpolate import interp1d
## insert the path corresponding to bond_price; we will need this function!
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/home/jjwalker/Desktop/finance/codes/bonds')
from bond_price import bond_price
## import the csv reader for FRED data
sys.path.insert(1, '/home/jjwalker/Desktop/finance/codes/data_cleaning')
from fred_csv_reader import fred_csv_reader

filename = '/home/jjwalker/Desktop/finance/data/bonds/fredgraph_01_may_2020'

yc = fred_csv_reader(filename)
## get recession dates?
recession_dates = fred_csv_reader(
    '/home/jjwalker/Desktop/finance/data/us_economic_data/fred_recession_dates'
)
## get fed funds rate?
fedfunds = fred_csv_reader(
    '/home/jjwalker/Desktop/finance/data/us_economic_data/FEDFUNDS')
## change some column names:
yc.rename(columns={
    'DGS1MO': '0.083',
    'DGS3MO': '0.25',
    'DGS6MO': '0.5',
    'DGS1': '1',
    'DGS2': '2',
    'DGS3': '3',
Ejemplo n.º 3
0
wheat_analysis.py
	load wheat prices (from FRED) and Oceanic Nino Index

"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
import datetime
from datetime import timedelta
from dateutil.relativedelta import relativedelta
## insert the path corresponding to bond_price; we will need this function!
# insert at 1, 0 is the script path (or '' in REPL)
## import the csv reader for FRED data
sys.path.insert(1, '/home/jjwalker/Desktop/finance/codes/data_cleaning')
from fred_csv_reader import fred_csv_reader

## import wheat prices
filename = '/home/jjwalker/Desktop/finance/data/commodities/WPU0121'
wt = fred_csv_reader(filename)

oni = pd.read_csv(
    '/home/jjwalker/Desktop/finance/data/commodities/monthly_oni.csv',
    header=0)
oni['PeriodNum'] = pd.to_datetime(oni.PeriodNum, infer_datetime_format=True)
## This rename is not really needed.
#df=df.rename(columns={'DATE':'date'})
oni = oni.set_index('PeriodNum')
oni = oni[oni.columns].apply(pd.to_numeric, errors='coerce')
Ejemplo n.º 4
0
## calculate market prices right now, before we even start:
pzc = par_val / ((1 + zc[zc.columns[67:97]] / 200.0)**2.0)**(range(1, 31))
## 30 year STRIPS start date
strips30_start = pd.Timestamp('1985-12-02')
## Duration of zero-coupon bond that you want to buy when yield curve inverts?
## My thought was to use 30 year STRIPS, but the lack of issuance between
## February 18, 2002 until February 9, 2006 suggests using 26 for the sake of
## continuity.
## Default: does not allow lower duration 10 years as an input
strips_duration = 10
pzc_buy = pzc[pzc.columns[strips_duration - 1]]

## regular yield curve data from FRED:
#filename='/home/jjwalker/Desktop/finance/data/bonds/fredgraph_01_may_2020'
filename = '/home/jjwalker/Desktop/finance/data/bonds/fredgraph_2020_dec_31'
yc = fred_csv_reader(filename)
## change some column names:
yc.rename(columns={
    'DGS1MO': '0.083',
    'DGS3MO': '0.25',
    'DGS6MO': '0.5',
    'DGS1': '1',
    'DGS2': '2',
    'DGS3': '3',
    'DGS5': '5',
    'DGS7': '7',
    'DGS10': '10',
    'DGS20': '20',
    'DGS30': '30',
},
          inplace=True)