Created
January 24, 2019 17:13
-
-
Save kurtbrose/9531082f2204883e1f55f8955550cd7a to your computer and use it in GitHub Desktop.
this is actually not useful -- just keeping it here in case it is handy later
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import attr | |
| import numpy as np | |
| class TimeSampler(object) | |
| ''' | |
| samples from a time indexed series based on time -- pick a random | |
| time within the range of the series; returns the time-delta | |
| and value-delta of the point to the right of the sample with the | |
| point before it | |
| this is needed so that small / incremental steps | |
| do not dominate the results when trying to sample a time | |
| sequence in order to generate a forecasted future | |
| ''' | |
| s = attr.ib() # time-indexed series to sample from | |
| ts_index, begin, end = attr.ib(), attr.ib(), attr.ib() | |
| @classmethod | |
| def from_s(cls, s): | |
| ts_index = s.index.astype('int64') | |
| return cls(s, ts_index, ts_index[0], ts_index[-1]) | |
| def sample(self): | |
| ''' | |
| returns (time width, value delta) for the step that | |
| was occurring at a random point over the duration of | |
| the series | |
| ''' | |
| # sample evenly across time so that "small" steps aren't | |
| # overrepresented in the simulation | |
| sample_time = random.randint(self.begin, self.end) | |
| sample_ix = np.searchsorted(self.ts_index, sample_time) | |
| cur_ts, prev_ts = self.ts_index[sample_ix], self.ts_index[sample_ix - 1] | |
| cur_val, prev_val = self.s[cur_ts], self.s[prev_ts] | |
| width = cur_ts - prev_ts | |
| delta = cur_val - prev_val | |
| return width, delta |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment