博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
概率编程——Figaro的基础示例
阅读量:2253 次
发布时间:2019-05-09

本文共 4276 字,大约阅读时间需要 14 分钟。

文章目录

概率编程——Figaro的基础示例

0. 导包

import com.cra.figaro.algorithm.factored.VariableEliminationimport com.cra.figaro.algorithm.sampling.Importanceimport com.cra.figaro.language._import com.cra.figaro.library.atomic.continuous.{
Normal, Uniform}import com.cra.figaro.library.atomic.discrete.Binomialimport com.cra.figaro.library.compound.If

1. 原子元素

  • 离散型
    • Flip
    val sunnyToday = Flip(0.2)println(VariableElimination.probability(sunnyToday, true))
    • Select
    val language = Select(  0.5 -> "Java",  0.3 -> "Scala",  0.2 -> "Python")println(VariableElimination.probability(language, "Scala"))
    • Binomial
    val numSunnyDayInWeek = Binomial(7, 0.5)println(VariableElimination.probability(numSunnyDayInWeek, 3))
  • 连续型
    • Normal 正态分布
    val temperature = Normal(40, 100)val greaterThan50 = (d: Double) => d > 50println(Importance.probability(temperature, greaterThan50))
    • Uniform 连续均匀分布
    val temperature2 = Uniform(10, 81)println(Importance.probability(temperature2, greaterThan50))

2. 复合元素

  • 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!"))println(VariableElimination.probability(greetingToday, "Hello World!"))sunnyToday.observe(true)println(VariableElimination.probability(greetingToday, "Hello World!"))
  • Dist
    val goodMood = Dist(  0.2 -> Flip(0.6),  0.8 -> Flip(0.2))println(VariableElimination.probability(goodMood, true))
  • 原子复合
    val sunnyTodayProbability = Uniform(0, 0.6)val sunnyToday2 = Flip(sunnyTodayProbability)println(Importance.probability(sunnyToday2, true))val tempMean = Normal(40, 36)val temperature = Normal(tempMean, 100)println(Importance.probability(temperature, (d: Double) => d > 50))
  • Apply
    val sunnyDaysInMonth = Binomial(30, 0.2)val getQuality = (i: Int) => if (i > 10) "good" else if (i > 5) "average" else "poor"val monthQuality = Apply(sunnyDaysInMonth, getQuality)println(VariableElimination.probability(monthQuality, "good"))val teamWinsInMonth = Binomial(5, 0.4)val monthQuality2 = Apply(sunnyDaysInMonth, teamWinsInMonth,  (days: Int, wins: Int) => {
    val x = days * wins if (x > 20) "good" else if (x > 10) "average" else "poor" })println(VariableElimination.probability(monthQuality2, "good"))
  • Chain
    val goodMood2 = Chain(monthQuality,  (s: String) =>    if (s == "good") Flip(0.9)    else if (s == "average") Flip(0.6)    else Flip(0.1))println(VariableElimination.probability(goodMood2, true))val sunnyToday3 = Flip(0.2)val goodMood3 = Chain(monthQuality, sunnyToday3,  (quality: String, sunny: Boolean) => {
    (quality, sunny) match {
    case ("good", true) => Flip(0.9) case ("average", true) => Flip(0.7) case (_, true) => Flip(0.4) case ("good", false) => Flip(0.6) case ("average", false) => Flip(0.3) case (_, false) => Flip(0.05) } })println(VariableElimination.probability(goodMood3, true))

3. 条件与约束

  • 条件
    val sunntDayInMonth = Binomial(30, 0.2)val monthQuality = Apply(sunntDayInMonth,  (i: Int) => if (i > 10) "good" else if (i > 5) "average" else "poor")val goodMood = Chain(monthQuality,  (s: String) => {
    if (s == "good") Flip(0.9) else if (s == "average") Flip(0.6) else Flip(0.1) })println(VariableElimination.probability(goodMood, true))// 设置条件,会清楚之前所有的条件sunntDayInMonth.setCondition((i: Int) => i > 8)println(VariableElimination.probability(goodMood, true))// 添加条件sunntDayInMonth.addCondition((i: Int) => i % 3 == 2)println(VariableElimination.probability(goodMood, true))// 移除条件sunntDayInMonth.removeConditions()println(VariableElimination.probability(goodMood, true))
  • 约束
    goodMood.setConstraint((b: Boolean) => if (b) 0.5 else 1.0)println(VariableElimination.probability(goodMood, true))val result1 = Flip(0.4)val result2 = Flip(0.4)val result3 = Flip(0.4)val allWins = Apply(result1, result2, result3,  (w1: Boolean, w2: Boolean, w3: Boolean) => w1 && w2 && w3)println(VariableElimination.probability(allWins, true))def makeStreaky(r1: Element[Boolean], r2: Element[Boolean]): Unit = {
    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)}makeStreaky(result1, result2)makeStreaky(result2, result3)println(VariableElimination.probability(allWins, true))

转载地址:http://bwqdb.baihongyu.com/

你可能感兴趣的文章
二、转换成3NF的保持函数依赖的分解
查看>>
三、转换成3NF的保持无损连接和函数依赖的分解
查看>>
四、转换成BCNF的保持无损连接的分解
查看>>
用Rational_Rose来建立数据库表
查看>>
配置文件模板
查看>>
android EditText 全面阐述
查看>>
C#项目打包,并自动安装SQL数据库
查看>>
数据库日常维护常用的脚本部分收录
查看>>
数据库备份九点详解
查看>>
经典S Q L语句大全
查看>>
聚簇索引与非聚簇索引的区别以及SQL Server查询优化技术
查看>>
聚焦索引与非聚焦索引的适用范围
查看>>
SQL2005自动定时备份数据库并按日期命名
查看>>
分享 SQL Server 2005 分区实现教程
查看>>
海量数据查询优化(转)
查看>>
三十而立,半夜醒来,无法入眠,掩面而泣,心痛处,泪流满面!
查看>>
Oracle学习笔记之三字符型及处理方法
查看>>
Android中SurfaceView和View的区别
查看>>
Android中由文件名获取文件Id的两种方法
查看>>
Android推送方式比较
查看>>