def test_gaussian_energy(space, nonlinearity, noise, seed): np.random.seed(seed) dim = len(space.shape) hspace = space.get_default_codomain() ht = ift.HarmonicTransformOperator(hspace, target=space) binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False) pspace = ift.PowerSpace(hspace, binbounds=binbounds) Dist = ift.PowerDistributor(target=hspace, power_space=pspace) xi0 = ift.Field.from_random(domain=hspace, random_type='normal') def pspec(k): return 1 / (1 + k**2)**dim pspec = ift.PS_field(pspace, pspec) A = Dist(ift.sqrt(pspec)) N = ift.ScalingOperator(noise, space) n = N.draw_sample() R = ift.ScalingOperator(10., space) def d_model(): if nonlinearity == "": return R(ht(ift.makeOp(A))) else: tmp = ht(ift.makeOp(A)) nonlin = getattr(tmp, nonlinearity)() return R(nonlin) d = d_model()(xi0) + n if noise == 1: N = None energy = ift.GaussianEnergy(d, N)(d_model()) ift.extra.check_jacobian_consistency(energy, xi0, ntries=10, tol=5e-8)
def test_hamiltonian_and_KL(field): field = field.exp() space = field.domain lh = ift.GaussianEnergy(domain=space) hamiltonian = ift.StandardHamiltonian(lh) ift.extra.check_jacobian_consistency(hamiltonian, field) S = ift.ScalingOperator(1., space) samps = [S.draw_sample() for i in range(3)] kl = ift.AveragedEnergy(hamiltonian, samps) ift.extra.check_jacobian_consistency(kl, field)
### SETTING UP SPECIFIC SCENARIO #### R = ift.GeometryRemover(position_space) data_space = R.target signal_response = R(correlated_field) # Set up likelihood and load data N = ift.ScalingOperator(0.1, data_space) data, ground_truth = generate_mysterious_data(position_space) data = ift.from_global_data(data_space, data) likelihood = ift.GaussianEnergy(mean=data, inverse_covariance=N.inverse)(signal_response) #### SOLVING PROBLEM #### ic_sampling = ift.GradientNormController(iteration_limit=100) ic_newton = ift.GradInfNormController( name='Newton', tol=1e-6, iteration_limit=30) minimizer = ift.NewtonCG(ic_newton) H = ift.StandardHamiltonian(likelihood, ic_sampling) initial_mean = ift.MultiField.full(H.domain, 0.) mean = initial_mean # number of samples used to estimate the KL N_samples = 10
def test_gaussian(field): energy = ift.GaussianEnergy(domain=field.domain) ift.extra.check_jacobian_consistency(energy, field)
data_space = R.target noise = .001 N = ift.ScalingOperator(noise, data_space) # Generate mock signal and data mock_position = ift.from_random('normal', signal_response.domain) data = signal_response(mock_position) + N.draw_sample() # Minimization parameters ic_sampling = ift.GradientNormController(iteration_limit=100) ic_newton = ift.GradInfNormController( name='Newton', tol=1e-7, iteration_limit=35) minimizer = ift.NewtonCG(ic_newton) # Set up likelihood and information Hamiltonian likelihood = ift.GaussianEnergy(mean=data, covariance=N)(signal_response) H = ift.StandardHamiltonian(likelihood, ic_sampling) initial_mean = ift.MultiField.full(H.domain, 0.) mean = initial_mean plot = ift.Plot() plot.add(signal(mock_position), title='Ground Truth') plot.add(R.adjoint_times(data), title='Data') plot.add([A.force(mock_position)], title='Power Spectrum') plot.output(ny=1, nx=3, xsize=24, ysize=6, name=filename.format("setup")) # number of samples used to estimate the KL N_samples = 20 # Draw new samples to approximate the KL five times