コード例 #1
0
ファイル: argmax.py プロジェクト: cosmosZhou/sagemath
def apply(*given):
    x_equality, inequality = given

    assert inequality.is_Unequality
    xy_probability, zero = inequality.args
    assert xy_probability.is_Probability
    assert zero.is_zero
    xy_joint = xy_probability.arg

    assert x_equality.is_Equality

    x_t_given, x_t = x_equality.args

    assert x_t_given.is_Conditioned
    _x_t, x_t_historic = x_t_given.args

    assert x_t == _x_t

    x, t = x_t.args
    assert x_t_historic == x[:t].as_boolean()
    x_boolean, y_boolean = xy_joint._argset
    if x_boolean != x.as_boolean():
        x_boolean, y_boolean = y_boolean, x_boolean

    y = y_boolean.lhs

    assert t.is_positive
    i = Symbol.i(integer=True)
    j = Symbol.j(integer=True)
    return Equality(ArgMax[i](P(y[i] | x)),
                    ArgMax[i](P(y[i]) * Product[j](P(x[j] | y[i]))),
                    given=given)
コード例 #2
0
def apply(*given):
    x, y = process_assumptions(*given)
    n, d = x.shape
    t = Symbol.t(integer=True, domain=[0, n - 1])
    i = Symbol.i(integer=True)

    joint_probability = P(x[:t + 1], y[:t + 1])
    emission_probability = P(x[i] | y[i])
    transition_probability = P(y[i] | y[i - 1])
    y_given_x_probability = P(y | x)
    y = pspace(y).symbol

    G = Symbol.G(shape=(d, d),
                 definition=LAMBDA[y[i - 1],
                                   y[i]](-sympy.log(transition_probability)))
    s = Symbol.s(shape=(n, ), definition=LAMBDA[t](-log(joint_probability)))
    x = Symbol.x(shape=(n, d),
                 definition=LAMBDA[y[i], i](-sympy.log(emission_probability)))

    z = Symbol.z(shape=(n, d),
                 definition=LAMBDA[y[t], t](Piecewise(
                     (Sum[y[0:t]](sympy.E**-s[t]), t > 0),
                     (sympy.E**-s[t], True))))
    #     assert z.is_extended_positive

    x_quote = Symbol.x_quote(shape=(n, d),
                             definition=-LAMBDA[t](sympy.log(z[t])))

    #     assert x_quote.is_real

    return Equality(x_quote[t + 1], -log(Sum(exp(-x_quote[t] - G))) + x[t + 1], given=given), \
        Equality(-log(y_given_x_probability), log(Sum(exp(-x_quote[n - 1]))) + s[n - 1], given=given)
コード例 #3
0
ファイル: nonzero.py プロジェクト: cosmosZhou/sagemath
def apply(*given):
    given_x, given_y, unequality = given
    assert given_x.is_Equality
    assert given_y.is_Equality
    assert unequality.is_Unequality
    assert unequality.rhs.is_zero
    
    x = given_x.rhs
    y = given_y.rhs
    assert unequality.lhs.arg.lhs in {x, y} 
    
    eq = given_x.lhs
    assert eq.is_Conditioned
    _x, z = eq.args

    eq = given_y.lhs
    assert eq.is_Conditioned
    _y, _z = eq.args
    
    assert x == _x
    assert y == _y
    assert z == _z
    
    assert x.is_random and y.is_random and z.is_random
    return Equality(P(x, y, given=z), P(x, y), given=given)
コード例 #4
0
ファイル: nonzero.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)
    z = Symbol.z(real=True, random=True)
        
    Eq << apply(x.is_independent_of(z), y.is_independent_of(z), Unequal(P(y), 0))
    
    Eq << bayes.equality.equality.product.apply(Eq[0])
    
    Eq.bayes_yz = bayes.equality.equality.product.apply(Eq[1])
    
    Eq.bayes_xyz = bayes.theorem.apply(Eq[0].domain_definition(), var=P(x, y))
    
    Eq << Eq[2].subs(Eq[1].reversed)
    
    Eq.given_addition = bayes.equality.equality.given_addition.condition_probability.apply(Eq[0], Eq[-1])
    
    Eq << bayes.inequality.inequality.joint.apply(Eq[-1])
    
    Eq << bayes.theorem.apply(Eq[-1], var=x)
    
    Eq << Eq.bayes_xyz.subs(Eq[-1])
    
    Eq << Eq[-1].subs(Eq.bayes_yz)
    
    Eq << algebre.scalar.inequality.equality.apply(Eq[-1], Eq[0].domain_definition())
    
    Eq << Eq[-1].subs(Eq.given_addition)
    
    Eq << bayes.theorem.apply(Eq[2], var=x)
    
    Eq << Eq[-2].subs(Eq[-1].reversed)
    
    Eq << Eq[-1].reversed
