1

我尝试从 [PPP] Avi Pfeffer 解决 8.5.4。实用概率编程。2016 年

我必须使用 DynamicBayesianNetwork 来创建一个简单的公司经济模型。为此,我需要三个变量:投资、利润和资本。我的 figaro 程序显示了一些关于类型不匹配的错误

found   : Int
[error]  required: com.cra.figaro.language.Element[?]
[error]  val newInvestment = Apply(Flip(0.6), capital, (b : Boolean, c:Int) => if(b) Constant(0.5 * c) else Constant(0.3 * c))

这是我的代码:

import com.cra.figaro.language._
import com.cra.figaro.library.atomic.continuous.Normal
import com.cra.figaro.library.atomic.discrete.{FromRange, Poisson}
import com.cra.figaro.library.compound._
import com.cra.figaro.algorithm.filtering.ParticleFilter

object DynamicBayesianNetwork {

val initial = Universe.createNew()
Constant(200)("capital", initial)
Constant(500)("investment", initial)
Constant(50)("profit", initial)


def transition(capital: Int, profit: Int, investment: Int): (Element[(Int, Int, Int)]) = {

val newInvestment = Apply(Flip(0.6), capital, (b : Boolean, c:Int) => if(b) Constant(0.5 * c) 
                          else  Constant(0.3 * c))
val newProfit = Apply(investment, newInvestment, (i : Int, nI: Int) => if(i < nI) 
                      Constant(profit +0.5*profit) else Constant(profit - 0.5*profit))
val newCapital = capital + newProfit - newInvestment
^^(newInvestment, newProfit, newCapital)
}

def nextUniverse(previous: Universe): Universe = {
 val next = Universe.createNew()
 val previousInvestment = previous.get[Int]("investment")
 val previousCapital = previous.get[Int]("capital")
 val previousProfit = previous.get[Int]("profitl")
 val newState = Chain(previousCapital, previousProfit, previousInvestment, transition _)
 Apply(newState, (s: (Int, Int, Int)) => s._1)("investment", next)
 Apply(newState, (s: (Int, Int, Int)) => s._2)("profit", next)
 Apply(newState, (s: (Int, Int, Int)) => s._3)("capital", next)
 next
}

def main(args: Array[String]) {
 val capitalObservation = List(200, None, None, None, None, None, None,None, None, None)
 val alg = ParticleFilter(initial, nextUniverse, 10000)
 alg.start()
 for { time <- 1 to 10 } {
   val evidence = {
      capitalObservation(time) match {
      case None => List()
      case Some(n) => List(NamedEvidence("capital", Observation(n)))
    }
  }
  alg.advanceTime(evidence)
  print("Time " + time + ": ")
  print("capital = " + alg.currentExpectation("capital", (c: Int) => c))
  println(", investment = " + alg.currentExpectation("investment", (i: Int) => i))
  println(", profit = " + alg.currentExpectation("profit", (p: Int) => p))
  }
 }

}
4

1 回答 1

0

尝试

def transition(capital: Int, profit: Int, investment: Int): (Element[(Double, Double, Double)]) = {

  val newInvestment = Apply(Flip(0.6), Constant(capital), (b : Boolean, c:Int) => if(b) 0.5 * c
  else  0.3 * c)
  val newProfit = Apply(Constant(investment), newInvestment, (i : Int, nI: Double) => if(i < nI)
    profit +0.5*profit else profit - 0.5*profit)
  val newCapital = for {
    np <- newProfit
    ni <- newInvestment
  } yield capital + np - ni
  ^^(newCapital, newProfit, newInvestment)
}

def nextUniverse(previous: Universe): Universe = {
  val next = Universe.createNew()
  val previousInvestment = previous.get[Int]("investment")
  val previousCapital = previous.get[Int]("capital")
  val previousProfit = previous.get[Int]("profitl")
  val newState = Chain(^^(previousCapital, previousProfit, previousInvestment), (transition _).tupled)
  Apply(newState, (s: (Double, Double, Double)) => s._1)("investment", next)
  Apply(newState, (s: (Double, Double, Double)) => s._2)("profit", next)
  Apply(newState, (s: (Double, Double, Double)) => s._3)("capital", next)
  next
}
于 2019-12-14T03:53:34.453 回答