/**
 * Copyright (c) 2011 Michael Kutschke.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Michael Kutschke - initial API and implementation.
 */
package bayesnet.jayes.inference;

import java.util.Map;
import java.util.Map.Entry;

import bayesnet.jayes.BayesNet;
import bayesnet.jayes.BayesNode;
import bayesnet.jayes.sampling.BasicSampler;
import bayesnet.jayes.util.BayesNodeUtil;
import bayesnet.jayes.util.MathUtils;

@SuppressWarnings("deprecation")
public class LikelihoodWeightedSampling extends AbstractInferer {

    private static final int DEFAULT_SAMPLE_COUNT = 200;

    private int sampleCount = DEFAULT_SAMPLE_COUNT;
    private final BasicSampler sampler = new BasicSampler();

    @Override
    public void setNetwork(BayesNet bn) {
        super.setNetwork(bn);
        sampler.setNetwork(bn);
    }

    @Override
    protected void updateBeliefs() {
        sampler.setEvidence(evidence);
        for (int i = 0; i < sampleCount; i++) {
            Map sample = sampler.sample();
            double weight = computeEvidenceProbability(sample);

            for (Entry e : sample.entrySet()) {
                BayesNode n = e.getKey();
                beliefs[n.getId()][n.getOutcomeIndex(e.getValue())] += weight;
            }
        }

        normalizeBeliefs();
    }

    private void normalizeBeliefs() {
        for (int i = 0; i < beliefs.length; i++) {
            beliefs[i] = MathUtils.normalize(beliefs[i]);
        }
    }

    private double computeEvidenceProbability(Map sample) {
        double factor = 1.0;
        for (Entry entry : evidence.entrySet()) {
            BayesNode n = entry.getKey();
            factor *= BayesNodeUtil.getSubCpt(n, sample)[n.getOutcomeIndex(entry.getValue())];
        }
        return factor;
    }

    public void setSampleCount(int sampleCount) {
        this.sampleCount = sampleCount;
    }

    public void seed(long seed) {
        sampler.seed(seed);
    }
}

 

Ads help maintain this website.