Inheritance: AIMA.Probability.CategoricalDistribution
コード例 #1
0
        public ProbabilityTable pointwiseProductPOS(
            ProbabilityTable multiplier, params RandomVariable[] prodVarOrder)
        {
            ProbabilityTable product = new ProbabilityTable(prodVarOrder);

            if (!product.randomVarInfo.keySet().Equals(
                    SetOps.union(new List <RandomVariable>(randomVarInfo.keySet()),
                                 new List <RandomVariable>(multiplier.randomVarInfo
                                                           .keySet()))))
            {
                if (1 == product.getValues().Length)
                {
                    product.getValues()[0] = getValues()[0] * multiplier.getValues()[0];
                }
                else
                {
                    // Otherwise need to iterate through the product
                    // to calculate its values based on the terms.
                    Object[] term1Values = new Object[randomVarInfo.size()];
                    Object[] term2Values = new Object[multiplier.randomVarInfo
                                                      .size()];
                    //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
                    //    private int idx = 0;

                    //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                    //            double probability) {
                    //        int term1Idx = termIdx(term1Values, ProbabilityTable.this,
                    //                possibleWorld);
                    //        int term2Idx = termIdx(term2Values, multiplier,
                    //                possibleWorld);

                    //        product.getValues()[idx] = getValues()[term1Idx]
                    //                * multiplier.getValues()[term2Idx];

                    //        idx++;
                    //    }

                    //    private int termIdx(Object[] termValues, ProbabilityTable d,
                    //            Map<RandomVariable, Object> possibleWorld) {
                    //        if (0 == termValues.Length) {
                    //            // The term has no variables so always position 0.
                    //            return 0;
                    //        }

                    //        int i = 0;
                    //        for (RandomVariable rv : d.randomVarInfo.keySet()) {
                    //            termValues[i] = possibleWorld.get(rv);
                    //            i++;
                    //        }

                    //        return d.getIndex(termValues);
                    //    }
                    //};
                    //product.iterateOverTable(di);
                    // TODO
                }
            }
            return(product);
        }
コード例 #2
0
        public ProbabilityTable pointwiseProduct(ProbabilityTable multiplier)
        {
            List <RandomVariable> prodVars = SetOps.union(new List <RandomVariable>(randomVarInfo.Keys),
                                                          new List <RandomVariable>(multiplier.randomVarInfo.Keys));

            return(pointwiseProductPOS(multiplier, prodVars
                                       .ToArray()));
        }
コード例 #3
0
        public FullJointDistributionModel(double[] values, params RandomVariable[] vars)
        {
            if (null == vars)
            {
                throw new ArgumentException(
                    "Random Variables describing the model's representation of the World need to be specified.");
            }

            distribution = new ProbabilityTable(values, vars);

            representation = new Set<RandomVariable>();
            for (int i = 0; i < vars.Length; i++)
            {
                representation.add(vars[i]);
            }
            //representation = Collections.unmodifiableSet(representation);
        }
コード例 #4
0
ファイル: CPT.cs プロジェクト: PaulMineau/AIMA.Net
	public CPT(RandomVariable on, double[] values,
			params RandomVariable [] conditionedOn) {
		this.on = on;
		if (null == conditionedOn) {
			conditionedOn = new RandomVariable[0];
		}
		RandomVariable[] tableVars = new RandomVariable[conditionedOn.Length + 1];
		for (int i = 0; i < conditionedOn.Length; i++) {
			tableVars[i] = conditionedOn[i];
			parents.add(conditionedOn[i]);
		}
		tableVars[conditionedOn.Length] = on;
		table = new ProbabilityTable(values, tableVars);
		onDomain.AddRange(((FiniteDomain) on.getDomain()).getPossibleValues());

		checkEachRowTotalsOne();
	}
コード例 #5
0
        public ProbabilityTable sumOut(params RandomVariable[] vars)
        {
            Set <RandomVariable> soutVars = new Set <RandomVariable>(
                this.randomVarInfo.keySet());

            foreach (RandomVariable rv in vars)
            {
                soutVars.remove(rv);
            }
            ProbabilityTable summedOut = new ProbabilityTable(soutVars);

            if (1 == summedOut.getValues().Length)
            {
                summedOut.getValues()[0] = getSum();
            }
            else
            {
                // Otherwise need to iterate through this distribution
                // to calculate the summed out distribution.
                Object[] termValues = new Object[summedOut.randomVarInfo
                                                 .size()];
                //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
                //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                //            double probability) {

                //        int i = 0;
                //        foreach (RandomVariable rv in summedOut.randomVarInfo.keySet()) {
                //            termValues[i] = possibleWorld.get(rv);
                //            i++;
                //        }
                //        summedOut.getValues()[summedOut.getIndex(termValues)] += probability;
                //    }
                //};
                //iterateOverTable(di);
                //TODO:
            }

            return(summedOut);
        }
