def test_pdf(): ''' Tests the probability density function. ''' samples = np.array([np.linspace(0, 1, 5), np.linspace(0.2, 0.8, 5)]).T # Independence copula independence_copula = IndependenceCopula() # Comparison values r_pdf = np.array([0.0, 1.0, 1.0, 1.0, 0.0]) p_pdf = independence_copula.pdf(samples) assert_allclose(p_pdf, r_pdf) # Gaussian copula family gaussian_copula = GaussianCopula(0.5) # Comparison values r_pdf = np.array([0.0, 1.2417679440, 1.1547005384, 1.2417679440, 0.0]) p_pdf = gaussian_copula.pdf(samples) assert_allclose(p_pdf, r_pdf) # Clayton copula family clayton_copula = ClaytonCopula(5) # Comparison values r_pdf = np.array([0.0, 2.1943031503, 2.7037217178, 1.9477508961, 0.0]) p_pdf = clayton_copula.pdf(samples) assert_allclose(p_pdf, r_pdf) # Frank copula family frank_copula = FrankCopula(5) # Comparison values r_pdf = np.array([0.0, 1.5167615765, 1.4735637246, 1.5167615765, 0.0]) p_pdf = frank_copula.pdf(samples) assert_allclose(p_pdf, r_pdf)
def test_cdf(): ''' Tests the cumulative distribution function. ''' samples = np.array([np.linspace(0, 1, 5), np.linspace(0.2, 0.8, 5)]).T # Independence copula independence_copula = IndependenceCopula() # Comparison values r_cdf = np.array([0.0, 0.0875, 0.25, 0.4875, 0.8]) p_cdf = independence_copula.cdf(samples) assert_allclose(p_cdf, r_cdf) # Gaussian copula family gaussian_copula = GaussianCopula(0.5) # Comparison values r_cdf = np.array([0.0, 0.1520333540, 0.3333333333, 0.5520333540, 0.8]) p_cdf = gaussian_copula.cdf(samples) assert_allclose(p_cdf, r_cdf) # Clayton copula family clayton_copula = ClaytonCopula(5) # Comparison values r_cdf = np.array([0.0, 0.2416570262, 0.4366484171, 0.6100768349, 0.8]) p_cdf = clayton_copula.cdf(samples) assert_allclose(p_cdf, r_cdf) # Frank copula family frank_copula = FrankCopula(5) # Comparison values r_cdf = np.array([0.0, 0.1800378858, 0.3771485107, 0.5800378858, 0.8]) p_cdf = frank_copula.cdf(samples) assert_allclose(p_cdf, r_cdf)
def test_ppcf(): ''' Tests the conditional cumulative distribution function. ''' samples = np.array([np.linspace(0, 1, 5), np.linspace(0.2, 0.8, 5)]).T # Independence copula independence_copula = IndependenceCopula() # Comparison values r_ppcf = np.array([0.0, 0.25, 0.5, 0.75, 1.0]) p_ppcf = independence_copula.ppcf(samples) assert_allclose(p_ppcf, r_ppcf) # Test other axis r_ppcf = np.array([0.2, 0.35, 0.5, 0.65, 0.8]) p_ppcf = independence_copula.ppcf(samples, axis=0) assert_allclose(p_ppcf, r_ppcf) # Gaussian copula family gaussian_copula = GaussianCopula(0.5) # Comparison values r_ppcf = np.array([0.0, 0.218642669, 0.5, 0.781357331, 1.0]) p_ppcf = gaussian_copula.ppcf(samples) assert_allclose(p_ppcf, r_ppcf) # Test other axis r_ppcf = np.array([0.0, 0.2511286797, 0.5, 0.7488713203, 1.0]) p_ppcf = gaussian_copula.ppcf(samples, axis=0) assert_allclose(p_ppcf, r_ppcf) # Clayton copula family clayton_copula = ClaytonCopula(5) # Comparison values r_ppcf = np.array([0.0, 0.2994846602, 0.5211309028, 0.7859307932, 1.0]) p_ppcf = clayton_copula.ppcf(samples) assert_allclose(p_ppcf, r_ppcf) # Test other axis r_ppcf = np.array( [0.0, 0.2337467913, 0.5211309028, 0.8127416749, 0.9634924840]) p_ppcf = clayton_copula.ppcf(samples, axis=0) assert_allclose(p_ppcf, r_ppcf) # Frank copula family frank_copula = FrankCopula(5) # Comparison values r_ppcf = np.array([0.0, 0.21162507, 0.5, 0.78837493, 1.0]) p_ppcf = frank_copula.ppcf(samples) assert_allclose(p_ppcf, r_ppcf) # Test other axis r_ppcf = np.array([0.0442921, 0.20900068, 0.5, 0.79099932, 0.9557079]) p_ppcf = frank_copula.ppcf(samples, axis=0) assert_allclose(p_ppcf, r_ppcf)
def test_ccdf(): ''' Tests the conditional cumulative distribution function. ''' samples = np.array([np.linspace(0, 1, 5), np.linspace(0.2, 0.8, 5)]).T # Independence copula independence_copula = IndependenceCopula() # Comparison values r_ccdf = np.array([0.0, 0.25, 0.5, 0.75, 1.0]) p_ccdf = independence_copula.ccdf(samples) assert_allclose(p_ccdf, r_ccdf) # Test other axis r_ccdf = np.array([0.2, 0.35, 0.5, 0.65, 0.8]) p_ccdf = independence_copula.ccdf(samples, axis=0) assert_allclose(p_ccdf, r_ccdf) # Gaussian copula family gaussian_copula = GaussianCopula(0.5) # Comparison values r_ccdf = np.array([0.0, 0.2889793807, 0.5, 0.7110206193, 1.0]) p_ccdf = gaussian_copula.ccdf(samples) assert_allclose(p_ccdf, r_ccdf) # Test other axis r_ccdf = np.array([1.0, 0.4778649221, 0.5, 0.5221350779, 0.0]) p_ccdf = gaussian_copula.ccdf(samples, axis=0) assert_allclose(p_ccdf, r_ccdf) # Clayton copula family clayton_copula = ClaytonCopula(5) # Comparison values r_ccdf = np.array([0.0, 0.1083398661, 0.4435793443, 0.6836393756, 1.0]) p_ccdf = clayton_copula.ccdf(samples) assert_allclose(p_ccdf, r_ccdf) # Test other axis r_ccdf = np.array([0.0, 0.815748922, 0.4435793443, 0.2896940854, 0.262144]) p_ccdf = clayton_copula.ccdf(samples, axis=0) assert_allclose(p_ccdf, r_ccdf) # Frank copula family frank_copula = FrankCopula(5) # Comparison values r_ccdf = np.array([0.0, 0.3070854, 0.5, 0.6929146, 1.0]) p_ccdf = frank_copula.ccdf(samples) assert_allclose(p_ccdf, r_ccdf) # Test other axis r_ccdf = np.array([0.63640865, 0.58629237, 0.5, 0.41370763, 0.36359135]) p_ccdf = frank_copula.ccdf(samples, axis=0) assert_allclose(p_ccdf, r_ccdf)
def test_logcdf(): ''' Tests the log of the cumulative distribution function. ''' samples = np.array([np.linspace(0, 1, 5), np.linspace(0.2, 0.8, 5)]).T # Independence copula independence_copula = IndependenceCopula() # Comparison values r_logcdf = np.array( [-np.inf, -2.4361164856, -1.3862943611, -0.7184649885, -0.2231435513]) p_logcdf = independence_copula.logcdf(samples) assert_allclose(p_logcdf, r_logcdf) # Gaussian copula family gaussian_copula = GaussianCopula(0.5) # Comparison values r_logcdf = np.array( [-np.inf, -1.8836553477, -1.0986122887, -0.5941468105, -0.2231435513]) p_logcdf = gaussian_copula.logcdf(samples) assert_allclose(p_logcdf, r_logcdf) # Clayton copula family clayton_copula = ClaytonCopula(5) # Comparison values r_logcdf = np.array( [-np.inf, -1.4202358053, -0.8286269453, -0.4941703709, -0.2231435513]) p_logcdf = clayton_copula.logcdf(samples) assert_allclose(p_logcdf, r_logcdf) # Frank copula family frank_copula = FrankCopula(5) # Comparison values r_logcdf = np.array( [-np.inf, -1.7145879734, -0.9751162414, -0.5446618572, -0.2231435513]) p_logcdf = frank_copula.logcdf(samples) assert_allclose(p_logcdf, r_logcdf)
def test_logpdf(): ''' Tests the log of the probability density function. ''' samples = np.array([np.linspace(0, 1, 5), np.linspace(0.2, 0.8, 5)]).T # Independence copula independence_copula = IndependenceCopula() # Comparison values r_logpdf = np.array([-np.inf, 0.0, 0.0, 0.0, -np.inf]) p_logpdf = independence_copula.logpdf(samples) assert_allclose(p_logpdf, r_logpdf) # Gaussian copula family gaussian_copula = GaussianCopula(0.5) # Comparison values r_logpdf = np.array( [-np.inf, 0.2165361255, 0.1438410362, 0.2165361255, -np.inf]) p_logpdf = gaussian_copula.logpdf(samples) assert_allclose(p_logpdf, r_logpdf) # Clayton copula family clayton_copula = ClaytonCopula(5) # Comparison values r_logpdf = np.array( [-np.inf, 0.7858645247, 0.9946292379, 0.6666753203, -np.inf]) p_logpdf = clayton_copula.logpdf(samples) assert_allclose(p_logpdf, r_logpdf) # Frank copula family frank_copula = FrankCopula(5) # Comparison values r_logpdf = np.array( [-np.inf, 0.4165775202, 0.3876837693, 0.4165775202, -np.inf]) p_logpdf = frank_copula.logpdf(samples) assert_allclose(p_logpdf, r_logpdf)
def fit(self, samples, is_continuous, is_adjusted=None, trunc_level=None): ''' Fits the vine tree to the given samples. This method is supposed to be called on the output layer and will recurse to its input layers. Parameters ---------- samples : array_like n-by-d matrix of samples where n is the number of samples and d is the number of marginals. is_continuous : array_like List of boolean values of length d, where d is the number of marginals and element i is `True` if marginal i is continuous. manual_adjust : array_like list of distributions of length d, where d is the number of marginals and element i is a scipy distribution if it was adjusted beforehand or None if the program has to ajust it. trunc_level : integer, optional Layer level to truncate the vine at. Copulas in layers beyond are just independence copulas. If the level is `None`, then the vine is not truncated. (Default: `None`) Returns ------- output_urvs : array_like The output uniform random variates of the layer. Can be ignored if this is the output layer. ''' if self.is_marginal_layer(): output_urvs = np.zeros(samples.shape) if is_adjusted: for i in range(samples.shape[1]): self.marginals[i] = Marginal_.fit( samples[:, i], is_continuous[i], is_adjusted[i]) output_urvs[:, i] = self.marginals[i].cdf(samples[:, i]) else: for i in range(samples.shape[1]): self.marginals[i] = Marginal_.fit( samples[:, i], is_continuous[i]) output_urvs[:, i] = self.marginals[i].cdf(samples[:, i]) else: input_urvs = self.input_layer.fit(samples, is_continuous, is_adjusted) truncate = trunc_level and samples.shape[1] \ - len(self.input_indices) > trunc_level - 1 output_urvs = np.zeros( (samples.shape[0], len(self.input_indices))) for i, i_ind in enumerate(self.input_indices): if truncate: self.copulas[i] = IndependenceCopula() else: self.copulas[i] = Copula.fit(input_urvs[:, i_ind]) output_urvs[:, i] \ = self.copulas[i].ccdf(input_urvs[:, i_ind]) return output_urvs