In [1]:
classpath.addPath("../jsci-1.2.jar")
classpath.addPath("../figaro_2.11-4.1.0.0.jar")
In [2]:
import com.cra.figaro.language._
import com.cra.figaro.algorithm.factored.VariableElimination
In [3]:
// Flip 伯努利分布
val sunnyToday = Flip(0.2)
println(VariableElimination.probability(sunnyToday, true))
println(VariableElimination.probability(sunnyToday, false))
In [4]:
// Select 离散分布
val greeting = Select(0.6 -> "Hello, world!", 0.3 -> "Howdy, universe!", 0.1 -> "Oh no, not again")
println(VariableElimination.probability(greeting, "Howdy, universe!"))
In [5]:
// Binomial 二项分布
import com.cra.figaro.library.atomic.discrete.Binomial
val numSunnyDaysInWeek = Binomial(7, 0.2)
In [6]:
println(VariableElimination.probability(numSunnyDaysInWeek, 1))
//java.lang.NoClassDefFoundError: JSci/maths/ExtraMath
In [7]:
// 正态分布
import com.cra.figaro.library.atomic.continuous.Normal
val temperature = Normal(40, 100)
In [8]:
import com.cra.figaro.algorithm.sampling.Importance
def greaterThan50(d: Double) = d > 50
println(Importance.probability(temperature, greaterThan50 _))
In [9]:
// 均匀分布
import com.cra.figaro.library.atomic.continuous.Uniform
val temperature2 = Uniform(10, 70)
In [10]:
Importance.probability(temperature2, greaterThan50 _)
In [11]:
// If随机过程,首先检查测试结果,如果测试值是true则生成then子句的值,否则生成else子句的值
import com.cra.figaro.library.compound.If
val sunnyToday = Flip(0.2)
val greetingToday = If(sunnyToday,
Select(0.6 -> "Hello, world!", 0.4 -> "Howdy, universe!"),
Select(0.2 -> "Hello, world!", 0.8 -> "Oh no, not again"))
In [12]:
println(VariableElimination.probability(greetingToday, "Hello, world!"))
In [13]:
sunnyToday.observe(true)
println(VariableElimination.probability(greetingToday, "Hello, world!"))
In [14]:
sunnyToday.observe(false)
println(VariableElimination.probability(greetingToday, "Hello, world!"))
In [15]:
// Dist, 它选择一组元素中的一个而不是选择一组值中的一个
val goodMood = Dist(0.2 -> Flip(0.6), 0.8 -> Flip(0.2))
In [16]:
// print 0.28 = 0.2*0.6 + 0.8*0.2
println(VariableElimination.probability(goodMood, true))
In [17]:
// 晴天的概率不确定
val sunnyTodayProbability = Uniform(0, 0.5)
val sunnyToday = Flip(sunnyTodayProbability)
println(Importance.probability(sunnyToday, true))
In [18]:
// 均值和方差都不确定
val tempMean = Normal(40, 9)
val tempVariance = Select(0.5 -> 80.0, 0.5 -> 105.0)
val temperature = Normal(tempMean, tempVariance)
println(Importance.probability(temperature, (d: Double) => d > 50))
In [19]:
val sunnyDaysInMonth = Binomial(30, 0.2)
def getQuality(i: Int): String =
if(i > 10) "good"; else if (i > 5) "average"; else "poor"
val monthQuality = Apply(sunnyDaysInMonth, getQuality)
In [20]:
println(VariableElimination.probability(monthQuality, "good"))
Apply元素定义随机过程,它首先生成第一个元素参数的值,然后,该过程取得第二个函数参数,并将其应用到生成的值。
In [21]:
val goodMood = Chain(monthQuality, (s: String) =>
if(s == "good") Flip(0.9)
else if (s == "average") Flip(0.6)
else Flip(0.1))
In [22]:
println(VariableElimination.probability(goodMood, true))
In [23]:
greetingToday.observe("Hello, world!")
In [24]:
println(VariableElimination.probability(sunnyToday, true))
In [25]:
greetingToday.unobserve()
In [26]:
println(VariableElimination.probability(sunnyToday, true))
val sunnyDaysInMonth = Binomial(30, 0.2)
def getQuality(i: Int): String =
if(i > 10) "good"; else if (i > 5) "average"; else "poor"
val monthQuality = Apply(sunnyDaysInMonth, getQuality)
val goodMood = Chain(monthQuality, (s: String) =>
if(s == "good") Flip(0.9)
else if (s == "average") Flip(0.6)
else Flip(0.1))
In [27]:
println(VariableElimination.probability(goodMood, true))
In [30]:
// 规定sunnyDaysInMonth必须大于8,好心情的概率明显上升
sunnyDaysInMonth.setCondition((i: Int) => i > 8)
println(VariableElimination.probability(goodMood, true))
In [31]:
// sunnyDaysInMonth必须比3的倍数大2,排除9和10等等情况的可能
sunnyDaysInMonth.addCondition((i: Int) => i % 3 == 2)
println(VariableElimination.probability(goodMood, true))
In [32]:
sunnyDaysInMonth.removeConditions()
println(VariableElimination.probability(goodMood, true))
In [33]:
// 表示为goodMood为true的可能性只是false的一半
goodMood.addConstraint((b: Boolean) => if(b) 0.5; else 1.0)
In [34]:
println(VariableElimination.probability(goodMood, true))
作为连接元素的约束
In [35]:
// 定义三场比赛的结果
val result1 = Flip(0.4)
val result2 = Flip(0.4)
val result3 = Flip(0.4)
In [36]:
val allWins = Apply(result1, result2, result3,
(w1: Boolean, w2: Boolean, w3: Boolean) => w1 && w2 && w3)
In [37]:
println(VariableElimination.probability(allWins, true))
In [38]:
def makeStreaky(r1: Element[Boolean], r2: Element[Boolean]) {
val pair = Apply(r1, r2, (b1: Boolean, b2: Boolean) => (b1, b2))
pair.setConstraint((bb: (Boolean, Boolean)) =>
if(bb._1 == bb._2) 1.0; else 0.5)
}
In [39]:
// 由于描述了相邻比赛的连续性,使得比赛全胜的概率增加
makeStreaky(result1, result2)
makeStreaky(result2, result3)
println(VariableElimination.probability(allWins, true))