コード例 #6
0
        public ProbabilityTable divideBy(ProbabilityTable divisor)
        {
            if (!randomVarInfo.keySet().containsAll(divisor.randomVarInfo.keySet()))
            {
                throw new IllegalArgumentException(
                          "Divisor must be a subset of the dividend.");
            }

            ProbabilityTable quotient = new ProbabilityTable(new List <RandomVariable>(randomVarInfo.Keys));

            if (1 == divisor.getValues().Length)
            {
                double d = divisor.getValues()[0];
                for (int i = 0; i < quotient.getValues().Length; i++)
                {
                    if (0 == d)
                    {
                        quotient.getValues()[i] = 0;
                    }
                    else
                    {
                        quotient.getValues()[i] = getValues()[i] / d;
                    }
                }
            }
            else
            {
                Set <RandomVariable> dividendDivisorDiff = SetOps
                                                           .difference(new List <RVInfo>(randomVarInfo.keySet()),
                                                                       new List <RVInfo>(randomVarInfo.keySet()));
                Map <RandomVariable, RVInfo> tdiff = null;
                MixedRadixNumber             tdMRN = null;
                if (dividendDivisorDiff.size() > 0)
                {
                    tdiff = new LinkedHashMap <RandomVariable, RVInfo>();
                    foreach (RandomVariable rv in dividendDivisorDiff)
                    {
                        tdiff.put(rv, new RVInfo(rv));
                    }
                    tdMRN = new MixedRadixNumber(0, createRadixs(tdiff));
                }
                Map <RandomVariable, RVInfo> diff = tdiff;
                MixedRadixNumber             dMRN = tdMRN;
                int[]            qRVs             = new int[quotient.radices.Length];
                MixedRadixNumber qMRN             = new MixedRadixNumber(0,
                                                                         quotient.radices);
                //ProbabilityTable.Iterator divisorIterator = new ProbabilityTable.Iterator() {
                //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                //            double probability) {
                //        foreach (RandomVariable rv in possibleWorld.keySet()) {
                //            RVInfo rvInfo = quotient.randomVarInfo.get(rv);
                //            qRVs[rvInfo.getRadixIdx()] = rvInfo
                //                    .getIdxForDomain(possibleWorld.get(rv));
                //        }
                //        if (null != diff) {
                //            // Start from 0 off the diff
                //            dMRN.setCurrentValueFor(new int[diff.size()]);
                //            do {
                //                for (RandomVariable rv : diff.keySet()) {
                //                    RVInfo drvInfo = diff.get(rv);
                //                    RVInfo qrvInfo = quotient.randomVarInfo.get(rv);
                //                    qRVs[qrvInfo.getRadixIdx()] = dMRN
                //                            .getCurrentNumeralValue(drvInfo
                //                                    .getRadixIdx());
                //                }
                //                updateQuotient(probability);
                //            } while (dMRN.increment());
                //        } else {
                //            updateQuotient(probability);
                //        }
                //    }

                //    //
                //
                //private void updateQuotient(double probability) {
                //    int offset = (int) qMRN.getCurrentValueFor(qRVs);
                //    if (0 == probability) {
                //        quotient.getValues()[offset] = 0;
                //    } else {
                //        quotient.getValues()[offset] += getValues()[offset]
                //                / probability;
                //    }
                //}
                //////  };

                //	divisor.iterateOverTable(divisorIterator);
                // TODO
            }

            return(quotient);
        }
コード例 #7
0
ファイル: CPT.cs プロジェクト: PaulMineau/AIMA.Net
	public Factor getFactorFor(params AssignmentProposition[] evidence) {
		Set<RandomVariable> fofVars = new Set<RandomVariable>(
				table.getFor());
		foreach (AssignmentProposition ap in evidence) {
			fofVars.Remove(ap.getTermVariable());
		}
		 ProbabilityTable fof = new ProbabilityTable(new List<RandomVariable>(fofVars));
		// Otherwise need to iterate through the table for the
		// non evidence variables.
		 Object[] termValues = new Object[fofVars.Count];
        //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
        //    public void iterate(Map<RandomVariable, Object> possibleWorld,
        //            double probability) {
        //        if (0 == termValues.length) {
        //            fof.getValues()[0] += probability;
        //        } else {
        //            int i = 0;
        //            for (RandomVariable rv : fof.getFor()) {
        //                termValues[i] = possibleWorld.get(rv);
        //                i++;
        //            }
        //            fof.getValues()[fof.getIndex(termValues)] += probability;
        //        }
        //    }
        //};
		//table.iterateOverTable(di, evidence);
        // TODO: another screwed up iterator
		return fof;
	}
