Skip to content

Commit 698ded5

Browse files
SiegeLordExtensorflower-gardener
authored andcommitted
InferenceGym: Add log-return S&P500 VectorizedStochasticVolatilityModel.
PiperOrigin-RevId: 379575684
1 parent 3d07421 commit 698ded5

File tree

11 files changed

+8213
-10
lines changed

11 files changed

+8213
-10
lines changed

spinoffs/inference_gym/inference_gym/internal/data.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
'convection_lorenz_bridge',
3131
'german_credit_numeric',
3232
'radon',
33-
'sp500_closing_prices',
33+
'sp500_returns',
34+
'sp500_log_returns',
3435
'synthetic_item_response_theory',
3536
'synthetic_log_gaussian_cox_process',
3637
'synthetic_plasma_spectroscopy',
@@ -427,9 +428,12 @@ def load_dataset():
427428
)
428429

429430

430-
def sp500_closing_prices(num_points=None):
431+
def sp500_returns(num_points=None):
431432
"""Dataset of mean-adjusted returns of the S&P 500 index.
432433
434+
The returns here are the absolute returns (current price minus previous
435+
price).
436+
433437
Each of the 2516 entries represents the adjusted return of the daily closing
434438
price relative to the previous close, for each (non-holiday) weekday,
435439
beginning 6/26/2010 and ending 6/24/2020.
@@ -448,6 +452,31 @@ def sp500_closing_prices(num_points=None):
448452
centered_returns=returns[-num_points:] - np.mean(returns[-num_points:]))
449453

450454

455+
def sp500_log_returns(num_points=None):
456+
"""Dataset of mean-adjusted log returns of the S&P 500 index.
457+
458+
A log return is the change in the log-transformed closing prices, which also
459+
corresponds to the log of relative returns (current price divided by previous
460+
price).
461+
462+
Each of the 2516 entries represents the adjusted log return of the daily
463+
closing price relative to the previous close, for each (non-holiday) weekday,
464+
beginning 6/26/2010 and ending 6/24/2020.
465+
466+
Args:
467+
num_points: Optional `int` length of the series to return. If specified,
468+
only the final `num_points` returns are centered and returned.
469+
Default value: `None`.
470+
Returns:
471+
dataset: A Dict with the following keys:
472+
`centered_returns`: float `Tensor` daily returns, minus the mean return.
473+
"""
474+
returns = np.diff(np.log(sp500_closing_prices_lib.CLOSING_PRICES))
475+
num_points = num_points or len(returns)
476+
return dict(
477+
centered_returns=returns[-num_points:] - np.mean(returns[-num_points:]))
478+
479+
451480
def synthetic_item_response_theory(
452481
train_fraction=1.,
453482
shuffle=True,

spinoffs/inference_gym/inference_gym/internal/data_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ def load_dataset_in_xla():
6060
def testStochasticVolatilityModelSP500(self):
6161
num_train_points = 2516
6262

63-
dataset = data.sp500_closing_prices()
63+
dataset = data.sp500_returns()
64+
65+
self.assertEqual((num_train_points,), dataset['centered_returns'].shape)
66+
self.assertAllClose(0.0, np.mean(dataset['centered_returns']), atol=1e-5)
67+
68+
def testStochasticVolatilityModelLogSP500(self):
69+
num_train_points = 2516
70+
71+
dataset = data.sp500_log_returns()
6472

6573
self.assertEqual((num_train_points,), dataset['centered_returns'].shape)
6674
self.assertAllClose(0.0, np.mean(dataset['centered_returns']), atol=1e-5)

spinoffs/inference_gym/inference_gym/targets/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ py_library(
551551
":bayesian_model",
552552
":model",
553553
"//inference_gym/internal:data",
554+
"//inference_gym/targets/ground_truth:stochastic_volatility_log_sp500",
555+
"//inference_gym/targets/ground_truth:stochastic_volatility_log_sp500_small",
554556
"//inference_gym/targets/ground_truth:stochastic_volatility_sp500",
555557
"//inference_gym/targets/ground_truth:stochastic_volatility_sp500_small",
556558
],

spinoffs/inference_gym/inference_gym/targets/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
from inference_gym.targets.stochastic_volatility import StochasticVolatilitySP500
5252
from inference_gym.targets.stochastic_volatility import StochasticVolatilitySP500Small
5353
from inference_gym.targets.vectorized_stochastic_volatility import VectorizedStochasticVolatility
54+
from inference_gym.targets.vectorized_stochastic_volatility import VectorizedStochasticVolatilityLogSP500
55+
from inference_gym.targets.vectorized_stochastic_volatility import VectorizedStochasticVolatilityLogSP500Small
5456
from inference_gym.targets.vectorized_stochastic_volatility import VectorizedStochasticVolatilitySP500
5557
from inference_gym.targets.vectorized_stochastic_volatility import VectorizedStochasticVolatilitySP500Small
5658
from inference_gym.targets.vector_model import VectorModel
@@ -90,6 +92,8 @@
9092
'SyntheticLogGaussianCoxProcess',
9193
'SyntheticPlasmaSpectroscopy',
9294
'VectorizedStochasticVolatility',
95+
'VectorizedStochasticVolatilityLogSP500',
96+
'VectorizedStochasticVolatilityLogSP500Small',
9397
'VectorizedStochasticVolatilitySP500',
9498
'VectorizedStochasticVolatilitySP500Small',
9599
'VectorModel',

spinoffs/inference_gym/inference_gym/targets/ground_truth/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@ py_library(
126126
],
127127
)
128128

129+
# pytype_strict
130+
py_library(
131+
name = "stochastic_volatility_log_sp500",
132+
srcs = ["stochastic_volatility_log_sp500.py"],
133+
srcs_version = "PY3",
134+
deps = [
135+
# numpy dep,
136+
],
137+
)
138+
139+
# pytype_strict
140+
py_library(
141+
name = "stochastic_volatility_log_sp500_small",
142+
srcs = ["stochastic_volatility_log_sp500_small.py"],
143+
srcs_version = "PY3",
144+
deps = [
145+
# numpy dep,
146+
],
147+
)
148+
129149
# pytype_strict
130150
py_library(
131151
name = "stochastic_volatility_sp500",

0 commit comments

Comments
 (0)