Commit 5c4d0aba authored by Philipp Arras's avatar Philipp Arras

Generalize do_adjust_variances

parent 4be4bc7f
Pipeline #45230 passed with stages
in 9 minutes and 29 seconds
...@@ -17,9 +17,8 @@ ...@@ -17,9 +17,8 @@
from ..minimization.energy_adapter import EnergyAdapter from ..minimization.energy_adapter import EnergyAdapter
from ..multi_field import MultiField from ..multi_field import MultiField
from ..operators.distributors import PowerDistributor from ..operators.energy_operators import (InverseGammaLikelihood,
from ..operators.energy_operators import (StandardHamiltonian, StandardHamiltonian)
InverseGammaLikelihood)
from ..operators.scaling_operator import ScalingOperator from ..operators.scaling_operator import ScalingOperator
from ..operators.simple_linear_operators import ducktape from ..operators.simple_linear_operators import ducktape
...@@ -79,27 +78,23 @@ def make_adjust_variances_hamiltonian(a, ...@@ -79,27 +78,23 @@ def make_adjust_variances_hamiltonian(a,
ic_samp=ic_samp) ic_samp=ic_samp)
def do_adjust_variances(position, def do_adjust_variances(position, A, minimizer, xi_key='xi', samples=[]):
amplitude_operator,
minimizer,
xi_key='xi',
samples=[]):
"""Adjusts the variance of xi_key to be represented by amplitude_operator. """Adjusts the variance of xi_key to be represented by amplitude_operator.
Solves a constant likelihood optimization of the Solves a constant likelihood optimization of the
form phi = amplitude_operator * position[xi_key] under the constraint that form phi = A * position[xi_key] under the constraint that phi remains
phi remains constant. constant.
The field indexed by xi_key is desired to be a Gaussian white Field, The field indexed by xi_key is desired to be a Gaussian white Field,
thus variations that are more easily represented by amplitude_operator thus variations that are more easily represented by A will be absorbed in
will be absorbed in amplitude_operator. A.
Parameters Parameters
---------- ----------
position : Field, MultiField position : Field, MultiField
Contains the initial values for amplitude_operator and the key xi_key, Contains the initial values for amplitude_operator and the key xi_key,
to be adjusted. to be adjusted.
amplitude_operator : Operator A : Operator
Gives the amplitude when evaluated at position. Gives the amplitude when evaluated at position.
minimizer : Minimizer minimizer : Minimizer
Used to solve the optimization problem. Used to solve the optimization problem.
...@@ -113,27 +108,20 @@ def do_adjust_variances(position, ...@@ -113,27 +108,20 @@ def do_adjust_variances(position,
Returns Returns
------- -------
MultiField MultiField
The new position after variances were adjusted. The new position after variances have been adjusted.
""" """
h_space = position[xi_key].domain[0]
pd = PowerDistributor(h_space, amplitude_operator.target[0])
a = pd(amplitude_operator)
xi = ducktape(None, position.domain, xi_key) xi = ducktape(None, position.domain, xi_key)
ham = make_adjust_variances_hamiltonian(A, xi, position, samples=samples)
ham = make_adjust_variances_hamiltonian(a, xi, position, samples=samples)
# Minimize # Minimize
e = EnergyAdapter( e = EnergyAdapter(
position.extract(a.domain), ham, constants=[], want_metric=True) position.extract(A.domain), ham, constants=[], want_metric=True)
e, _ = minimizer(e) e, _ = minimizer(e)
# Update position # Update position
s_h_old = (a*xi).force(position) s_h_old = (A*xi).force(position)
position = position.to_dict() position = position.to_dict()
position[xi_key] = s_h_old/a(e.position) position[xi_key] = s_h_old/A(e.position)
position = MultiField.from_dict(position) position = MultiField.from_dict(position)
position = MultiField.union([position, e.position]) return MultiField.union([position, e.position])
return position
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment