Skip to content

Commit 48bf273

Browse files
eigenfooaseyboldt
authored andcommitted
MAINT: Save data and rebuild model (#49)
1 parent ee36a20 commit 48bf273

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

bayesalpha/author_model.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pandas as pd
1010
from sklearn.preprocessing import LabelEncoder
1111
import pymc3 as pm
12+
import xarray as xr
1213
from .serialize import to_xarray
1314
from ._version import get_versions
1415
from .base import BayesAlphaResult
@@ -56,27 +57,27 @@ def __init__(self, data):
5657
self.model = self._build_model(data)
5758

5859
self.coords = {
59-
'author': data.meta_user_id.drop_duplicates().values,
60-
'algo': data.meta_algorithm_id.drop_duplicates().values,
61-
'backtest': data.meta_code_id.values
60+
'meta_user_id': data.meta_user_id.drop_duplicates().values,
61+
'meta_algorithm_id': data.meta_algorithm_id.drop_duplicates().values,
62+
'meta_code_id': data.meta_code_id.values
6263
}
6364

6465
self.dims = {
6566
'mu_global': (),
66-
'mu_author': ('author', ),
67-
'mu_author_raw': ('author', ),
67+
'mu_author': ('meta_user_id', ),
68+
'mu_author_raw': ('meta_user_id', ),
6869
'mu_author_sd': (),
69-
'sigma_author': ('author', ),
70+
'sigma_author': ('meta_user_id', ),
7071
'sigma_author_sd': (),
71-
'mu_algo': ('algo', ),
72-
'mu_algo_raw': ('algo', ),
72+
'mu_algo': ('meta_algorithm_id', ),
73+
'mu_algo_raw': ('meta_algorithm_id', ),
7374
'mu_algo_sd': (),
74-
'sigma_algo': ('algo', ),
75+
'sigma_algo': ('meta_algorithm_id', ),
7576
'sigma_algo_sd': (),
76-
'mu_backtest': ('backtest', ),
77-
'sigma_backtest': ('backtest', ),
78-
'alpha_author': ('author', ),
79-
'alpha_algo': ('algo', )
77+
'mu_backtest': ('meta_code_id', ),
78+
'sigma_backtest': ('meta_code_id', ),
79+
'alpha_author': ('meta_user_id', ),
80+
'alpha_algo': ('meta_algorithm_id', )
8081
}
8182

8283
def _build_model(self, data):
@@ -155,7 +156,12 @@ class AuthorModelResult(BayesAlphaResult):
155156
def rebuild_model(self, data=None):
156157
""" Return an AuthorModelBuilder that recreates the original model. """
157158
if data is None:
158-
data = self.trace._data.to_pandas().copy()
159+
data = (self.trace
160+
._data
161+
.to_pandas()
162+
.rename('perf_sharpe_ratio_is')
163+
.reset_index()
164+
.copy())
159165

160166
return AuthorModelBuilder(data)
161167

@@ -233,6 +239,12 @@ def fit_authors(data,
233239
trace.attrs['model-version'] = get_versions()['version']
234240
trace.attrs['model-type'] = AUTHOR_MODEL_TYPE
235241

242+
if save_data:
243+
d = data.set_index(['meta_user_id',
244+
'meta_algorithm_id',
245+
'meta_code_id']).squeeze()
246+
trace['_data'] = xr.DataArray(d)
247+
236248
return AuthorModelResult(trace)
237249

238250

0 commit comments

Comments
 (0)