def run_prior_solve(): componentstorage = TestComponentDefinition() solutionstorage = SpaceTimeComponentSolutionStorage_InMemory() printstats=True compute_uncertainties = False method='EXACT' compute_sample=False sample_size=1 compute_prior_sample=True component = SpaceTimeComponent(componentstorage, solutionstorage, printstats, compute_uncertainties, method, compute_sample, sample_size, compute_prior_sample) component_solution = component.component_solution() component_solution.update()
def test_process_observations_compute_sample(self): # Our test system is: # # ( [ 2.0 0.0 ] + [ -1.5 0.0 ] [ 5.0 0.0 ] [ -1.5 2.2 ] ) x = [ -1.5 0.0 ] [ 10.0 3.0 ] [ 7.0 - 2.0 ] # ( [ 0.0 2.0 ] [ 2.2 3.3 ] [ 0.0 5.0 ] [ 0.0 3.3 ] ) [ 2.2 3.3 ] [ 3.0 10.0 ] [ 9.0 - 3.0 ] # # [ 13.25 -16.5 ] x = [ -37.5 ] # [-16.5 80.65 ] [ 154.0 ] # # => x = [ -0.60697861 ] # [ 1.78530506 ] # number_of_samples = 200 c = SpaceTimeComponent(ComponentStorage_InMemory( TestSpaceTimeComponentSolution.TestElement(), CovariateHyperparameters(-0.5 * numpy.log(2.0))), SpaceTimeComponentSolutionStorage_InMemory(), compute_sample=True, sample_size=number_of_samples) s = c.component_solution() self.assertIsInstance(s, SpaceTimeComponentSolution) self.assertTrue(s.compute_sample) test_offset = numpy.array([2.0, 3.0]) s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=21), test_offset[0:1]) s.update_time_step() s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=532), test_offset[1:2]) s.update_time_step() numpy.random.seed(0) s.update() # In this case we are considering the last iteration of model solving, hence sample should have been stored computed_sample = s.solutionstorage.state_sample self.assertEqual((2, number_of_samples), computed_sample.shape) numpy.random.seed(0) variate = scipy.random.normal(0.0, 1.0, (2, number_of_samples)) expected_posterior_precision = numpy.array([[13.25, -16.5], [-16.5, 80.65]]) # check result numpy.testing.assert_almost_equal( numpy.dot(variate.T, variate), numpy.dot(computed_sample.T, numpy.dot(expected_posterior_precision, computed_sample)))
def test_process_observations_no_uncertainties(self): # Our test system is: # # ( [ 2.0 0.0 ] + [ -1.5 0.0 ] [ 5.0 0.0 ] [ -1.5 2.2 ] ) x = [ -1.5 0.0 ] [ 10.0 3.0 ] [ 7.0 - 2.0 ] # ( [ 0.0 2.0 ] [ 2.2 3.3 ] [ 0.0 5.0 ] [ 0.0 3.3 ] ) [ 2.2 3.3 ] [ 3.0 10.0 ] [ 9.0 - 3.0 ] # # [ 13.25 -16.5 ] x = [ -37.5 ] # [-16.5 80.65 ] [ 154.0 ] # # => x = [ -0.60697861 ] # [ 1.78530506 ] # c = SpaceTimeComponent(ComponentStorage_InMemory( TestSpaceTimeComponentSolution.TestElement(), CovariateHyperparameters(-0.5 * numpy.log(2.0))), SpaceTimeComponentSolutionStorage_InMemory(), sample_size=666) s = c.component_solution() self.assertIsInstance(s, SpaceTimeComponentSolution) self.assertFalse(s.compute_uncertainties) test_offset = numpy.array([2.0, 3.0]) s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=21), test_offset[0:1]) s.update_time_step() s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=532), test_offset[1:2]) s.update_time_step() s.update() numpy.testing.assert_almost_equal( s.solutionstorage.state, numpy.array([-0.60697861, 1.78530506])) # No marginal variances should have been computed at all, same for the sample self.assertEqual(None, s.solutionstorage.state_marginal_std) self.assertEqual(None, s.solutionstorage.state_sample) for time, expected_array in zip([21, 532], [ numpy.array([-1.5 * -0.60697861 + 2.2 * 1.78530506]), numpy.array([3.3 * 1.78530506]) ]): # Observation at time t=t* should be design matrix for that time multiplied by expected state numpy.testing.assert_almost_equal( s.solution_observation_expected_value( TestSpaceTimeComponentSolution.TestObservations(t=time)), expected_array) # In this case we are considering a generical model solving iteration for the model, no marginal variances stored, hence we expect 0. as observation uncertainties numpy.testing.assert_array_equal( s.solution_observation_expected_uncertainties( TestSpaceTimeComponentSolution.TestObservations(t=time)), 0.) # check samples are zero numpy.testing.assert_array_equal( 0., s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations(t=time))) # check default number of samples self.assertEqual( s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations( t=time)).shape[1], 666)
def test_process_observations_compute_uncertainties(self): # Our test system is: # # ( [ 2.0 0.0 ] + [ -1.5 0.0 ] [ 5.0 0.0 ] [ -1.5 2.2 ] ) x = [ -1.5 0.0 ] [ 10.0 3.0 ] [ 7.0 - 2.0 ] # ( [ 0.0 2.0 ] [ 2.2 3.3 ] [ 0.0 5.0 ] [ 0.0 3.3 ] ) [ 2.2 3.3 ] [ 3.0 10.0 ] [ 9.0 - 3.0 ] # # [ 13.25 -16.5 ] x = [ -37.5 ] # [-16.5 80.65 ] [ 154.0 ] # # => x = [ -0.60697861 ] # [ 1.78530506 ] # c = SpaceTimeComponent(ComponentStorage_InMemory( TestSpaceTimeComponentSolution.TestElement(), CovariateHyperparameters(-0.5 * numpy.log(2.0))), SpaceTimeComponentSolutionStorage_InMemory(), compute_uncertainties=True) s = c.component_solution() self.assertIsInstance(s, SpaceTimeComponentSolution) self.assertTrue(s.compute_uncertainties) test_offset = numpy.array([2.0, 3.0]) s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=21), test_offset[0:1]) s.update_time_step() s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=532), test_offset[1:2]) s.update_time_step() s.update() # No sample should have been computed at all self.assertEqual(None, s.solutionstorage.state_sample) # In this case we are considering the last iteration of model solving, hence marginal variances should have been stored expected_marginal_std = numpy.sqrt( numpy.diag( numpy.linalg.inv(numpy.array([[13.25, -16.5], [-16.5, 80.65]])))) numpy.testing.assert_array_almost_equal( s.solutionstorage.state_marginal_std, expected_marginal_std) # Now we compute the projection of marginal variances onto the given observations for time in [532]: # Observation at time t=t* should be design matrix for that time multiplied by expected state expected_projection = TestSpaceTimeComponentSolution.TestDesign( t=time).design_function(expected_marginal_std) numpy.testing.assert_almost_equal( s.solution_observation_expected_uncertainties( TestSpaceTimeComponentSolution.TestObservations(t=time)), expected_projection) # check samples are zero numpy.testing.assert_array_equal( 0., s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations(t=time))) # check default number of samples self.assertEqual( s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations( t=time)).shape[1], 1)