|
9 | 9 | import pandas as pd
|
10 | 10 | from sklearn.preprocessing import LabelEncoder
|
11 | 11 | import pymc3 as pm
|
| 12 | +import xarray as xr |
12 | 13 | from .serialize import to_xarray
|
13 | 14 | from ._version import get_versions
|
14 | 15 | from .base import BayesAlphaResult
|
@@ -56,27 +57,27 @@ def __init__(self, data):
|
56 | 57 | self.model = self._build_model(data)
|
57 | 58 |
|
58 | 59 | 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 |
62 | 63 | }
|
63 | 64 |
|
64 | 65 | self.dims = {
|
65 | 66 | 'mu_global': (),
|
66 |
| - 'mu_author': ('author', ), |
67 |
| - 'mu_author_raw': ('author', ), |
| 67 | + 'mu_author': ('meta_user_id', ), |
| 68 | + 'mu_author_raw': ('meta_user_id', ), |
68 | 69 | 'mu_author_sd': (),
|
69 |
| - 'sigma_author': ('author', ), |
| 70 | + 'sigma_author': ('meta_user_id', ), |
70 | 71 | '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', ), |
73 | 74 | 'mu_algo_sd': (),
|
74 |
| - 'sigma_algo': ('algo', ), |
| 75 | + 'sigma_algo': ('meta_algorithm_id', ), |
75 | 76 | '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', ) |
80 | 81 | }
|
81 | 82 |
|
82 | 83 | def _build_model(self, data):
|
@@ -155,7 +156,12 @@ class AuthorModelResult(BayesAlphaResult):
|
155 | 156 | def rebuild_model(self, data=None):
|
156 | 157 | """ Return an AuthorModelBuilder that recreates the original model. """
|
157 | 158 | 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()) |
159 | 165 |
|
160 | 166 | return AuthorModelBuilder(data)
|
161 | 167 |
|
@@ -233,6 +239,12 @@ def fit_authors(data,
|
233 | 239 | trace.attrs['model-version'] = get_versions()['version']
|
234 | 240 | trace.attrs['model-type'] = AUTHOR_MODEL_TYPE
|
235 | 241 |
|
| 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 | + |
236 | 248 | return AuthorModelResult(trace)
|
237 | 249 |
|
238 | 250 |
|
|
0 commit comments