コード例 #5
0
def apply(*given):
    equality, unequal = given
    if equality.is_Unequality:
        equality, unequal = unequal, equality
    assert unequal.is_Unequality
    assert unequal.lhs.is_Probability
    assert unequal.rhs.is_zero
    _y, z = unequal.lhs.arg.args
        
    assert equality.is_Equality
    lhs, rhs = equality.args
    if lhs.is_Probability:
        assert rhs.is_Probability
        lhs = lhs.arg
        rhs = rhs.arg
        
    assert lhs.is_Conditioned
    x, y = lhs.args
    assert x == rhs
    
    if _y != y:
        _y, z = z, _y
        
    assert _y == y    
    
    if x.is_symbol:
        return Equality(x | y & z, x | z, given=given)
    else:
        return Equality(P(x, given=y & z), P(x, given=z), given=given)
コード例 #6
0
def apply(*given):
    x, y = process_assumptions(*given)

    n, d = x.shape
    t = Symbol.t(integer=True, domain=[0, n - 1])
    i = Symbol.i(integer=True)

    joint_probability_t = P(x[:t + 1], y[:t + 1])
    joint_probability = P(x, y)
    emission_probability = P(x[i] | y[i])
    transition_probability = P(y[i] | y[i - 1])
    y = pspace(y).symbol

    G = Symbol.G(shape=(d, d),
                 definition=LAMBDA[y[i - 1],
                                   y[i]](-sympy.log(transition_probability)))
    s = Symbol.s(shape=(n, ), definition=LAMBDA[t](-log(joint_probability_t)))
    x = Symbol.x(shape=(n, d),
                 definition=LAMBDA[y[i], i](-sympy.log(emission_probability)))

    x_quote = Symbol.x_quote(shape=(n, d),
                             definition=LAMBDA[y[t], t](Piecewise(
                                 (MIN[y[0:t]](s[t]), t > 0), (s[0], True))))

    assert x_quote.is_real
    return Equality(x_quote[t + 1], x[t + 1] + MIN(x_quote[t] + G), given=given), \
        Equality(MAX[y](joint_probability), exp(-MIN(x_quote[n - 1])), given=given)
コード例 #7
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)

    Eq << apply(Unequal(P(x), 0))

    Eq << GreaterThan(P(x), 0, plausible=True)

    Eq <<= Eq[-1] & Eq[0]
コード例 #8
0
def apply(*given):
    x, y = process_assumptions(*given)
    n, _ = x.shape
    t = Symbol.t(integer=True, domain=[0, n - 1])
    i = Symbol.i(integer=True)

    return Equality(P(x[:t + 1], y[:t + 1]),
                    P(x[0] | y[0]) * P(y[0]) *
                    Product[i:1:t](P(y[i] | y[i - 1]) * P(x[i] | y[i])),
                    given=given)
