def setUp(self): self.mu1 = np.array([0.0]) self.cov1 = np.eye(1) self.mu2 = np.array([1.0]) self.cov2 = np.eye(1) * 2 self.gaussian1 = GaussianDummy(mean=self.mu1, cov=self.cov1, ndim_x=2, ndim_y=1) self.gaussian2 = GaussianDummy(mean=self.mu2, cov=self.cov2, ndim_x=2, ndim_y=1) self.mu3 = np.array([1.6, -7.0]) self.cov3 = np.array([[2.0, 0.5], [0.5, 4.5]]) self.mu4 = np.array([1.0, -5.0]) self.cov4 = np.eye(2) * 2 self.gaussian3 = GaussianDummy(mean=self.mu3, cov=self.cov3, ndim_x=2, ndim_y=2) self.gaussian4 = GaussianDummy(mean=self.mu4, cov=self.cov4, ndim_x=2, ndim_y=2)
def test_covariance1(self): # prepare estimator dummy mu = np.array([-1]) sigma = np.array([[0.1]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=1) est.fit(None, None) cov_est = est.covariance(x_cond=np.array([[0.5, 2]])) self.assertAlmostEqual(cov_est[0][0][0], sigma[0][0], places=2)
def test_covariance2(self): # prepare estimator dummy mu = np.array([0, 1]) sigma = np.array([[1, -0.2], [-0.2, 2]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=2, can_sample=False) est.fit(None, None) cov_est = est.covariance(x_cond=np.array([[0, 1]])) self.assertAlmostEqual(cov_est[0][0][0], sigma[0][0], places=2) self.assertAlmostEqual(cov_est[0][1][0], sigma[1][0], places=2)
def test_std2(self): # prepare estimator dummy mu = np.array([14]) sigma = np.array([[0.1]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=1, ndim_y=1, can_sample=False) est.fit(None, None) std_est = est.std_(x_cond=np.array([[0.0], [1.0]])) self.assertAlmostEqual(std_est[0][0]**2, sigma[0][0], places=2) self.assertAlmostEqual(std_est[1][0]**2, sigma[0][0], places=2)
def test_std1(self): # prepare estimator dummy mu = np.array([0, 1]) sigma = np.array([[1, -0.2], [-0.2, 2]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=2, can_sample=False) est.fit(None, None) std = est.std_(x_cond=np.array([[0, 1]])) self.assertAlmostEqual(std[0][0], np.sqrt(sigma[0][0]), places=2) self.assertAlmostEqual(std[0][1], np.sqrt(sigma[1][1]), places=2)
def test_mean_pdf(self): # prepare estimator dummy mu = np.array([0, 1]) sigma = np.identity(n=2) * 1 est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=2, can_sample=False) est.fit(None, None) mean_est = est.mean_(x_cond=np.array([[0, 1]])) self.assertAlmostEqual(mean_est[0][0], mu[0], places=2) self.assertAlmostEqual(mean_est[0][1], mu[1], places=2)
def test_mean_std(self): mu = np.array([0, 1]) sigma = np.array([[1, -0.2], [-0.2, 2]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=2, can_sample=False) est.fit(None, None) mean_est, std_est = est.mean_std(x_cond=np.array([[0, 1]])) self.assertAlmostEqual(mean_est[0][0], mu[0], places=2) self.assertAlmostEqual(mean_est[0][1], mu[1], places=2) self.assertAlmostEqual(std_est[0][0]**2, sigma[0][0], places=2) self.assertAlmostEqual(std_est[0][1]**2, sigma[1][1], places=2)
def test_value_at_risk_cdf(self): for mu, sigma in [(-2, 0.5), (0.4, 0.01), (22, 3)]: # prepare estimator dummy mu1 = np.array([mu]) sigma1 = np.identity(n=1) * sigma est = GaussianDummy(mean=mu1, cov=sigma1**2, ndim_x=1, ndim_y=1, has_cdf=True) est.fit(None, None) alpha = 0.05 VaR_est = est.value_at_risk(x_cond=np.array([[0], [1]]), alpha=alpha) VaR_true = norm.ppf(alpha, loc=mu, scale=sigma) self.assertAlmostEqual(VaR_est[0], VaR_true, places=2) self.assertAlmostEqual(VaR_est[1], VaR_true, places=2)
def test_conditional_value_at_risk_mc(self): for mu, sigma, alpha in [(1, 1, 0.05), (0.4, 0.1, 0.02), (0.1, 2, 0.01)]: # prepare estimator dummy mu1 = np.array([mu]) sigma1 = np.identity(n=1) * sigma est = GaussianDummy(mean=mu1, cov=sigma1**2, ndim_x=1, ndim_y=1, has_pdf=True) est.fit(None, None) CVaR_true = mu - sigma / alpha * norm.pdf(norm.ppf(alpha)) CVaR_est = est.conditional_value_at_risk(x_cond=np.array([[0], [1]]), alpha=alpha) print("CVaR True (%.2f, %.2f):" % (mu, sigma), CVaR_true) print("CVaR_est (%.2f, %.2f):" % (mu, sigma), CVaR_est) print("VaR (%.2f, %.2f):" % (mu, sigma), est.value_at_risk(x_cond=np.array([[0], [1]]), alpha=alpha)) self.assertAlmostEqual(CVaR_est[0], CVaR_true, places=2) self.assertAlmostEqual(CVaR_est[1], CVaR_true, places=2)
def test_conditional_value_at_risk_sample(self): # prepare estimator dummy for mu, sigma in [(-6, 0.25), (0.4, 0.1), (22, 3)]: mu1 = np.array([mu]) sigma1 = np.identity(n=1) * sigma est = GaussianDummy(mean=mu1, cov=sigma1**2, ndim_x=1, ndim_y=1, has_pdf=False) est.fit(None, None) alpha = 0.02 CVaR_true = mu - sigma / alpha * norm.pdf(norm.ppf(alpha)) CVaR_est = est.conditional_value_at_risk(x_cond=np.array([[0], [1]]), alpha=alpha, n_samples=2 * 10**6) self.assertAlmostEqual(CVaR_est[0], CVaR_true, places=2) self.assertAlmostEqual(CVaR_est[1], CVaR_true, places=2)
def test_kurtosis1(self): mu = np.array([-0.001]) sigma = np.array([[0.02]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=1, can_sample=False) est.fit(None, None) kurt = est._kurtosis_mc(x_cond=np.array([[0, 1]]), n_samples=10**6) print("Kurtosis sample estimate:", kurt) self.assertAlmostEqual(kurt[0], 0, places=1) kurt = est._kurtosis_pdf(x_cond=np.array([[0, 1]]), n_samples=10**6) print("Kurtosis pdf estimate:", kurt) self.assertAlmostEqual(kurt[0], 0, places=1)
def test_skewness1(self): mu = np.array([-0.001]) sigma = np.array([[0.02]]) est = GaussianDummy(mean=mu, cov=sigma, ndim_x=2, ndim_y=1, can_sample=False) est.fit(None, None) skew = est._skewness_mc(x_cond=np.array([[0, 1]])) print("Skewness sample estimate:", skew) self.assertAlmostEqual(skew[0], 0, places=1) skew = est._skewness_pdf(x_cond=np.array([[0, 1]])) print("Skewness pdf estimate:", skew) self.assertAlmostEqual(skew[0], 0, places=1)