def test_fromfuncs(self): _vol_ave_int = 2 _sma_window = 4 _skipatstart = _sma_window - 1 _n_sess = 3 _smafunc = pn.expand(pn.tech.sma, 'Adj Close') _funcs = [ pn.decorate(partial(pn.tech.ratio_to_ave, _vol_ave_int), title='MyRelVol'), pn.decorate(partial(_smafunc, window=_sma_window), title='MySMA')] _features = pn.data.feat.fromfuncs(_funcs, _n_sess, self.equity_data, skipatstart=_skipatstart) self.assertEqual(len(_features.index), len(self.equity_data.index) - _n_sess - _skipatstart + 1) for i in range(len(_features.index)): self.assertAlmostEqual(_features.iloc[i, 0], 1.) self.assertEqual(_features.index[i], self.equity_data.index[i + _n_sess + _skipatstart - 1]) for j in range(_n_sess): # relative volumes all between 1.0 and 2.0 self.assertTrue(_features.iloc[i, j + 1] < 2.) self.assertTrue(_features.iloc[i, j + 1] > 1.) # SMAs are means of values like 4, 6, 8, 10 self.assertAlmostEqual(_features.iloc[i, j + _n_sess + 1], 1. + _sma_window + 2. * (i + j)) # properties of relative volumes if i >= 1: # strictly decreasing self.assertTrue(_features.iloc[i, 1] < _features.iloc[i - 1, 1]) # columns match with offset self.assertAlmostEqual(_features.iloc[i, 1], _features.iloc[i - 1, 2]) self.assertAlmostEqual(_features.iloc[i, 2], _features.iloc[i - 1, 3])
def test_expand(self): def _f(eqdata): return 2. * eqdata _expanded_ret = pn.expand(_f, 'Adj Close')(self.equity_data) self.assertEqual(len(_expanded_ret.values.flatten()), len(self.equity_data.index)) for i in range(len(_expanded_ret.index)): self.assertEqual(_expanded_ret.index[i], self.equity_data.index[i]) self.assertAlmostEqual(_expanded_ret.values[i], 4. + 4. * i)