예제 #1
0
        public ProbabilityTable pointwiseProductPOS(ProbabilityTable multiplier, params IRandomVariable[] prodVarOrder)
        {
            ProbabilityTable product = new ProbabilityTable(prodVarOrder);

            if (!product.randomVarInfo.GetKeys()
                .SequenceEqual(
                    SetOps.union(CollectionFactory.CreateSet <IRandomVariable>(randomVarInfo.GetKeys()), CollectionFactory.CreateSet <IRandomVariable>(multiplier.randomVarInfo.GetKeys()))))
            {
                throw new IllegalArgumentException("Specified list deatailing order of mulitplier is inconsistent.");
            }

            // If no variables in the product
            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()];
                ProbabilityTableIterator di = new ProbabilityTablecIteratorImpl3(term1Values,
                                                                                 term2Values, product, multiplier, this);
                product.iterateOverTable(di);
            }

            return(product);
        }
예제 #2
0
        public ProbabilityTable sumOut(params IRandomVariable[] vars)
        {
            ISet <IRandomVariable> soutVars = CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys());

            foreach (IRandomVariable 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()];
                ProbabilityTableIterator di = new ProbabilityTableIteratorImpl(summedOut, termValues);

                iterateOverTable(di);
            }

            return(summedOut);
        }
예제 #3
0
        public ProbabilityTable divideBy(ProbabilityTable divisor)
        {
            if (!randomVarInfo.GetKeys().ContainsAll(divisor.randomVarInfo.GetKeys()))
            {
                throw new IllegalArgumentException("Divisor must be a subset of the dividend.");
            }

            ProbabilityTable quotient = new ProbabilityTable(randomVarInfo.GetKeys());

            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
            {
                ISet <IRandomVariable> dividendDivisorDiff = SetOps
                                                             .difference(
                    CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys()),
                    CollectionFactory.CreateSet <IRandomVariable>(divisor.randomVarInfo.GetKeys()));
                IMap <IRandomVariable, RVInfo> tdiff = null;
                MixedRadixNumber tdMRN = null;
                if (dividendDivisorDiff.Size() > 0)
                {
                    tdiff = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, RVInfo>();
                    foreach (IRandomVariable rv in dividendDivisorDiff)
                    {
                        tdiff.Put(rv, new RVInfo(rv));
                    }
                    tdMRN = new MixedRadixNumber(0, createRadixs(tdiff));
                }
                IMap <IRandomVariable, RVInfo> diff      = tdiff;
                MixedRadixNumber         dMRN            = tdMRN;
                int[]                    qRVs            = new int[quotient.radices.Length];
                MixedRadixNumber         qMRN            = new MixedRadixNumber(0, quotient.radices);
                ProbabilityTableIterator divisorIterator = new ProbabilityTableIteratorImp2(quotient, qRVs, qMRN, dMRN, diff, this);

                divisor.iterateOverTable(divisorIterator);
            }

            return(quotient);
        }
예제 #4
0
            //
            //
            private void updateQuotient(double probability)
            {
                int offset = (int)qMRN.GetCurrentValueFor(qRVs);

                if (0 == probability)
                {
                    quotient.getValues()[offset] = 0;
                }
                else
                {
                    quotient.getValues()[offset] += probabilityTable.getValues()[offset] / probability;
                }
            }
예제 #5
0
            public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability)
            {
                int term1Idx = termIdx(term1Values, probabilityTable, possibleWorld);
                int term2Idx = termIdx(term2Values, multiplier, possibleWorld);

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

                idx++;
            }
예제 #6
0
            public void iterate(IMap <IRandomVariable, object> possibleWorld, double probability)
            {
                int i = 0;

                foreach (IRandomVariable rv in summedOut.randomVarInfo.GetKeys())
                {
                    termValues[i] = possibleWorld.Get(rv);
                    ++i;
                }
                summedOut.getValues()[summedOut.getIndex(termValues)] += probability;
            }