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)
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)
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())