コード例 #9
0
ファイル: logits.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    # d is the number of output labels
    # oo is the length of the sequence
    d = Symbol.d(integer=True, positive=True)
    n = Symbol.n(integer=True, positive=True)
    x = Symbol.x(shape=(n, d), real=True, random=True, given=True)
    y = Symbol.y(shape=(n, ),
                 integer=True,
                 domain=[0, d - 1],
                 random=True,
                 given=True)

    i = Symbol.i(integer=True)
    t = Symbol.t(integer=True, domain=[0, n])

    joint_probability_t = P(x[:t + 1], y[:t + 1])
    emission_probability = P(x[i] | y[i])
    transition_probability = P(y[i] | y[i - 1])

    given = Equality(
        joint_probability_t,
        P(x[0] | y[0]) * P(y[0]) *
        Product[i:1:t](transition_probability * emission_probability))

    y = pspace(y).symbol
    G = Symbol.G(shape=(d, d),
                 definition=LAMBDA[y[i - 1],
                                   y[i]](-sympy.log(transition_probability)))
    s = Symbol.s(shape=(n, ), definition=LAMBDA[t](-log(joint_probability_t)))
    x = Symbol.x(shape=(n, d),
                 definition=LAMBDA[y[i], i](-sympy.log(emission_probability)))

    Eq.s_definition, Eq.G_definition, Eq.x_definition, Eq.given, Eq.logits_recursion = apply(
        G, x, s, given=given)

    Eq << Eq.s_definition.this.rhs.subs(Eq.given)

    Eq << Eq[-1].this.rhs.args[1].as_Add()

    Eq << Eq[-1].subs(Eq.x_definition.subs(i, 0).reversed)

    Eq << Eq[-1].this.rhs.args[-1].args[1].as_Add()

    Eq << Eq[-1].this.rhs.args[-1].args[1].function.as_Add()

    Eq << Eq[-1].this.rhs.args[-1].args[1].as_two_terms()

    Eq << Eq[-1].subs(Eq.x_definition.reversed).subs(Eq.G_definition.reversed)

    Eq << Eq[-1].this.rhs.args[-1].bisect({0})

    Eq << Eq[-1].subs(t, t + 1) - Eq[-1]

    s = Eq.s_definition.lhs.base
    Eq << Eq[-1].this.rhs.simplify() + s[t]
コード例 #10
0
ファイル: product.py プロジェクト: cosmosZhou/sagemath
def apply(given):
    assert given.is_Equality
    lhs, rhs = given.args

    assert lhs.is_Conditioned

    x, y = lhs.args

    assert x == rhs

    return Equality(P(x, y), P(x) * P(y), given=given)
コード例 #11
0
ファイル: argmax.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    t = Symbol.t(integer=True, positive=True)
    n = Symbol.n(integer=True, positive=True)
    m = Symbol.m(integer=True, positive=True)
    x = Symbol.x(real=True, shape=(n, ), random=True)
    y = Symbol.y(real=True, shape=(m, ), random=True)

    Eq << apply(Equal(x[t] | x[:t], x[t]), Unequal(P(x, y), 0))

    i = Eq[-1].lhs.variable
    j = Eq[-1].rhs.function.args[-1].variable

    Eq << bayes.inequality.et.apply(Eq[1]).split()
    Eq << bayes.theorem.apply(Eq[-2], var=y[i])

    Eq.y_given_x = algebre.scalar.inequality.equality.apply(Eq[-1],
                                                            Eq[-3]).reversed

    Eq << bayes.inequality.inequality.joint_slice.apply(Eq[1], [j, i])

    Eq << bayes.inequality.et.apply(Eq[-1]).split()

    Eq << bayes.theorem.apply(Eq[-1], var=x)

    Eq.y_given_x = Eq.y_given_x.subs(Eq[-1])

    Eq << Eq.y_given_x.argmax((i, ))

    Eq << bayes.inequality.inequality.joint_slice.apply(Eq[1], Slice[:t, i])

    Eq.xt_given_x_historic = bayes.equality.equality.given_addition.joint_probability.apply(
        Eq[0], Eq[-1])

    Eq.xt_given_yi_nonzero = bayes.inequality.inequality.conditioned.apply(
        Eq[-1], wrt=y[i])

    Eq << P(x[:t + 1] | y[i]).bayes_theorem(x[:t]).as_Or()
    Eq << (Eq[-1] & Eq.xt_given_yi_nonzero).split()

    Eq << Eq[-1].subs(Eq.xt_given_x_historic)

    Eq << algebre.scalar.inequality.equality.apply(Eq[-1],
                                                   Eq.xt_given_yi_nonzero)

    Eq << Eq[-1].product((t, 1, n - 1))

    t = Eq[-1].rhs.variable
    Eq << Eq[-1] / Eq[-1].lhs.args[-1]

    Eq << Eq[-1].this.rhs.limits_subs(t, j)

    Eq << Eq[2].subs(Eq[-1].reversed)
コード例 #12
0
ファイル: marginal.py プロジェクト: cosmosZhou/sagemath
def apply(given):
    assert given.is_Unequality
    assert given.lhs.is_Probability
    assert given.rhs.is_zero
    eq = given.lhs.arg
    assert eq.is_Conditioned
    return Unequal(P(eq.lhs), 0, given=given)
