使用Java实现贝叶斯推理

时间:2011-03-25 00:53:36

标签: java bayesian-networks inference

this in php这样的java中是否有任何实现?

<?php

/**
* Returns conditional probability of $A given $B and $Data.
* $Data is an indexed array.  Each element of the $Data array 
* consists of an A measurement and B measurment on a sample 
* item.
*/
function getConditionalProbabilty($A, $B, $Data) {
  $NumAB   = 0;
  $NumB    = 0;
  $NumData = count($Data);
  for ($i=0; $i < $NumData; $i++) {
    if (in_array($B, $Data[$i])) {
      $NumB++;
      if (in_array($A, $Data[$i])) {
        $NumAB++;
      }
    }
  }
  return $NumAB / $NumB;
}

?>


<?php

require "getConditionalProbability.php";

/**
* The elements of the $Data array use this coding convention:
*
* +cancer - patient has cancer
* -cancer - patient does not have cancer
* +test   - patient tested positive on cancer test
* -test   - patient tested negative on cancer test
*/

$Data[0] = array("+cancer", "+test");
$Data[1] = array("-cancer", "-test");
$Data[2] = array("+cancer", "+test");
$Data[3] = array("-cancer", "+test");

// specify query variable $A and conditioning variable $B
$A = "+cancer";
$B = "+test";

// compute the conditional probability of having cancer given 1) 
// a positive test and 2) a sample of covariation data
$probability = getConditionalProbabilty($A, $B, $Data);

echo "P($A|$B) = $probability";

// P(+cancer|+test) = 0.66666666666667

?>

2 个答案:

答案 0 :(得分:0)

AFAIK标准JDK中没有它虽然它是一个非常简单的函数,你可以自己编写它,Java版本几乎就是一个直接的翻译。虽然有一些来自apache的this第三方库,如果你打算做很多这样的事情,它会处理统计和数学函数。

答案 1 :(得分:0)

您可以查看CI Bayes。这是Naive的一个实现,它将超越上述范围,让你能够做一个Fisher分类器(Naive Bayes的变种)。它有其局限性,但将它添加到项目并开始运行非常容易。