Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forecasting.ipynb - #189 time-series analysis using kats. #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .replit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language = "python3"
run = ""
1 change: 1 addition & 0 deletions gs_quant/timeseries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
from .measures_xccy import *
from .measures_fx_vol import *
from .helper import *
from .kats import *

__name__ = 'timeseries'
8 changes: 5 additions & 3 deletions gs_quant/timeseries/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from gs_quant.datetime import relative_date_add
from gs_quant.timeseries.datetime import *
from .helper import plot_function

"""
Timeseries analysis library contains functions used to analyze properties of timeseries, including laging, differencing,
autocorrelation, co-integration and other operations
Expand Down Expand Up @@ -196,7 +195,9 @@ class LagMode(Enum):


@plot_function
def lag(x: pd.Series, obs: Union[Window, int, str] = 1, mode: LagMode = LagMode.EXTEND) -> pd.Series:
def lag(x: pd.Series,
obs: Union[Window, int, str] = 1,
mode: LagMode = LagMode.EXTEND) -> pd.Series:
"""
Lag timeseries by a number of observations or a relative date.

Expand Down Expand Up @@ -248,7 +249,8 @@ def lag(x: pd.Series, obs: Union[Window, int, str] = 1, mode: LagMode = LagMode.
# Determine how we want to handle observations prior to start date
if mode == LagMode.EXTEND:
if x.index.resolution != 'day':
raise MqValueError(f'unable to extend index with resolution {x.index.resolution}')
raise MqValueError(
f'unable to extend index with resolution {x.index.resolution}')
kwargs = {'periods': abs(obs) + 1, 'freq': 'D'}
if obs > 0:
kwargs['start'] = x.index[-1]
Expand Down
99 changes: 99 additions & 0 deletions gs_quant/timeseries/forecasting.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright 2018 Goldman Sachs.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#
# Marquee Plot Service will attempt to make public functions (not prefixed with _) from this module available.
# Such functions should be fully documented: docstrings should describe parameters and the return value, and provide
# a 1-line description. Type annotations should be provided for parameters.
#
#Kats (Kits to Analyze Time Series) is a light-weight, easy-to-use,extenable, and generalizable framework to perform time series analysis in Python. Time series analysis is an essential component of data science and engineering work.


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from kats.consts import TimeSeriesData
from gs_quant.datetime import relative_date_add
from gs_quant.timeseries.datetime import *
from .helper import plot_function




# Note: If the column holding the time values is not called time, you will want to specify the name of this column.
gs_quant.columns = ["time", "value"]
ts = TimeSeriesData(gs_quant)

from kats.models.sarima import SARIMAModel, SARIMAParams


# create SARIMA param class
params = SARIMAParams(
p = 2,
d=1,
q=1,
trend = 'cti',
seasonal_order=(1,0,1,12)
)

# initiate SARIMA model
m = SARIMAModel(data=ts, params=params)

# fit SARIMA model
m.fit()

# generate forecast values
fcst = m.predict(
steps=30,
freq="MS"
)

# make plot to visualize
m.plot()


# import the param and model classes for Prophet model
from kats.models.prophet import ProphetModel, ProphetParams

# create a model param instance
params = ProphetParams(seasonality_mode='multiplicative') # additive mode gives worse results

# create a prophet model instance
m = ProphetModel(ts, params)

# fit model simply by calling m.fit()
m.fit()

# make prediction for next 30 month
fcst = m.predict(steps=30, freq="MS")

# plot to visualize
m.plot()

from kats.models.holtwinters import HoltWintersParams, HoltWintersModel


params = HoltWintersParams(
trend="add",
#damped=False,
seasonal="mul",
seasonal_periods=12,
)
m = HoltWintersModel(
data=ts,
params=params)

m.fit()

fcst = m.predict(steps=20, alpha = 0.3)
m.plot()
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@
"internal": ["gs_quant_internal>=1.1.30", "requests_kerberos"],
"turbo": ["quant-extensions"],
"notebook": ["jupyter", "matplotlib~=3.1.0", "seaborn", "treelib"],
"test": ["pytest", "pytest-cov", "pytest-mock", "testfixtures", "nbconvert", "nbformat", "jupyter_client"],
"develop": ["wheel", "sphinx", "sphinx_rtd_theme", "sphinx_autodoc_typehints", "pytest", "pytest-cov",
"pytest-mock", "testfixtures"]
"test": [
"pytest", "pytest-cov", "pytest-mock", "testfixtures", "nbconvert",
"nbformat", "jupyter_client"
],
"develop": [
"wheel", "sphinx", "sphinx_rtd_theme", "sphinx_autodoc_typehints",
"pytest", "pytest-cov", "pytest-mock", "testfixtures"
]
},
classifiers=[
"Programming Language :: Python :: 3",
Expand Down