コード例 #8
0
ファイル: CPT.cs プロジェクト: PaulMineau/AIMA.Net
	public CategoricalDistribution getConditioningCase(
			params AssignmentProposition[] parentValues) {
		if (parentValues.Length != parents.Count) {
			throw new ArgumentException(
					"The number of parent value arguments ["
							+ parentValues.Length
							+ "] is not equal to the number of parents ["
							+ parents.Count + "] for this CPT.");
		}
		 ProbabilityTable cc = new ProbabilityTable(getOn());
        //ProbabilityTable.Iterator  pti = new ProbabilityTable.Iterator() {
        //    private int idx = 0;


        //    public void iterate(Map<RandomVariable, Object> possibleAssignment,
        //            double probability) {
        //        cc.getValues()[idx] = probability;
        //        idx++;
        //    }
        //};
        //table.iterateOverTable(pti, parentValues);
        // TODO: this is screwed up
		return cc;
	}
コード例 #9
0
        public CategoricalDistribution jointDistribution(
            params IProposition[] propositions)
        {
            ProbabilityTable d = null;
            IProposition conjProp = ProbUtil
                .constructConjunction(propositions);
            LinkedHashSet<RandomVariable> vars = new LinkedHashSet<RandomVariable>(
                conjProp.getUnboundScope());

            if (vars.Count > 0)
            {
                RandomVariable[] distVars = new RandomVariable[vars.Count];
                vars.CopyTo(distVars);

                ProbabilityTable ud = new ProbabilityTable(distVars);
                Object[] values = new Object[vars.Count];

                //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {

                //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                //            double probability) {
                //        if (conjProp.holds(possibleWorld)) {
                //            int i = 0;
                //            for (RandomVariable rv : vars) {
                //                values[i] = possibleWorld.get(rv);
                //                i++;
                //            }
                //            int dIdx = ud.getIndex(values);
                //            ud.setValue(dIdx, ud.getValues()[dIdx] + probability);
                //        }
                //    }
                //};

                //distribution.iterateOverTable(di);
                // TODO:
                d = ud;
            }
            else
            {
                // No Unbound Variables, therefore just return
                // the singular probability related to the proposition.
                d = new ProbabilityTable();
                d.setValue(0, prior(propositions));
            }
            return d;
        }
コード例 #10
0
        public ProbabilityTable pointwiseProductPOS(
            ProbabilityTable multiplier, params RandomVariable[] prodVarOrder)
        {
            ProbabilityTable product = new ProbabilityTable(prodVarOrder);
            if (!product.randomVarInfo.keySet().Equals(
                SetOps.union(new List<RandomVariable>(randomVarInfo.keySet()),
                                                      new List<RandomVariable>(multiplier.randomVarInfo
                                                                                   .keySet()))))
            {
                if (1 == product.getValues().Length)
                {
                    product.getValues()[0] = getValues()[0]*multiplier.getValues()[0];
                }
                else
                {
                    // Otherwise need to iterate through the product
                    // to calculate its values based on the terms.
                    Object[] term1Values = new Object[randomVarInfo.size()];
                    Object[] term2Values = new Object[multiplier.randomVarInfo
                        .size()];
                    //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
                    //    private int idx = 0;

                    //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                    //            double probability) {
                    //        int term1Idx = termIdx(term1Values, ProbabilityTable.this,
                    //                possibleWorld);
                    //        int term2Idx = termIdx(term2Values, multiplier,
                    //                possibleWorld);

                    //        product.getValues()[idx] = getValues()[term1Idx]
                    //                * multiplier.getValues()[term2Idx];

                    //        idx++;
                    //    }

                    //    private int termIdx(Object[] termValues, ProbabilityTable d,
                    //            Map<RandomVariable, Object> possibleWorld) {
                    //        if (0 == termValues.Length) {
                    //            // The term has no variables so always position 0.
                    //            return 0;
                    //        }

                    //        int i = 0;
                    //        for (RandomVariable rv : d.randomVarInfo.keySet()) {
                    //            termValues[i] = possibleWorld.get(rv);
                    //            i++;
                    //        }

                    //        return d.getIndex(termValues);
                    //    }
                    //};
                    //product.iterateOverTable(di);
                    // TODO		
                }
            }
            return product;
        }
コード例 #11
0
 public ProbabilityTable pointwiseProduct(ProbabilityTable multiplier)
 {
     List<RandomVariable> prodVars = SetOps.union(new List<RandomVariable>(randomVarInfo.Keys),
                                                 new List<RandomVariable>(multiplier.randomVarInfo.Keys));
     return pointwiseProductPOS(multiplier, prodVars
                                                .ToArray());
 }