コード例 #13
0
ファイル: conditioned.py プロジェクト: cosmosZhou/sagemath
def apply(given, wrt):
    assert given.is_Unequality
    assert given.lhs.is_Probability
    assert given.rhs.is_zero

    probability = given.lhs
    p = probability.marginalize(wrt)

    return Unequal(P(p.arg | wrt), 0, given=given)
コード例 #14
0
ファイル: marginal.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)

    Eq << apply(Unequal(P(x | y), 0))

    Eq << bayes.inequality.inequality.joint.apply(Eq[0])

    Eq << bayes.inequality.et.apply(Eq[-1]).split()
コード例 #15
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)
    
    Eq << apply(Unequal(P(x, y), 0))
    
    Eq.x_marginal_probability, Eq.y_marginal_probability = P(x, y).total_probability_theorem(y), P(x, y).total_probability_theorem(x)
    
    _y = Eq.x_marginal_probability.lhs.variable
    _x = Eq.y_marginal_probability.lhs.variable
    
    Eq << bayes.inequality.strict_greater_than.apply(Eq[0])
    
    Eq <<= Eq[-1].integrate((_y,)), Eq[-1].integrate((_x,))
    
    Eq <<= Eq[-2].subs(Eq.x_marginal_probability), Eq[-1].subs(Eq.y_marginal_probability)
    
    Eq <<= algebre.scalar.strict_greater_than.inequality.apply(Eq[-1]) & algebre.scalar.strict_greater_than.inequality.apply(Eq[-2])
コード例 #16
0
def apply(*given):
    equality, inequality = given    
    assert equality.is_Equality
    assert inequality.is_Unequality
    assert inequality.rhs.is_zero
    inequality.lhs.is_Probability 
    x = inequality.lhs.arg
    
    lhs, rhs = equality.args
    assert lhs.is_Probability
    _x, y = lhs.arg.args

    if x != _x:
        _x, y = y, _x
    assert x == _x
    assert rhs == P(x) * P(y)
   
    return Equality(P(y, given=x), P(y), given=given)
コード例 #17
0
def apply(given, wrt=None):
    assert given.is_Unequality
    assert given.lhs.is_Probability
    assert given.rhs.is_zero
    
    eqs = given.lhs.arg
    assert eqs.is_And
    if wrt is not None:
        lhs, rhs = [], []
        for eq in eqs.args:
            if eq.lhs in wrt:
                rhs.append(eq)
            else:
                lhs.append(eq)
        lhs = And(*lhs)
        rhs = And(*rhs)
        return And(Unequal(P(lhs), 0), Unequal(P(rhs), 0), given=given)            
    else:
        return And(*[Unequal(P(eq), 0) for eq in eqs.args], given=given)
コード例 #18
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)
    z = Symbol.z(real=True, random=True)
    
    Eq << apply(Equality(x | y, x), Unequal(P(y, z), 0))
    
    Eq << bayes.inequality.inequality.conditioned.apply(Eq[1], y)
    
    Eq << bayes.equality.equality.given_addition.condition_probability.apply(Eq[0], Eq[-1])
コード例 #19
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)

    given = Equality(x | y, x)

    Eq << apply(given, Unequal(P(x), 0))

    Eq << bayes.equality.equality.product.apply(given)

    Eq << bayes.equality.equality.independence.apply(Eq[-1], Eq[1])
コード例 #20
0
ファイル: slice.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    n = Symbol.n(integer=True, domain=[2, oo])
    x = Symbol.x(real=True, shape=(n,), random=True)
    
    Eq << apply(Unequal(P(x), 0))
    
    t = Symbol.t(integer=True, domain=[1, n - 1])
    
    Eq << Eq[0].this.lhs.arg.bisect(Slice[:t])
     
    Eq << bayes.inequality.et.apply(Eq[-1]).split()
コード例 #21
0
ファイル: slice.py プロジェクト: cosmosZhou/sagemath
def apply(given):
    assert given.is_Unequality
    assert given.lhs.is_Probability
    assert given.rhs.is_zero
    
    eq = given.lhs.arg
    x, _x = eq.args
    assert _x == pspace(x).symbol
    n = x.shape[0]
    t = Symbol.t(integer=True, domain=[1, n - 1])
    return Unequal(P(x[:t]), 0, given=given)
コード例 #22
0
ファイル: symmetry_xy.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)

    given = Equality(x | y, x)

    Eq << apply(given, Unequal(P(x, y), 0))

    Eq << bayes.inequality.et.apply(Eq[1]).split()

    Eq << bayes.equality.equality.symmetry.apply(Eq[0], Eq[-2])
