Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_set_parameters(self):
#Test initialization and setting of parameters with fixed values
distribution = MultiStudentT([1.2,1.3],[[1,0],[0,1]],2,seed=1)
distribution.set_parameters([[1.5,1.6],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[1.5, 1.6], [[1.1, 0], [0, 1.1]], 3])
#Test initialization and setting of parameters with 1 multidimensional distribution
helper_distribution = MultiStudentT([1,1],[[0.1,0],[0,0.1]],2)
distribution = MultiStudentT(helper_distribution, [[1,0],[0,1]],3,seed=1)
distribution.set_parameters([[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
self.assertTrue(distribution.get_parameters()==[[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
#Test initialization and setting of parameters with 2 distributions
helper_distribution = Normal(1,0.5,seed=1)
helper_distribution_2 = Normal(2,0.1,seed=1)
distribution = MultiStudentT([helper_distribution,helper_distribution_2],[[1,0],[0,1]],2)
distribution.set_parameters([[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
#Test initialization and setting of parameters with distribution for degrees of freedom
distribution = MultiStudentT([1,1],[[1,0],[0,1]],helper_distribution)
distribution.set_parameters([[1.1,1.2],[[0.1,0],[0,0.1]],[1,[1.4,0.7]]])
self.assertTrue(distribution.get_parameters()==[[1.1,1.2],[[0.1,0],[0,0.1]],[1,[1.4,0.7]]])
def test_set_parameters(self):
#Test initialization and setting of parameters with fixed values
distribution = MultiStudentT([1.2,1.3],[[1,0],[0,1]],2,seed=1)
distribution.set_parameters([[1.5,1.6],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[1.5, 1.6], [[1.1, 0], [0, 1.1]], 3])
#Test initialization and setting of parameters with 1 multidimensional distribution
helper_distribution = MultiStudentT([1,1],[[0.1,0],[0,0.1]],2)
distribution = MultiStudentT(helper_distribution, [[1,0],[0,1]],3,seed=1)
distribution.set_parameters([[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
self.assertTrue(distribution.get_parameters()==[[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
#Test initialization and setting of parameters with 2 distributions
helper_distribution = Normal(1,0.5,seed=1)
helper_distribution_2 = Normal(2,0.1,seed=1)
distribution = MultiStudentT([helper_distribution,helper_distribution_2],[[1,0],[0,1]],2)
distribution.set_parameters([[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
#Test initialization and setting of parameters with distribution for degrees of freedom
distribution = MultiStudentT([1,1],[[1,0],[0,1]],helper_distribution)
distribution.set_parameters([[1.1,1.2],[[0.1,0],[0,0.1]],[1,[1.4,0.7]]])
self.assertTrue(distribution.get_parameters()==[[1.1,1.2],[[0.1,0],[0,0.1]],[1,[1.4,0.7]]])
def test_set_parameters(self):
#Test initialization and setting of parameters with fixed values
distribution = MultiStudentT([1.2,1.3],[[1,0],[0,1]],2,seed=1)
distribution.set_parameters([[1.5,1.6],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[1.5, 1.6], [[1.1, 0], [0, 1.1]], 3])
#Test initialization and setting of parameters with 1 multidimensional distribution
helper_distribution = MultiStudentT([1,1],[[0.1,0],[0,0.1]],2)
distribution = MultiStudentT(helper_distribution, [[1,0],[0,1]],3,seed=1)
distribution.set_parameters([[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
self.assertTrue(distribution.get_parameters()==[[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
#Test initialization and setting of parameters with 2 distributions
helper_distribution = Normal(1,0.5,seed=1)
helper_distribution_2 = Normal(2,0.1,seed=1)
distribution = MultiStudentT([helper_distribution,helper_distribution_2],[[1,0],[0,1]],2)
distribution.set_parameters([[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
def test_set_parameters(self):
#Test initialization and setting of parameters with fixed values
distribution = MultiStudentT([1.2,1.3],[[1,0],[0,1]],2,seed=1)
distribution.set_parameters([[1.5,1.6],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[1.5, 1.6], [[1.1, 0], [0, 1.1]], 3])
#Test initialization and setting of parameters with 1 multidimensional distribution
helper_distribution = MultiStudentT([1,1],[[0.1,0],[0,0.1]],2)
distribution = MultiStudentT(helper_distribution, [[1,0],[0,1]],3,seed=1)
distribution.set_parameters([[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
self.assertTrue(distribution.get_parameters()==[[[[1.2,1.3],[[1.2,1.4],[[1,0],[0,1]],2]]],[[1.1,0],[0,1.2]],2])
#Test initialization and setting of parameters with 2 distributions
helper_distribution = Normal(1,0.5,seed=1)
helper_distribution_2 = Normal(2,0.1,seed=1)
distribution = MultiStudentT([helper_distribution,helper_distribution_2],[[1,0],[0,1]],2)
distribution.set_parameters([[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
self.assertTrue(distribution.get_parameters()==[[[1.1,[1.2,0.3]],[2.2,[2.1,0.2]]],[[1.1,0],[0,1.1]],3])
#Test initialization and setting of parameters with distribution for degrees of freedom
distribution = MultiStudentT([1,1],[[1,0],[0,1]],helper_distribution)
distribution.set_parameters([[1.1,1.2],[[0.1,0],[0,0.1]],[1,[1.4,0.7]]])
self.assertTrue(distribution.get_parameters()==[[1.1,1.2],[[0.1,0],[0,0.1]],[1,[1.4,0.7]]])
def test_sample(self):
m = np.array([0, 0])
cov = np.eye(2)
distribution = MultiStudentT(m, cov, 4)
samples = distribution.sample(10000000)
expected_mean = np.array([0., 0.])
expected_var = np.array([1.9978, 2.0005])
diff_mean = np.abs(samples.mean(axis=0) - expected_mean)
diff_var = np.abs(samples.var(axis=0) - expected_var)
self.assertLess(diff_mean.sum(), 2e-2)
self.assertLess(diff_var.sum(), 2e-2)
distribution = MultiStudentT(distribution, cov, 4, seed=1)
samples_graph = distribution.sample(10000000)
expected_mean_graph = np.array([0.006, 0.003])
expected_var_graph = np.array([1.99683813, 2.00319229])
diff_mean_graph = np.abs(samples_graph.mean(axis=0) - expected_mean_graph)
diff_var_graph = np.abs(samples_graph.var(axis=0)-expected_var_graph)
self.assertLess(diff_mean_graph.sum(), 3.5) #this value doesnt work at aaaaalllllll
def test_sample(self):
m = np.array([0, 0])
cov = np.eye(2)
distribution = MultiStudentT(m, cov, 4)
samples = distribution.sample(10000000)
expected_mean = np.array([0., 0.])
expected_var = np.array([1.9978, 2.0005])
diff_mean = np.abs(samples.mean(axis=0) - expected_mean)
diff_var = np.abs(samples.var(axis=0) - expected_var)
self.assertLess(diff_mean.sum(), 2e-2)
self.assertLess(diff_var.sum(), 2e-2)
distribution = MultiStudentT(distribution, cov, 4, seed=1)
samples_graph = distribution.sample(10000000)
expected_mean_graph = np.array([0.006, 0.003])
expected_var_graph = np.array([1.99683813, 2.00319229])
diff_mean_graph = np.abs(samples_graph.mean(axis=0) - expected_mean_graph)
diff_var_graph = np.abs(samples_graph.var(axis=0)-expected_var_graph)
self.assertLess(diff_mean_graph.sum(), 3.5) #this value doesnt work at aaaaalllllll
self.assertLess(diff_var_graph.sum(), 2e-2)
distribution = MultiStudentT(Uniform([1,2],[3,4],seed=1),cov,2,seed=1)
distribution = MultiStudentT([Normal(1,0.5,1),StudentT(1,2,seed=1)],cov,2,seed=1)
def test_pdf(self):
m = [0, 0]
cov = [[1,0],[0,1]]
distribution = MultiStudentT(m, cov, 1)
self.assertLess(abs(distribution.pdf([0., 0.]) - 0.15915), 1e-5)
cov = np.array([[2, 0], [0, 2]])
distribution = MultiStudentT(m, cov, 1)
self.assertLess(abs(distribution.pdf([0., 0.]) - 0.079577), 1e-5)
self.assertLess(abs(distribution.pdf([1., 1.]) - 0.028135), 1e-5)
# define the model
from abcpy.models import Gaussian
model = Gaussian(prior, seed=1)
# define statistics
from abcpy.statistics import Identity
statistics_calculator = Identity(degree = 2, cross = False)
# define distance
from abcpy.distances import LogReg
distance_calculator = LogReg(statistics_calculator)
# define kernel
from abcpy.distributions import MultiStudentT
mean, cov, df = np.array([.0, .0]), np.eye(2), 3.
kernel = MultiStudentT(mean, cov, df, seed=1)
# define sampling scheme
from abcpy.inferences import PMCABC
sampler = PMCABC(model, distance_calculator, kernel, backend, seed=1)
# sample from scheme
T, n_sample, n_samples_per_param = 3, 250, 10
eps_arr = np.array([.75])
epsilon_percentile = 10
journal = sampler.sample(y_obs, T, eps_arr, n_sample, n_samples_per_param, epsilon_percentile)
return journal
# define the model
model = Gaussian(prior)
# define statistics
from abcpy.statistics import Identity
statistics_calculator = Identity(degree = 2, cross = False)
# define distance
from abcpy.distances import LogReg
distance_calculator = LogReg(statistics_calculator)
# define kernel
from abcpy.distributions import MultiStudentT
mean, cov, df = np.array([.0, .0]), np.eye(2), 3.
kernel = MultiStudentT(mean, cov, df)
# define backend
from abcpy.backends import BackendSpark as Backend
from abcpy.backends import BackendDummy as Backend
backend = Backend()
# define sampling scheme
from abcpy.inferences import PMCABC
sampler = PMCABC(model, distance_calculator, kernel, backend)
# sample from scheme
T, n_sample, n_samples_per_param = 3, 100, 10
eps_arr = np.array([.75])
epsilon_percentile = 10
journal = sampler.sample(y_obs, T, eps_arr, n_sample, n_samples_per_param, epsilon_percentile)