Skip to content

Commit

Permalink
set setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
scrat-online committed May 11, 2017
1 parent 961ec55 commit 61b6cfe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
62 changes: 31 additions & 31 deletions pySTARMA/starma_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"""

import numpy as np
from scipy.stats import t as t_dist

from pySTARMA import utils
from pySTARMA.utils import set_stationary
from pySTARMA.utils import _set_stationary


class STARMA:
Expand Down Expand Up @@ -114,41 +113,41 @@ def _fit_model(self, ts_matrix):
# run kalman filter
# first iteration
eps = ts_matrix.copy()
self._model = utils.kf_estimation(ts_matrix,
self._wa_matrices,
eps,
self._get_ar_matrix(),
self._get_ma_matrix(),
self._max_p_tlag,
self._max_q_tlag,
self._max_tlag)
self._model = utils._kf_estimation(ts_matrix,
self._wa_matrices,
eps,
self._get_ar_matrix(),
self._get_ma_matrix(),
self._max_p_tlag,
self._max_q_tlag,
self._max_tlag)

# if ma orders present, do further iteration
if self._q > 0:
count = 0
while self._iter > count:
eps[0: self._max_tlag] = utils.residuals_estimation(ts_matrix[0:self._max_tlag],
self._wa_matrices,
self._model['phi'],
self._model['theta'])
eps[0: self._max_tlag] = utils._residuals_estimation(ts_matrix[0:self._max_tlag],
self._wa_matrices,
self._model['phi'],
self._model['theta'])

self._model = utils.kf_estimation(ts_matrix,
self._wa_matrices,
eps,
self._get_ar_matrix(),
self._get_ma_matrix(),
self._max_p_tlag,
self._max_q_tlag,
self._max_tlag)
self._model = utils._kf_estimation(ts_matrix,
self._wa_matrices,
eps,
self._get_ar_matrix(),
self._get_ma_matrix(),
self._max_p_tlag,
self._max_q_tlag,
self._max_tlag)

count += 1

# write information to model
self._model['timeseries'] = ts_matrix
self._model['residuals'] = utils.residuals_estimation(ts_matrix, self._wa_matrices, self._model['phi'],
self._model['theta'])
self._model['residuals'] = utils._residuals_estimation(ts_matrix, self._wa_matrices, self._model['phi'],
self._model['theta'])
self._model['sigma2'] = np.trace(self._model['sigma2VarianceMatrix']) / len(self._model['sigma2VarianceMatrix'])
self._model['llh'] = utils.loglikelihood(self._model) + np.log(
self._model['llh'] = utils._loglikelihood(self._model) + np.log(
ts_matrix.size * ((self._get_total_parameter()) * len(self._wa_matrices)))
self._model['bic'] = self._get_total_parameter() * np.log(ts_matrix.size) - 2 * self._model['llh']
self._model['phi_tvalue'] = self._model['phi'] / self._model['phi_sd']
Expand All @@ -163,6 +162,7 @@ def _p_value(self, t_value):
:param t_value:
:return: p-value of parameter
"""
from scipy.stats import t as t_dist
df = (self._ts_matrix.size) - (self._get_total_parameter() * len(self._wa_matrices))
# TODO check calculation of p-values with degrees of freedom
# p_value = tdist.pdf(abs(tvalue), self._total_parameter())
Expand All @@ -181,11 +181,11 @@ def predict(self, ts_matrix, t_lags):
:param t_lags: Maximum time lags in future
:return: Matrix with predictions
"""
return utils.prediction(ts_matrix
, self._wa_matrices
, self._model['phi']
, self._model['theta']
, t_lags)
return utils._prediction(ts_matrix
, self._wa_matrices
, self._model['phi']
, self._model['theta']
, t_lags)

def print_results(self):
"""
Expand Down Expand Up @@ -252,5 +252,5 @@ def fit(self):
"""
Estimate parameter for model
"""
self._ts = set_stationary(self._ts, self._d)
self._ts = _set_stationary(self._ts, self._d)
self._fit_model(self._ts)
12 changes: 6 additions & 6 deletions pySTARMA/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from numpy.linalg import inv


def loglikelihood(model):
def _loglikelihood(model):
"""
Calculates Log-Likelihood of model
:param ts_matrix: Matrix of timeseries
Expand All @@ -30,7 +30,7 @@ def loglikelihood(model):
return -llh / 2


def prediction(ts_matrix, wa_matrices, phi, theta, prediction_lag=1):
def _prediction(ts_matrix, wa_matrices, phi, theta, prediction_lag=1):
"""
Calcuates predictions for time series matrix
:param ts_matrix: time series matrix
Expand All @@ -41,7 +41,7 @@ def prediction(ts_matrix, wa_matrices, phi, theta, prediction_lag=1):
:return:
"""
predictions = ts_matrix[:ts_matrix.shape[0] - prediction_lag, :].copy()
residuals = residuals_estimation(predictions, wa_matrices, phi, theta)
residuals = _residuals_estimation(predictions, wa_matrices, phi, theta)

for h in range(prediction_lag):
predict = 0
Expand All @@ -58,7 +58,7 @@ def prediction(ts_matrix, wa_matrices, phi, theta, prediction_lag=1):
return predictions


def kf_estimation(ts_matrix, wa_matrices, residuals, ar_matrix, ma_matrix, p_lag, q_lag, max_t_lag):
def _kf_estimation(ts_matrix, wa_matrices, residuals, ar_matrix, ma_matrix, p_lag, q_lag, max_t_lag):
"""
Estimate parameters and variance with kalman filtering. After implementation of the Kalman Filter
by Cipra & Motykova - Study on Kalman filter in time series anlysis (1987) and
Expand Down Expand Up @@ -134,7 +134,7 @@ def kf_estimation(ts_matrix, wa_matrices, residuals, ar_matrix, ma_matrix, p_lag
'sigma2VarianceMatrix': sigma2, }


def residuals_estimation(ts_matrix, wa_matrices, phi, theta):
def _residuals_estimation(ts_matrix, wa_matrices, phi, theta):
"""
Calculation of residuals for model
:param ts_matrix: time series matrix
Expand All @@ -159,7 +159,7 @@ def residuals_estimation(ts_matrix, wa_matrices, phi, theta):
return residuals


def set_stationary(ts_matrix, lags):
def _set_stationary(ts_matrix, lags):
"""
Differencing of time series
:param ts_matrix: time series matrix
Expand Down

0 comments on commit 61b6cfe

Please sign in to comment.