예제 #1
0
    def testFrankFrozen(self):
        np.random.seed(123)
        # Load matlab data set
        stocks = np.loadtxt(dataDir + 'stocks.csv', delimiter=',')
        x = stocks[:, 0]
        y = stocks[:, 1]
        stockModel = PairCopula(x, y, family={
            'frank': 0,
        })

        # Try to fit all copula
        stockModel.copulaTournament(verbosity=0)

        # Eval the frozen model
        frzU, frzV = stockModel.copulaModel.sample(40000)

        # Eval a model with specified params
        setU, setV = stockModel.copulaModel.sample(40000,
                                                   *stockModel.copulaParams[1])

        # Ensure both frozen model and specified param model produce same result
        frzModel = PairCopula(frzU, frzV)
        setModel = PairCopula(setU, setV)
        frzKtau, fp = frzModel.empKTau()
        setKtau, sp = setModel.empKTau()
        self.assertAlmostEqual(frzKtau, setKtau, delta=0.02)
        self.assertAlmostEqual(fp, sp, delta=0.02)
예제 #2
0
    def testBivariateBase(self):
        print(
            "--------------------- MULTI COPULA FIT TEST --------------------------"
        )
        # Load matlab data set
        stocks = np.loadtxt(dataDir + 'stocks.csv', delimiter=',')
        x = stocks[:, 0]
        y = stocks[:, 1]
        stockModel = PairCopula(x, y)

        empTau = stockModel.empKTau()  # kendall's tau corr coeff
        empSRho = stockModel.empSRho()  # spearmans corr coeff
        empPRho = stockModel.empPRho()  # pearson's corr coeff
        print("Emprical kTau, spearmanr, pearsonr: " + str(empTau[0]) + ", " +
              str(empSRho[0]) + ", " + str(empPRho[0]))

        # Try to fit all copula
        stockModel.copulaTournament()

        # Ensure that the gaussian copula was chosen as the best fit
        self.assertTrue(stockModel.copulaModel.name == "gauss")
        self.assertTrue(stockModel.copulaParams[0] == "gauss")

        # Check gaussian copula parameters for correctness
        self.assertAlmostEqual(stockModel.copulaParams[1], 0.73874003, 4)

        # Test kendalls criterion on original data
        stockModel.copulaTournament(criterion='Kc', log=True)

        # When using kendalls criterion the predicted copula should be gumbel
        self.assertTrue(stockModel.copulaModel.name == "gumbel")
        self.assertTrue(stockModel.copulaParams[0] == "gumbel")
        self.assertTrue(stockModel.copulaModel.rotation == 0)

        # Rotate the data and test kendalls criteron again
        stockModelNegative = PairCopula(-1 * x, y)
        empTau = stockModelNegative.empKTau()[0]
        self.assertTrue(empTau < 0)

        # Test kendalls criterion on original data
        stockModelNegative.copulaTournament(criterion='Kc')

        # When using kendalls criterion the predicted copula should be gumbel
        self.assertTrue(stockModelNegative.copulaModel.name == "gumbel")
        self.assertTrue(stockModelNegative.copulaParams[0] == "gumbel")
        self.assertTrue(stockModelNegative.copulaModel.rotation == 1)

        # Rotate the data and test kendalls criteron again
        stockModelNegative = PairCopula(x, -1 * y)
        empTau = stockModelNegative.empKTau()[0]
        self.assertTrue(empTau < 0)

        # Test kendalls criterion on original data
        stockModelNegative.copulaTournament(criterion='Kc')

        # When using kendalls criterion the predicted copula should be gumbel
        self.assertTrue(stockModelNegative.copulaModel.name == "gumbel")
        self.assertTrue(stockModelNegative.copulaParams[0] == "gumbel")
        self.assertTrue(stockModelNegative.copulaModel.rotation == 3)
예제 #3
0
    def testGaussFrozen(self):
        # Load matlab data set
        stocks = np.loadtxt(dataDir + 'stocks.csv', delimiter=',')
        x = stocks[:, 0]
        y = stocks[:, 1]
        stockModel = PairCopula(x, y)

        # Try to fit all copula
        stockModel.copulaTournament(verbosity=0)

        # Ensure that the gaussian copula was chosen as the best fit
        self.assertTrue(stockModel.copulaModel.name == "gauss")
        self.assertTrue(stockModel.copulaParams[0] == "gauss")

        # Check gaussian copula parameters for correctness
        self.assertAlmostEqual(stockModel.copulaParams[1][0], 0.73874003, 4)

        # Eval the frozen model
        frzU, frzV = stockModel.copulaModel.sample(40000)

        # Eval a model with specified params
        setU, setV = stockModel.copulaModel.sample(40000, (0.73874003, ))

        # Ensure both frozen model and specified param model produce same result
        frzModel = PairCopula(frzU, frzV)
        setModel = PairCopula(setU, setV)
        frzKtau, fp = frzModel.empKTau()
        setKtau, sp = setModel.empKTau()
        self.assertAlmostEqual(frzKtau, setKtau, delta=0.02)
        self.assertAlmostEqual(fp, sp, delta=0.02)

        # Eval a model with different specified params
        setU2, setV2 = stockModel.copulaModel.sample(20000, (0.3, ))
        setModel2 = PairCopula(setU2, setV2)
        setKtau2, sp2 = setModel2.empKTau()
        self.assertTrue(setKtau2 != setKtau)
        self.assertTrue(abs(setKtau2 - setKtau) > 0.2)