|
| 1 | +## |
| 2 | +# \brief Mixture of two copula |
| 3 | +from __future__ import print_function, absolute_import, division |
| 4 | +import numpy as np |
| 5 | +from scipy import stats |
| 6 | +# STARVINE IMPORTS |
| 7 | +from starvine.bvcopula.copula.copula_base import CopulaBase |
| 8 | +from starvine.bvcopula.copula.mvtdstpack import mvtdstpack as mvt |
| 9 | + |
| 10 | + |
| 11 | +class MixtureCopula(CopulaBase): |
| 12 | + """! |
| 13 | + @brief Gaussian copula model |
| 14 | + single parameter |
| 15 | + \f$\theta[0] \in (-1, 1)\f$ |
| 16 | + """ |
| 17 | + def __init__(self, copula_a, wt_a, copula_b, wt_b): |
| 18 | + wt_a = wt_a / (wt_a + wt_b) |
| 19 | + wt_b = wt_b / (wt_a + wt_b) |
| 20 | + self._name = copula_a.name + '-' + copula_b.name |
| 21 | + self._thetaBounds = tuple(list(copula_a.thetaBounds) + list(copula_b.thetaBounds) + [(0.,1.), (0.,1.)]) |
| 22 | + self._theta0 = tuple(list(copula_a.theta0) + list(copula_b.theta0) + [wt_a, wt_b]) |
| 23 | + self.fittedParams = list(copula_a.theta0) + list(copula_b.theta0) + [wt_a, wt_b] |
| 24 | + self._copula_a = copula_a |
| 25 | + self._copula_b = copula_b |
| 26 | + self._n_params_a = len(copula_a.thetaBounds) |
| 27 | + self._n_params_b = len(copula_b.thetaBounds) |
| 28 | + |
| 29 | + @property |
| 30 | + def wgts(self): |
| 31 | + return self._fittedParams[-2:] |
| 32 | + |
| 33 | + @property |
| 34 | + def thetaBounds(self): |
| 35 | + return self._thetaBounds |
| 36 | + |
| 37 | + @property |
| 38 | + def theta0(self): |
| 39 | + return self._theta0 |
| 40 | + |
| 41 | + @property |
| 42 | + def name(self): |
| 43 | + return self._name |
| 44 | + |
| 45 | + @property |
| 46 | + def rotation(self): |
| 47 | + # mixture copula has no defined rotation |
| 48 | + return 0 |
| 49 | + |
| 50 | + @CopulaBase._rotPDF |
| 51 | + def _pdf(self, u, v, rotation=0, *theta): |
| 52 | + theta_a = theta[0:self._n_params_a] |
| 53 | + theta_b = theta[self._n_params_a:-2] |
| 54 | + wgt_a = theta[-2] |
| 55 | + wgt_b = theta[-1] |
| 56 | + return wgt_a * self._copula_a.pdf(u, v, self._copula_a.rotation, *theta_a) + \ |
| 57 | + wgt_b * self._copula_b.pdf(u, v, self._copula_b.rotation, *theta_b) |
| 58 | + |
| 59 | + @CopulaBase._rotCDF |
| 60 | + def _cdf(self, u, v, rotation=0, *theta): |
| 61 | + theta_a = theta[0:self._n_params_a] |
| 62 | + theta_b = theta[self._n_params_a:-2] |
| 63 | + wgt_a = theta[-2] |
| 64 | + wgt_b = theta[-1] |
| 65 | + return wgt_a * self._copula_a.cdf(u, v, self._copula_a.rotation, *theta_a) + \ |
| 66 | + wgt_b * self._copula_b.cdf(u, v, self._copula_b.rotation, *theta_b) |
| 67 | + |
| 68 | + @CopulaBase._rotH |
| 69 | + def _h(self, v, u, rotation=0, *theta): |
| 70 | + theta_a = theta[0:self._n_params_a] |
| 71 | + theta_b = theta[self._n_params_a:-2] |
| 72 | + wgt_a = theta[-2] |
| 73 | + wgt_b = theta[-1] |
| 74 | + return wgt_a * self._copula_a.h(u, v, self._copula_a.rotation, *theta_a) + \ |
| 75 | + wgt_b * self._copula_b.h(u, v, self._copula_b.rotation, *theta_b) |
| 76 | + |
| 77 | + @CopulaBase._rotHinv |
| 78 | + def _hinv(self, v, u, rotation=0, *theta): |
| 79 | + theta_a = theta[0:self._n_params_a] |
| 80 | + theta_b = theta[self._n_params_a:-2] |
| 81 | + wgt_a = theta[-2] |
| 82 | + wgt_b = theta[-1] |
| 83 | + return wgt_a * self._copula_a.hinv(u, v, self._copula_a.rotation, *theta_a) + \ |
| 84 | + wgt_b * self._copula_b.hinv(u, v, self._copula_b.rotation, *theta_b) |
| 85 | + |
| 86 | + @CopulaBase._rotGen |
| 87 | + def _gen(self, t, *theta): |
| 88 | + """! |
| 89 | + @brief Copula generating function |
| 90 | + """ |
| 91 | + raise NotImplementedError |
0 commit comments