public function __construct($mean, $variance, Variable $variable)
 {
     parent::__construct(sprintf("Prior value going to %s", $variable));
     $this->_newMessage = new GaussianDistribution($mean, sqrt($variance));
     $newMessage = new Message(GaussianDistribution::fromPrecisionMean(0, 0), sprintf("message from %s to %s", $this, $variable));
     $this->createVariableToMessageBindingWithMessage($variable, $newMessage);
 }
 public function __construct($betaSquared, Variable $variable1, Variable $variable2)
 {
     parent::__construct(sprintf("Likelihood of %s going to %s", $variable2, $variable1));
     $this->_precision = 1.0 / $betaSquared;
     $this->createVariableToMessageBinding($variable1);
     $this->createVariableToMessageBinding($variable2);
 }
Example #3
0
 public function __construct($epsilon, Variable &$variable)
 {
     parent::__construct(sprintf("%s <= %.2f", $variable, $epsilon));
     $this->_epsilon = $epsilon;
     $this->createVariableToMessageBinding($variable);
 }
 public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights = null)
 {
     parent::__construct(self::createName($sumVariable, $variablesToSum, $variableWeights));
     $this->_weights = array();
     $this->_weightsSquared = array();
     // The first weights are a straightforward copy
     // v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n
     $variableWeightsLength = count($variableWeights);
     $this->_weights[0] = \array_fill(0, count($variableWeights), 0);
     for ($i = 0; $i < $variableWeightsLength; $i++) {
         $weight = $variableWeights[$i];
         $this->_weights[0][$i] = $weight;
         $this->_weightsSquared[0][$i] = square($weight);
     }
     $variablesToSumLength = count($variablesToSum);
     // 0..n-1
     $this->_variableIndexOrdersForWeights[0] = array();
     for ($i = 0; $i < $variablesToSumLength + 1; $i++) {
         $this->_variableIndexOrdersForWeights[0][] = $i;
     }
     $variableWeightsLength = count($variableWeights);
     // The rest move the variables around and divide out the constant.
     // For example:
     // v_1 = (-a_2 / a_1) * v_2 + (-a3/a1) * v_3 + ... + (1.0 / a_1) * v_0
     // By convention, we'll put the v_0 term at the end
     $weightsLength = $variableWeightsLength + 1;
     for ($weightsIndex = 1; $weightsIndex < $weightsLength; $weightsIndex++) {
         $currentWeights = \array_fill(0, $variableWeightsLength, 0);
         $variableIndices = \array_fill(0, $variableWeightsLength + 1, 0);
         $variableIndices[0] = $weightsIndex;
         $currentWeightsSquared = \array_fill(0, $variableWeightsLength, 0);
         // keep a single variable to keep track of where we are in the array.
         // This is helpful since we skip over one of the spots
         $currentDestinationWeightIndex = 0;
         for ($currentWeightSourceIndex = 0; $currentWeightSourceIndex < $variableWeightsLength; $currentWeightSourceIndex++) {
             if ($currentWeightSourceIndex == $weightsIndex - 1) {
                 continue;
             }
             $currentWeight = -$variableWeights[$currentWeightSourceIndex] / $variableWeights[$weightsIndex - 1];
             if ($variableWeights[$weightsIndex - 1] == 0) {
                 // HACK: Getting around division by zero
                 $currentWeight = 0;
             }
             $currentWeights[$currentDestinationWeightIndex] = $currentWeight;
             $currentWeightsSquared[$currentDestinationWeightIndex] = $currentWeight * $currentWeight;
             $variableIndices[$currentDestinationWeightIndex + 1] = $currentWeightSourceIndex + 1;
             $currentDestinationWeightIndex++;
         }
         // And the final one
         $finalWeight = 1.0 / $variableWeights[$weightsIndex - 1];
         if ($variableWeights[$weightsIndex - 1] == 0) {
             // HACK: Getting around division by zero
             $finalWeight = 0;
         }
         $currentWeights[$currentDestinationWeightIndex] = $finalWeight;
         $currentWeightsSquared[$currentDestinationWeightIndex] = square($finalWeight);
         $variableIndices[count($variableWeights)] = 0;
         $this->_variableIndexOrdersForWeights[] = $variableIndices;
         $this->_weights[$weightsIndex] = $currentWeights;
         $this->_weightsSquared[$weightsIndex] = $currentWeightsSquared;
     }
     $this->createVariableToMessageBinding($sumVariable);
     foreach ($variablesToSum as $currentVariable) {
         $localCurrentVariable = $currentVariable;
         $this->createVariableToMessageBinding($localCurrentVariable);
     }
 }