【问题标题】:Scala Breeze Dirichlet distribution parameter estimationScala Breeze Dirichlet 分布参数估计
【发布时间】:2015-11-10 16:17:04
【问题描述】:

我正在尝试使用 Scala 的微风库估计数据集的参数(狄利克雷分布)。我已经有一个工作 python (pandas/dataframes) 和它的 R 代码,但我很好奇如何在 Scala 中做到这一点。我也是 Scala 的新手。

我似乎无法让它工作。我想在语法上我没有正确的东西。

我尝试使用的代码在这里:https://github.com/scalanlp/breeze/blob/master/math/src/main/scala/breeze/stats/distributions/Dirichlet.scala#L111

根据上面的代码:ExpFam[T,I]接受两个参数T和I,我不知道T和I是什么。 T 可以是密集矩阵吗?

我正在做的是:

# Creating a matrix. The values are counts in my case.
val mat = DenseMatrix((1.0, 2.0, 3.0),(4.0, 5.0, 6.0))

# Then try to get sufficient stats and then MLE. I think this where I doing something wrong.
val diri = new ExpFam[DenseMatrix[Double],Int](mat)
println(diri.sufficientStatisticFor(mat))

另外,如果有一个像这样的数据矩阵 DenseMatrix((1.0, 2.0, 3.0),(4.0, 5.0, 6.0)) 如何在 Scala 中估计参数(Dirichlet)。

【问题讨论】:

    标签: scala scala-breeze mle dirichlet


    【解决方案1】:

    我对微风的这方面不是很熟悉,但这对我有用:

    val data = Seq(
      DenseVector(0.1, 0.1, 0.8),
      DenseVector(0.2, 0.3, 0.5),
      DenseVector(0.5, 0.1, 0.4),
      DenseVector(0.3, 0.3, 0.4)
    )
    
    val expFam = new Dirichlet.ExpFam(DenseVector.zeros[Double](3))
    
    val suffStat = data.foldLeft(expFam.emptySufficientStatistic){(a, x) => 
      a + expFam.sufficientStatisticFor(x)
    }
    
    val alphaHat = expFam.mle(suffStat)
    //DenseVector(2.9803000577558274, 2.325871404559782, 5.850530402841005)
    

    结果与我使用自己的 Dirichlets 最大似然估计代码得到的结果非常接近,但并不完全相同。差异可能只是归结为正在使用的优化器的差异(我在 T. Minka 的 paper 的第 1 节中使用定点迭代 (9))和停止标准。

    也许有更好的方法来使用微风 api;如果是这样,希望@dlwh 或其他更熟悉微风的人会加入。

    【讨论】:

      【解决方案2】:

      T 应该是 DenseVector 而 I 应该是 Int。 ExpFams 目前没有向量化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-18
        • 2012-05-26
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 2022-10-15
        • 2015-10-07
        • 1970-01-01
        相关资源
        最近更新 更多