The chart table is not returning any values for OLS regression. Full code below and I have highlighted in bold for the code on "OLS Regression[3]".
import getpass
import refinitiv.dataplatform as rdp # the RDP library for Python
import pandas as pd
import numpy as np
import cufflinks as cf # to plot graph by pandas data frame
APP_KEY = getpass.getpass("Enter the app key: ")
rdp.open_desktop_session(APP_KEY)
RICs = ["USCPI=ECI","JPCPI=ECI","PHCPI=ECI"] # the list of RICs
s_date = "2018-01-02" # start date
e_date = "2018-12-30" # end date
#TBD - remove later pd.set_option('display.max_columns', None)
lastPriceField = "VALUE" # the last price field of these RICs
data = pd.DataFrame() # define data is a DataFrame
for aRIC in RICs: # request daily last price for each RIC
df= rdp.get_historical_price_summaries(aRIC,start=s_date,end=e_date,interval = rdp.Intervals.MONTHLY,fields=[lastPriceField])
if df is None: # check if there is any error
print("Error for RIC " + aRIC + ":" + str(rdp.get_last_status()['error'])) # print the error
else:
df[lastPriceField] = df[lastPriceField].astype(float) # convert string type to float
data[aRIC] = df[lastPriceField] # create the RIC's last price column
data # display daily last price of the RICs
data.dropna(inplace=True) # deletes rows with NaN values
data.index.names = ['DATE'] # set index name to be 'DATE'
data.sort_values(by=['DATE'], inplace=True, ascending=True) # sort prices according to the 'DATE' column in ascending order
data #display sorted prices
logReturns = np.log(data / data.shift(1)) # calculate the Log Returns
logReturns # display the result
cf.set_config_file(offline=True)
logReturns.iplot(kind='line', subplots=True)
data.corr() # calculate the Correlation Matrix.
data.corr().iplot(kind='line')
#No chart is returning
def add_lags(data, ric, lags):
cols = []
df = pd.DataFrame(data[ric]) #create data frame of the RIC
for lag in range(1, lags + 1):
col = 'lag_{}'.format(lag) # defines the column name
df[col] = df[ric].shift(lag) # creates the lagged data column
cols.append(col) # stores the column name
df.dropna(inplace=True) # gets rid of incomplete data rows
return df, cols
dfs = {}
for ric in RICs:
df, cols = add_lags(data, ric, 5) # create the lagged data of a RIC
dfs[ric] = df # create the RIC's logged data column
print(dfs[ric]) # print the lagged data of the RIC
regs = {}
for ric in RICs:
df = dfs[ric] # getting logged data of the RIC
reg = np.linalg.lstsq(df[cols], df[ric], rcond=-1)[0] # the OLS regression
regs[ric] = reg # storing the results
print('{:10} | {}'.format(ric, regs[ric])) # print the results
for ric in RICs:
res = pd.DataFrame(dfs[ric][ric]) # pick the data frame of the original time series
res['PRED'] = np.dot(dfs[ric][cols], regs[ric]) # creates the "prediction" values
layout1 = cf.Layout(height=450,width=1000)# Define a Layout with desired height and width
res.iloc[-50:].iplot(layout=layout1) # plot the graph
Thanks!