<s> from . . import inference as ifr <EOL> from . . import distributions as dst <EOL> from . . import output as op <EOL> from . . import tests as tst <EOL> from . . import tsm as tsm <EOL> from . . import data_check as dc <EOL> import numpy as np <EOL> import pandas as pd <EOL> import scipy . stats as ss <EOL> from math import exp , sqrt , log , tanh , pi <EOL> import copy <EOL> from scipy import optimize <EOL> import matplotlib . pyplot as plt <EOL> import seaborn as sns <EOL> import numdifftools as nd <EOL> from kalman import * <EOL> class LLEV ( tsm . TSM ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , integ = <NUM_LIT:0> , target = None ) : <EOL> tsm . TSM . __init__ ( self , '<STR_LIT>' ) <EOL> self . integ = integ <EOL> self . param_no = <NUM_LIT:2> <EOL> self . max_lag = <NUM_LIT:0> <EOL> self . hess_type = '<STR_LIT>' <EOL> self . param_hide = <NUM_LIT:0> <EOL> self . supported_methods = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . default_method = "<STR_LIT>" <EOL> self . model_name = "<STR_LIT>" <EOL> self . data , self . data_name , self . data_type , self . index = dc . data_check ( data , target ) <EOL> X = self . data <EOL> for order in range ( self . integ ) : <EOL> X = np . diff ( X ) <EOL> self . data_name = "<STR_LIT>" + self . data_name <EOL> self . data = X <EOL> self . data_length = X <EOL> self . cutoff = <NUM_LIT:0> <EOL> self . param_desc . append ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:index>' : <NUM_LIT:0> , '<STR_LIT>' : ifr . Uniform ( transform = '<STR_LIT>' ) , '<STR_LIT:q>' : dst . Normal ( <NUM_LIT:0> , <NUM_LIT:3> ) } ) <EOL> self . param_desc . append ( { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:index>' : <NUM_LIT:1> , '<STR_LIT>' : ifr . Uniform ( transform = '<STR_LIT>' ) , '<STR_LIT:q>' : dst . Normal ( <NUM_LIT:0> , <NUM_LIT:3> ) } ) <EOL> def model ( self , beta ) : <EOL> """<STR_LIT>""" <EOL> T = np . identity ( <NUM_LIT:1> ) <EOL> R = np . identity ( <NUM_LIT:1> ) <EOL> Z = np . identity ( <NUM_LIT:1> ) <EOL> H = np . identity ( <NUM_LIT:1> ) * self . param_desc [ <NUM_LIT:0> ] [ '<STR_LIT>' ] . transform ( beta [ <NUM_LIT:0> ] ) <EOL> Q = np . identity ( <NUM_LIT:1> ) * self . param_desc [ <NUM_LIT:1> ] [ '<STR_LIT>' ] . transform ( beta [ <NUM_LIT:1> ] ) <EOL> return univariate_kalman ( self . data , Z , H , T , Q , R ) <EOL> def likelihood ( self , beta ) : <EOL> _ , F , v = self . model ( beta ) <EOL> loglik = <NUM_LIT:0> <EOL> for i in xrange ( <NUM_LIT:0> , self . data . shape [ <NUM_LIT:0> ] ) : <EOL> loglik += np . linalg . slogdet ( F [ : , : , i ] ) [ <NUM_LIT:1> ] + np . dot ( v [ i ] , np . dot ( np . linalg . pinv ( F [ : , : , i ] ) , v [ i ] ) ) <EOL> return - ( - ( ( self . data . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> ) * log ( <NUM_LIT:2> * pi ) ) - <NUM_LIT:0.5> * loglik . T [ <NUM_LIT:0> ] . sum ( ) ) <EOL> def forecast_model ( self , beta , h ) : <EOL> """<STR_LIT>""" <EOL> T = np . identity ( <NUM_LIT:1> ) <EOL> R = np . identity ( <NUM_LIT:1> ) <EOL> Z = np . identity ( <NUM_LIT:1> ) <EOL> H = np . identity ( <NUM_LIT:1> ) * self . param_desc [ <NUM_LIT:0> ] [ '<STR_LIT>' ] . transform ( beta [ <NUM_LIT:0> ] ) <EOL> Q = np . identity ( <NUM_LIT:1> ) * self . param_desc [ <NUM_LIT:1> ] [ '<STR_LIT>' ] . transform ( beta [ <NUM_LIT:1> ] ) <EOL> return univariate_kalman_fcst ( self . data , Z , H , T , Q , R , h ) <EOL> def predict ( self , h = <NUM_LIT:5> , past_values = <NUM_LIT:20> , intervals = True , ** kwargs ) : <EOL> figsize = kwargs . get ( '<STR_LIT>' , ( <NUM_LIT:10> , <NUM_LIT:7> ) ) <EOL> """<STR_LIT>""" <EOL> if len ( self . params ) == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> a , P = self . forecast_model ( self . params , h ) <EOL> date_index = self . shift_dates ( h ) <EOL> plot_values = a [ <NUM_LIT:0> ] [ len ( a [ <NUM_LIT:0> ] ) - h - past_values : len ( a [ <NUM_LIT:0> ] ) ] <EOL> forecasted_values = a [ <NUM_LIT:0> ] [ len ( a [ <NUM_LIT:0> ] ) - h : len ( a [ <NUM_LIT:0> ] ) ] <EOL> lower = forecasted_values - <NUM_LIT> * np . power ( P [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ len ( P [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) - h : len ( P [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ] , <NUM_LIT:0.5> ) <EOL> upper = forecasted_values + <NUM_LIT> * np . power ( P [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ len ( P [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) - h : len ( P [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ] , <NUM_LIT:0.5> ) <EOL> lower = np . append ( plot_values [ len ( plot_values ) - h - <NUM_LIT:1> ] , lower ) <EOL> upper = np . append ( plot_values [ len ( plot_values ) - h - <NUM_LIT:1> ] , upper ) <EOL> plot_index = date_index [ len ( date_index ) - h - past_values : len ( date_index ) ] <EOL> plt . figure ( figsize = figsize ) <EOL> if intervals == True : <EOL> plt . fill_between ( date_index [ len ( date_index ) - h - <NUM_LIT:1> : len ( date_index ) ] , lower , upper , alpha = <NUM_LIT> ) <EOL> plt . plot ( plot_index , plot_values ) <EOL> plt . title ( "<STR_LIT>" + self . data_name ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( self . data_name ) <EOL> plt . show ( ) <EOL> self . predictions = { '<STR_LIT>' : np . array ( [ lower , upper ] ) , '<STR_LIT>' : forecasted_values , '<STR_LIT>' : plot_values , '<STR_LIT>' : plot_index } <EOL> def plot_fit ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> figsize = kwargs . get ( '<STR_LIT>' , ( <NUM_LIT:10> , <NUM_LIT:7> ) ) <EOL> if len ( self . params ) == <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> date_index = self . index <EOL> mu , _ , _ = self . model ( self . params ) <EOL> mu = mu [ <NUM_LIT:0> ] [ <NUM_LIT:0> : len ( mu [ <NUM_LIT:0> ] ) - <NUM_LIT:1> ] <EOL> plt . figure ( figsize = figsize ) <EOL> plt . subplot ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> plt . title ( self . data_name + "<STR_LIT>" ) <EOL> plt . plot ( date_index , self . data , label = '<STR_LIT>' ) <EOL> plt . plot ( date_index , mu , label = '<STR_LIT>' , c = '<STR_LIT>' ) <EOL> plt . legend ( loc = <NUM_LIT:2> ) <EOL> plt . subplot ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> plt . title ( self . data_name + "<STR_LIT>" ) <EOL> plt . plot ( date_index , mu ) <EOL> plt . subplot ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> plt . title ( "<STR_LIT>" ) <EOL> plt . plot ( date_index , self . data - mu ) <EOL> plt . show ( ) </s>