コード例 #12
0
        public ProbabilityTable divideBy(ProbabilityTable divisor)
        {
            if (!randomVarInfo.keySet().containsAll(divisor.randomVarInfo.keySet()))
            {
                throw new IllegalArgumentException(
                    "Divisor must be a subset of the dividend.");
            }

            ProbabilityTable quotient = new ProbabilityTable(new List<RandomVariable>(randomVarInfo.Keys));

            if (1 == divisor.getValues().Length)
            {
                double d = divisor.getValues()[0];
                for (int i = 0; i < quotient.getValues().Length; i++)
                {
                    if (0 == d)
                    {
                        quotient.getValues()[i] = 0;
                    }
                    else
                    {
                        quotient.getValues()[i] = getValues()[i]/d;
                    }
                }
            }
            else
            {
                Set<RandomVariable> dividendDivisorDiff = SetOps
                    .difference(new List<RVInfo>(randomVarInfo.keySet()),
                                new List<RVInfo>(randomVarInfo.keySet()));
                Map<RandomVariable, RVInfo> tdiff = null;
                MixedRadixNumber tdMRN = null;
                if (dividendDivisorDiff.size() > 0)
                {
                    tdiff = new LinkedHashMap<RandomVariable, RVInfo>();
                    foreach (RandomVariable rv in dividendDivisorDiff)
                    {
                        tdiff.put(rv, new RVInfo(rv));
                    }
                    tdMRN = new MixedRadixNumber(0, createRadixs(tdiff));
                }
                Map<RandomVariable, RVInfo> diff = tdiff;
                MixedRadixNumber dMRN = tdMRN;
                int[] qRVs = new int[quotient.radices.Length];
                MixedRadixNumber qMRN = new MixedRadixNumber(0,
                                                             quotient.radices);
                //ProbabilityTable.Iterator divisorIterator = new ProbabilityTable.Iterator() {
                //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                //            double probability) {
                //        foreach (RandomVariable rv in possibleWorld.keySet()) {
                //            RVInfo rvInfo = quotient.randomVarInfo.get(rv);
                //            qRVs[rvInfo.getRadixIdx()] = rvInfo
                //                    .getIdxForDomain(possibleWorld.get(rv));
                //        }
                //        if (null != diff) {
                //            // Start from 0 off the diff
                //            dMRN.setCurrentValueFor(new int[diff.size()]);
                //            do {
                //                for (RandomVariable rv : diff.keySet()) {
                //                    RVInfo drvInfo = diff.get(rv);
                //                    RVInfo qrvInfo = quotient.randomVarInfo.get(rv);
                //                    qRVs[qrvInfo.getRadixIdx()] = dMRN
                //                            .getCurrentNumeralValue(drvInfo
                //                                    .getRadixIdx());
                //                }
                //                updateQuotient(probability);
                //            } while (dMRN.increment());
                //        } else {
                //            updateQuotient(probability);
                //        }
                //    }

                //    //
                //
                //private void updateQuotient(double probability) {
                //    int offset = (int) qMRN.getCurrentValueFor(qRVs);
                //    if (0 == probability) {
                //        quotient.getValues()[offset] = 0;
                //    } else {
                //        quotient.getValues()[offset] += getValues()[offset]
                //                / probability;
                //    }
                //}
                ////// 	};

                //	divisor.iterateOverTable(divisorIterator);
                // TODO
            }

            return quotient;
        }
コード例 #13
0
        public ProbabilityTable sumOut(params RandomVariable[] vars)
        {
            Set<RandomVariable> soutVars = new Set<RandomVariable>(
                this.randomVarInfo.keySet());
            foreach (RandomVariable rv in vars)
            {
                soutVars.remove(rv);
            }
            ProbabilityTable summedOut = new ProbabilityTable(soutVars);
            if (1 == summedOut.getValues().Length)
            {
                summedOut.getValues()[0] = getSum();
            }
            else
            {
                // Otherwise need to iterate through this distribution
                // to calculate the summed out distribution.
                Object[] termValues = new Object[summedOut.randomVarInfo
                    .size()];
                //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
                //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                //            double probability) {

                //        int i = 0;
                //        foreach (RandomVariable rv in summedOut.randomVarInfo.keySet()) {
                //            termValues[i] = possibleWorld.get(rv);
                //            i++;
                //        }
                //        summedOut.getValues()[summedOut.getIndex(termValues)] += probability;
                //    }
                //};
                //iterateOverTable(di);
                //TODO:
            }

            return summedOut;
        }