コード例 #23
0
ファイル: joint_nonzero.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)
    z = Symbol.z(real=True, random=True)

    Eq << apply(x.is_independent_of(z), y.is_independent_of(z),
                Unequal(P(x, y), 0))

    Eq << bayes.inequality.et.apply(Eq[2]).split()

    Eq << bayes.equality.equality.conditional_joint_probability.nonzero.apply(
        Eq[0], Eq[1], Eq[-1])
コード例 #24
0
def process_assumptions(*given):
    x_independence_assumption, y_independence_assumption, xy_independence_assumption, xy_nonzero_assumption = given
    assert xy_nonzero_assumption.is_Unequality
    assert xy_nonzero_assumption.rhs.is_zero

    x = x_independence_assumption.rhs.base
    y = y_independence_assumption.lhs.lhs.base
    assert y_independence_assumption.lhs.lhs == y_independence_assumption.rhs.lhs

    assert xy_nonzero_assumption.lhs == P(x, y)

    assert xy_independence_assumption.rhs.base == y
    return x, y
コード例 #25
0
ファイル: conditioned.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)

    Eq << apply(Unequal(P(x, y), 0), y)

    Eq << bayes.inequality.et.apply(Eq[0]).split()

    Eq << bayes.theorem.apply(Eq[-1], var=Eq[0].lhs)

    Eq << Eq[0].subs(Eq[-1])

    Eq << algebre.scalar.inequality.inequality.apply(Eq[-3], Eq[-1])
コード例 #26
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)

    given = Unequal(P(x), 0)

    Eq << apply(given, y)

    Eq << Eq[-1].lhs.bayes_theorem(x)

    Eq << Eq[-1].as_Or()

    Eq << (Eq[-1] & Eq[0]).split()
コード例 #27
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)

    Eq << apply(Unequal(P(x | y), 0))

    Eq << Eq[0].domain_definition()

    Eq << bayes.theorem.apply(Eq[-1], var=x)

    Eq << Eq[0] * Eq[2]

    Eq << Eq[-2].subs(Eq[-1])
コード例 #28
0
ファイル: joint_slice.py プロジェクト: cosmosZhou/sagemath
def prove(Eq):
    n = Symbol.n(integer=True, domain=[2, oo])
    x = Symbol.x(real=True, shape=(n, ), random=True)
    y = Symbol.y(real=True, shape=(n, ), random=True)
    t = Symbol.t(integer=True, domain=[1, n - 1])

    Eq << apply(Unequal(P(x, y), 0), Slice[:t, :t])

    Eq << Eq[0].this.lhs.arg.args[-1].bisect(Slice[:t])

    Eq << Eq[-1].this.lhs.arg.args[0].bisect(Slice[:t])

    Eq << bayes.inequality.et.apply(Eq[-1], wrt={x[:t], y[:t]}).split()
コード例 #29
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)
    z = Symbol.z(real=True, random=True)

    Eq << apply(Equality(x | y, x), Unequal(P(z | y), 0))

    Eq << P(x | y, z).bayes_theorem(z)
    Eq << Eq[-1].as_Or()

    Eq << (Eq[-1] & Eq[1]).split()

    Eq << Eq[-1].subs(Eq[0])

    Eq << bayes.inequality.inequality.marginal.apply(Eq[1])

    Eq << bayes.theorem.apply(Eq[-1], var=x)

    Eq << Eq[-3].subs(Eq[-1])

    Eq << algebre.scalar.inequality.equality.apply(Eq[-1], Eq[-3])

    Eq << Eq[-1].reversed
コード例 #30
0
def prove(Eq):
    x = Symbol.x(real=True, random=True)
    y = Symbol.y(real=True, random=True)
    
    given = Equality(P(x, y), P(x) * P(y))
    
    Eq << apply(given, Unequal(P(x), 0))
    
    Eq << Eq[-1].simplify()
    
    Eq << bayes.theorem.apply(Eq[1], var=y)
    
    Eq << Eq[-1].subs(Eq[0])
    
    Eq << Eq[-1] - Eq[-1].rhs
    
    Eq << Eq[-1].this.lhs.collect(P(x))
    
    Eq << Eq[-1].as_Or()
    
    Eq << (Eq[-1] & Eq[1]).split()
    
    Eq << Eq[-1].reversed