<commit_before>package ml.wolfe

import ml.wolfe.fg.{CantProposeException, ContinuousVar, DiscreteVar, Var}

import scala.math._
import scalaxy.loops._
import scala.util.Random

/**
 * Created by luke on 22/08/14.
 */
object GibbsSampling {

  def apply(fg:FactorGraph, iterations:Int = 10000) = {
    fg.expectations = new SparseVector(10000)

    val unobservedNodes = fg.nodes.filterNot(_.variable.isObserved)

    for(i <- (0 until iterations).optimized) {
      //println( (for(n <- fg.nodes if !n.variable.isObserved) yield n.variable.label + ": " + n.variable.value).mkString("    ") + "\n")
      val n = unobservedNodes(Random.nextInt(unobservedNodes.length))
      sample(n.variable)
      for (f <- fg.expectationFactors) {
        fg.expectations += f.potential.statsForCurrentSetting()
      }
    }

    fg.expectations /= iterations
  }

  def sample(v:Var[_]):Unit = {
    def expScore = {
      val foo = v.node.edges.map(e => exp(e.f.potential.valueForCurrentSetting()))
      v.node.edges.map(e => exp(e.f.potential.valueForCurrentSetting())).sum
    }

    val oldSetting = v.setting
    val oldScore = expScore

    var sampled = false

    val sampledFromEdge = v.node.edges.sortBy(_ => Math.random()).find { e =>
      try {
        e.f.potential.proposeSetting(e)
        true
      } catch {
        case e:CantProposeException => false
      }
    }

    sampledFromEdge match {
      case None =>
        println("Could not propose for " + v.label + "; Invoking sampleUniform")
        sampleUniform(v)
      case Some(e) =>
    }
    val newScore = expScore

    if(newScore - oldScore < Math.log(Math.random())) v.setting = oldSetting

  }


  def sampleUniform(v:Var[_]): Unit = v match {
    case v:DiscreteVar[_] => v.setting = Random.nextInt(v.dim)
    case v:ContinuousVar => v.setting = 42
  }

}<commit_msg>Score fix for GibbsSampling<commit_after>package ml.wolfe

import ml.wolfe.fg.{CantProposeException, ContinuousVar, DiscreteVar, Var}

import scala.math._
import scalaxy.loops._
import scala.util.Random

/**
 * Created by luke on 22/08/14.
 */
object GibbsSampling {

  def apply(fg:FactorGraph, iterations:Int = 10000) = {
    fg.expectations = new SparseVector(10000)

    val unobservedNodes = fg.nodes.filterNot(_.variable.isObserved)

    for(i <- (0 until iterations).optimized) {
      //println( (for(n <- fg.nodes if !n.variable.isObserved) yield n.variable.label + ": " + n.variable.value).mkString("    ") + "\n")
      val n = unobservedNodes(Random.nextInt(unobservedNodes.length))
      sample(n.variable)
      for (f <- fg.expectationFactors) {
        fg.expectations += f.potential.statsForCurrentSetting()
      }
    }

    fg.expectations /= iterations
  }

  def sample(v:Var[_]):Unit = {
    def score = v.node.edges.map(e => e.f.potential.valueForCurrentSetting()).sum

    val oldSetting = v.setting
    val oldScore = score

    var sampled = false

    val sampledFromEdge = v.node.edges.sortBy(_ => Math.random()).find { e =>
      try {
        e.f.potential.proposeSetting(e)
        true
      } catch {
        case e:CantProposeException => false
      }
    }

    sampledFromEdge match {
      case None =>
        println("Could not propose for " + v.label + "; Invoking sampleUniform")
        sampleUniform(v)
      case Some(e) =>
    }
    val newScore = score

    if(exp(newScore - oldScore) < Math.random()) v.setting = oldSetting

  }


  def sampleUniform(v:Var[_]): Unit = v match {
    case v:DiscreteVar[_] => v.setting = Random.nextInt(v.dim)
    case v:ContinuousVar => v.setting = 42
  }

}
{'fraction_non_alphanumeric': 0.13337053571428573, 'fraction_numerical': 0.010602678571428572, 'mean_word_length': 2.7776606954689145, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7483503', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1163, 'n_words': 239}