def testCSZscoreSecurityValueHolderWithGroups(self): benchmark = SecurityLatestValueHolder(x='close') groups = SecurityLatestValueHolder(x='ind') meanAdjustedHolder = CSZScoreSecurityValueHolder(benchmark, groups) for i in range(len(self.datas['aapl']['close'])): data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i], Factors.OPEN: self.datas['aapl'][Factors.OPEN][i], 'ind': 1.}, 'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i], Factors.OPEN: self.datas['ibm'][Factors.OPEN][i], 'ind': 1.}, 'goog': {Factors.CLOSE: self.datas['goog'][Factors.CLOSE][i], Factors.OPEN: self.datas['goog'][Factors.OPEN][i], 'ind': 2.}, 'baba': {Factors.CLOSE: self.datas['baba'][Factors.CLOSE][i], Factors.OPEN: self.datas['baba'][Factors.OPEN][i], 'ind': 2.}} benchmark.push(data) meanAdjustedHolder.push(data) benchmarkValues = benchmark.value groups = {'aapl': 1., 'ibm': 1., 'goog': 2., 'baba': 2.} expected_rank = pd.Series(benchmarkValues.to_dict()).groupby(groups) \ .transform(lambda x: (x - x.mean()) / x.std(ddof=0)) np.testing.assert_array_almost_equal(expected_rank, meanAdjustedHolder.value.values)
def testCSZscoreSecurityValueHolder(self): keys = list(range(1, 11)) values = list(range(10, 0, -1)) data = {} for i, k in enumerate(keys): data[k] = {} data[k]['close'] = values[i] quantile_value = CSZScoreSecurityValueHolder('close') quantile_value.push(data) calculated = quantile_value.value data = np.linspace(10., 1., 10) expected = (data - data.mean()) / data.std() np.testing.assert_array_almost_equal(expected, calculated.values)