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(self):
model = Normal([1, 0.1])
Manager = AcceptedParametersManager([model])
backend = Backend()
Manager.update_kernel_values(backend, [1])
self.assertEqual(Manager.kernel_parameters_bds.value(),[1])
def setUp(self):
# find spark and initialize it
self.backend = BackendDummy()
# define a uniform prior distribution
mu = Uniform([[-5.0], [5.0]], name='mu')
sigma = Uniform([[0.0], [10.0]], name='sigma')
# define a Gaussian model
self.model = Normal([mu, sigma])
# define a distance function
stat_calc = Identity(degree=2, cross=0)
self.dist_calc = Euclidean(stat_calc)
# create fake observed data
#self.observation = self.model.forward_simulate(1, np.random.RandomState(1))[0].tolist()
self.observation = [np.array(9.8)]
def test_sample(self):
# setup backend
backend = BackendDummy()
# define a uniform prior distribution
mu = Uniform([[-5.0], [5.0]], name='mu')
sigma = Uniform([[0.0], [10.0]], name='sigma')
# define a Gaussian model
self.model = Normal([mu,sigma])
# define sufficient statistics for the model
stat_calc = Identity(degree = 2, cross = 0)
# create fake observed data
#y_obs = self.model.forward_simulate(1, np.random.RandomState(1))[0].tolist()
y_obs = [np.array(9.8)]
# Define the likelihood function
likfun = SynLiklihood(stat_calc)
def setUp(self):
# find spark and initialize it
self.backend = BackendDummy()
# define a uniform prior distribution
mu = Uniform([[-5.0], [5.0]], name='mu')
sigma = Uniform([[0.0], [10.0]], name='sigma')
# define a Gaussian model
self.model = Normal([mu, sigma])
# define a distance function
stat_calc = Identity(degree=2, cross=0)
self.dist_calc = Euclidean(stat_calc)
# create fake observed data
#self.observation = self.model.forward_simulate(1, np.random.RandomState(1))[0].tolist()
self.observation = [np.array(9.8)]
# A quantity determining whether a student receives a scholarship, including his social background
final_scholarship = scholarship_without_additional_effects + 3*background
# Define a summary statistics for final grade and final scholarship
from abcpy.statistics import Identity
statistics_calculator_final_grade = Identity(degree = 2, cross = False)
statistics_calculator_final_scholarship = Identity(degree = 3, cross = False)
# Define a distance measure for final grade and final scholarship
from abcpy.distances import Euclidean
distance_calculator_final_grade = Euclidean(statistics_calculator_final_grade)
distance_calculator_final_scholarship = Euclidean(statistics_calculator_final_scholarship)
# Define a backend
from abcpy.backends import BackendDummy as Backend
backend = Backend()
# Define a perturbation kernel
from abcpy.perturbationkernel import DefaultKernel
kernel = DefaultKernel([school_location, class_size, grade_without_additional_effects, \
background, scholarship_without_additional_effects])
# Define sampling parameters
T, n_sample, n_samples_per_param = 3, 250, 10
eps_arr = np.array([.75])
epsilon_percentile = 10
# Define sampler
from abcpy.inferences import PMCABC
sampler = PMCABC([final_grade, final_scholarship], \
[distance_calculator_final_grade, distance_calculator_final_scholarship], backend, kernel)
def infer_parameters():
# define backend
# Note, the dummy backend does not parallelize the code!
from abcpy.backends import BackendDummy as Backend
backend = Backend()
# define observation for true parameters mean=170, std=15
height_obs = [160.82499176, 167.24266737, 185.71695756, 153.7045709, 163.40568812, 140.70658699, 169.59102084, 172.81041696, 187.38782738, 179.66358934, 176.63417241, 189.16082803, 181.98288443, 170.18565017, 183.78493886, 166.58387299, 161.9521899, 155.69213073, 156.17867343, 144.51580379, 170.29847515, 197.96767899, 153.36646527, 162.22710198, 158.70012047, 178.53470703, 170.77697743, 164.31392633, 165.88595994, 177.38083686, 146.67058471763457, 179.41946565658628, 238.02751620619537, 206.22458790620766, 220.89530574344568, 221.04082532837026, 142.25301427453394, 261.37656571434275, 171.63761180867033, 210.28121820385866, 237.29130237612236, 175.75558340169619, 224.54340549862235, 197.42448680731226, 165.88273684581381, 166.55094082844519, 229.54308602661584, 222.99844054358519, 185.30223966014586, 152.69149367593846, 206.94372818527413, 256.35498655339154, 165.43140916577741, 250.19273595481803, 148.87781549665536, 223.05547559193792, 230.03418198709608, 146.13611923127021, 138.24716809523139, 179.26755740864527, 141.21704876815426, 170.89587081800852, 222.96391329259626, 188.27229523693822, 202.67075179617672, 211.75963110985992, 217.45423324370509]
# define prior
from abcpy.continuousmodels import Uniform
mu = Uniform([[150], [200]], )
sigma = Uniform([[5], [25]], )
# define the model
from abcpy.continuousmodels import Normal
height = Normal([mu, sigma], )
# define statistics
from abcpy.statistics import Identity
statistics_calculator = Identity(degree=3, cross=True)
# 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.perturbationkernel import DefaultKernel
kernel = DefaultKernel([mu, sigma])
# define backend
# Note, the dummy backend does not parallelize the code!
from abcpy.backends import BackendDummy as Backend
backend = Backend()
# define sampling scheme
from abcpy.inferences import PMCABC
sampler = PMCABC([height], [distance_calculator], backend, kernel, 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([height_obs], T, eps_arr, n_sample, n_samples_per_param, epsilon_percentile)
return journal
prior = Uniform([[150, 5],[200, 25]])
# 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 backend
from abcpy.backends import BackendDummy as Backend
backend = Backend()
# define sampling scheme
from abcpy.inferences import PMCABC
sampler = PMCABC([model], distance_calculator, backend)
# 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