def truncated_gaussian():
    print(
        "\n\n------------------ Infer.NET Truncated Gaussian example ------------------\n"
    )
    # The model
    threshold = Variable.New[float]().Named("threshold")
    x = Variable.GaussianFromMeanAndVariance(0, 1).Named("x")
    Variable.ConstrainTrue(x.op_GreaterThan(x, threshold))

    # The inference, looping over different thresholds
    ie = InferenceEngine()
    threshold.ObservedValue = -0.1
    for i in range(0, 11):
        threshold.ObservedValue = threshold.ObservedValue + 0.1
        print "Dist over x given thresh of ", threshold.ObservedValue, "=", ie.Infer(
            x)
Beispiel #2
0
def two_coins():
    print(
        "\n\n------------------ Infer.NET Two Coins example ------------------\n"
    )

    # The model
    b = MicrosoftResearch.Infer.Distributions.Bernoulli(0.5)
    firstCoin = Variable.Bernoulli(0.5)
    secondCoin = Variable.Bernoulli(0.5)
    #bothHeads = firstCoin & secondCoin
    bothHeads = firstCoin.op_BitwiseAnd(firstCoin, secondCoin)

    # The inference
    ie = InferenceEngine()
    print "Probability both coins are heads:", ie.Infer(bothHeads)
    bothHeads.ObservedValue = False
    print "Probability distribution over firstCoin:", ie.Infer(firstCoin)
Beispiel #3
0
def clinical_trial():

    print(
        "\n\n------------------ Infer.NET Clinical Trial example ------------------\n"
    )

    controlGroup = Variable.Observed[Array[bool]](
        [False, False, True, False, False])
    treatedGroup = Variable.Observed[Array[bool]](
        [True, False, True, True, True])

    numControl = Variable.New[int]()
    numTreated = Variable.New[int]()

    i = Range(numControl)
    j = Range(numTreated)

    # Prior on being an effective treatment
    isEffective = Variable.Bernoulli(0.5).Named("isEffective")

    # If block
    v = Variable.If(isEffective)
    probIfControl = Variable.Beta(1, 1).Named("probIfControl")

    controlGroup = Variable.Bernoulli(probIfControl).ForEach(i)
    probIfTreated = Variable.Beta(1, 1).Named("probIfTreated")
    treatedGroup = Variable.Bernoulli(probIfTreated).ForEach(j)
    v.CloseBlock()

    # If Not block
    v = Variable.IfNot(isEffective)
    probAll = Variable.Beta(1, 1).Named("probAll")
    controlGroup = Variable.Bernoulli(probAll).ForEach(i)
    treatedGroup = Variable.Bernoulli(probAll).ForEach(j)
    v.CloseBlock()

    numControl.ObservedValue = 5  # controlGroup.Length
    numTreated.ObservedValue = 5  # treatedGroup.Length

    # The inference
    ie = InferenceEngine()
    print "Probability treatment has an effect = ", ie.Infer(isEffective)
    print "Probability of good outcome if given treatment = {}".format(
        ie.Infer[Beta](probIfTreated).GetMean())
    print "Probability of good outcome if control = {}".format(
        ie.Infer[Beta](probIfControl).GetMean())
# The data
incomes = System.Array[float]((63, 16, 28, 55, 22, 20 ))
ages = System.Array[float]((38, 23, 40, 27, 18, 40 ))
willBuy = System.Array[bool]((True, False, True, True, False, False))
dataLen = willBuy.Length
xdata = System.Array.CreateInstance(Vector, dataLen)
for i in range(0, dataLen):
    xdata[i] = Vector.FromArray(System.Array[float]((incomes[i], ages[i], 1.0)))

# Binding the data
x.ObservedValue = xdata
y.ObservedValue = willBuy
len.ObservedValue = dataLen

# Inferring the weights
ie = InferenceEngine()
ie.ShowFactorGraph = True
wPosterior = ie.Infer[VectorGaussian](w)
print "Dist over w=\n", wPosterior

# Prediction
incomesTest = System.Array[float]((58, 18, 22))
agesTest = System.Array[float]((36, 24, 37))
testDataLen = incomesTest.Length
xtestData = System.Array.CreateInstance(Vector, testDataLen)
for i in range(0, testDataLen):
    xtestData[i] = Vector.FromArray(System.Array[float]((incomesTest[i], agesTest[i], 1.0)))

jtest = Range(testDataLen).Named("j")
xtest = Variable.Observed[Vector](xtestData, jtest).Named("xtest")
wtest = Variable.Random[Vector](wPosterior).Named("wtest")
#-----------------------------------------------------------------------------------
# Infer.NET IronPython example: Truncated Gaussian with different thresholds
#-----------------------------------------------------------------------------------

import infernet
infernet.setup()

from MicrosoftResearch.Infer import Models, Distributions, InferenceEngine, ExpectationPropagation

print("\n\n------------------ Infer.NET Truncated Gaussian example ------------------\n");
# The model
threshold = Models.Variable.New[float]().Named("threshold")
x = Models.Variable.GaussianFromMeanAndVariance(0, 1).Named("x")
Models.Variable.ConstrainTrue(x > threshold)

# The inference, looping over different thresholds
ie = InferenceEngine()
ie.Algorithm = ExpectationPropagation()
threshold.ObservedValue = -0.1
for i in xrange(0, 11):
    threshold.ObservedValue = threshold.ObservedValue + 0.1
    print "Dist over x given thresh of ", threshold.ObservedValue, "=", ie.Infer(x)