예제 #1
0
 def factor_definition(self):
     s = time.time()
     needData = self.needData  # 计算所需数据
     needData.loc[needData[t.TRDSTAT].isin([5, 6]),
                  t.PCTCHG] = np.nan  # 将停牌对应 股票收益率 设为 NaN
     factor = -Calculator.Sumif(x=needData[t.PCTCHG],
                                condition=needData[t.PCTCHG] > 0,
                                num=5)  # 计算5日动量
     factor = factor.to_frame()
     factor.columns = [self.factorName]
     print('factor {0} done with {1} seconds'.format(
         self.factorName,
         time.time() - s))
     return factor
예제 #2
0
    def test_return_shape(self):
        """
        测试 返回值形状
        :return:
        """
        calculator = Calculator()
        simDataX = simDataGen(colnames=('colX', ))
        simDataY = simDataGen(colnames=('colY', ))
        dataShape = simDataX.shape
        # test functions
        self.assertTrue(
            np.all(dataShape == calculator.Delay(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Sumif(x=simDataX,
                                                 num=3,
                                                 by=ALIAS_FIELDS.STKCD,
                                                 condition=simDataX > 0,
                                                 retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Diff(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Max(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Min(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Mean(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Sum(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Var(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Std(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Wma(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Sma(
                x=simDataX, n=3, m=2, by=ALIAS_FIELDS.STKCD,
                retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Decaylinear(
                x=simDataX, d=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Rank(
                x=simDataX, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.TsToMax(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.TsToMin(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.FindRank(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Corr(x=simDataX,
                                                y=simDataY,
                                                num=3,
                                                by=ALIAS_FIELDS.STKCD,
                                                retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.RegAlpha(x=simDataX,
                                                    y=simDataY,
                                                    num=3,
                                                    by=ALIAS_FIELDS.STKCD,
                                                    retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.RegBeta(x=simDataX,
                                                   y=simDataY,
                                                   num=3,
                                                   by=ALIAS_FIELDS.STKCD,
                                                   retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.RegResi(x=simDataX,
                                                   y=simDataY,
                                                   num=3,
                                                   by=ALIAS_FIELDS.STKCD,
                                                   retDf=True).shape))
        print('DataFrame return shape test passed')

        self.assertTrue(
            np.all(dataShape[0] == calculator.Delay(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Sumif(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
                condition=simDataX > 0,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Diff(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Max(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Min(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Mean(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Sum(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Var(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Std(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Wma(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Sma(
                x=simDataX,
                n=3,
                m=2,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Decaylinear(
                x=simDataX,
                d=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Rank(
                x=simDataX,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.TsToMax(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.TsToMin(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.FindRank(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Corr(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.RegAlpha(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.RegBeta(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.RegResi(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        print('Series return shape test passed')