adjust_variances.py 1.34 KB
Newer Older
Philipp Frank's avatar
Philipp Frank committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from ..operators.energy_operators import InverseGammaLikelihood
from ..operators.scaling_operator import ScalingOperator

def make_adjust_variances(a,xi,position,samples=[],scaling=None):
    """ Creates a Likelihood for constant likelihood optimizations.
    
    Constructs a Likelihood to solve constant likelihood optimizations of the form
        phi = a * xi
    under the constraint that phi remains constant.
    
    Parameters
    ----------
    a : Operator
        Operator which gives the amplitude when evaluated at a position
    xi : Operator
        Operator which gives the excitation when evaluated at a position
    postion : Field, MultiField
        Position of the whole problem
    res_samples : Field, MultiField
        Residual samples of the whole Problem
    scaling : Float
        Optional rescaling of the Likelihood

    Returns
    -------
    InverseGammaLikelihood
        A Likelihood that can be used for further minimization
    """

    d = a * xi
    d = (d.conjugate()*d).real
    n = len(samples)
    if n>0:
        d_eval = 0.
        for i in range(n):
            d_eval = d_eval + d(position+samples[i])
        d_eval = d_eval / n
    else:
        d_eval = d(position)

    x = (a.conjugate()*a).real
    if scaling is not None:
        x = ScalingOperator(scaling,x.target)(x)

    return InverseGammaLikelihood(x,d_eval)