Ejemplo n.º 1
0
  @Override
  public void nextSample() {
    WorldWithBlock curWorld = (WorldWithBlock) getBaseWorld();
    if (curWorld == null) {
      curWorld = new WorldWithBlock(model, evidence, intBound, depthBound);
      Util.debug("Creating initial possible world");
    }
    this.curWorld = curWorld;
    double logWeight = Double.NEGATIVE_INFINITY;
    latestSampleLogWeight = 0;
    while (!isCurWorldSufficient(curWorld)) {
      VarWithDistrib var = curWorld.getNextUninstVar();
      if (var != null) {
        if (var.canSample(curWorld)) {
          logWeight = sampleAndComputeLogWeight(var, curWorld);
        } else {
          curWorld.putBackVarWithDistrib(var);
        }
      } else {
        System.out.println(
            "World is not complete, but no basic random "
                + "variable is supported.  Please check for "
                + "a possible cycle in your model.");
      }
      latestSampleLogWeight += logWeight;
    }

    if (!evidence.isTrue(curWorld)) latestSampleLogWeight = Double.NEGATIVE_INFINITY;
    BLOGUtil.ensureDetAndSupported(queryVars, curWorld);

    // FIXME: remove duplication with LWSampler
    ++totalNumSamples;
    ++numSamplesThisTrial;
    if (latestSampleLogWeight > NEGLIGIBLE_LOG_WEIGHT) {
      ++totalNumConsistent;
      ++numConsistentThisTrial;
    }
    logSumWeightsThisTrial = Util.logSum(logSumWeightsThisTrial, latestSampleLogWeight);
  }
Ejemplo n.º 2
0
  private double sampleAndComputeLogWeight(VarWithDistrib var, WorldWithBlock curWorld) {
    DependencyModel.Distrib distrib =
        var.getDistrib(new BlockInstantiatingEvalContextImpl(curWorld));
    Util.debug("Instantiating: ", var);
    Type varType = var.getType();
    CondProbDistrib cpd = distrib.getCPD();
    cpd.setParams(distrib.getArgValues());
    Region r = curWorld.getSatisfyingRegion(var);

    double logWeight = Double.NEGATIVE_INFINITY;
    if (r.isEmpty()) return -1;
    Object value = null;
    if (r.isSingleton()) {
      value = r.getOneValue();
      logWeight = java.lang.Math.log(cpd.getProb(value));
    } else {
      do {
        value = cpd.sampleVal();
      } while (!r.contains(value));
      logWeight = java.lang.Math.log(computeCPD(cpd, r));
    }
    curWorld.setValue(var, value);
    return logWeight;
  }