Пример #1
0
        public ProbabilityTable(double[] vals, params IRandomVariable[] vars)
        {
            if (null == vals)
            {
                throw new IllegalArgumentException("Values must be specified");
            }
            if (vals.Length != ProbUtil.expectedSizeOfProbabilityTable(vars))
            {
                throw new IllegalArgumentException("ProbabilityTable of length "
                                                   + vals.Length + " is not the correct size, should be "
                                                   + ProbUtil.expectedSizeOfProbabilityTable(vars)
                                                   + " in order to represent all possible combinations.");
            }
            if (null != vars)
            {
                foreach (IRandomVariable rv in vars)
                {
                    // Track index information relevant to each variable.
                    randomVarInfo.Put(rv, new RVInfo(rv));
                }
            }

            values = new double[vals.Length];
            System.Array.Copy(vals, 0, values, 0, vals.Length);

            radices = createRadixs(randomVarInfo);

            if (radices.Length > 0)
            {
                queryMRN = new MixedRadixNumber(0, radices);
            }
        }
Пример #2
0
        /**
         * Calculate the indexes for X[i] into a vector representing the enumeration
         * of the value assignments for the variables X and their corresponding
         * assignment in x. For example the Random Variables:<br>
         * Q::{true, false}, R::{'A', 'B','C'}, and T::{true, false}, would be
         * enumerated in a Vector as follows:
         *
         * <pre>
         * Index  Q      R  T
         * -----  -      -  -
         * 00:    true,  A, true
         * 01:    true,  A, false
         * 02:    true,  B, true
         * 03:    true,  B, false
         * 04:    true,  C, true
         * 05:    true,  C, false
         * 06:    false, A, true
         * 07:    false, A, false
         * 08:    false, B, true
         * 09:    false, B, false
         * 10:    false, C, true
         * 11:    false, C, false
         * </pre>
         *
         * if X[i] = R and x = {..., R='C', ...} then the indexes returned would be
         * [4, 5, 10, 11].
         *
         * @param X
         *            a list of the Random Variables that would comprise the vector.
         * @param idx
         *            the index into X for the Random Variable whose assignment we
         *            wish to retrieve its indexes for.
         * @param x
         *            an assignment for the Random Variables in X.
         * @return the indexes into a vector that would represent the enumeration of
         *         the values for X[i] in x.
         */
        public static int[] indexesOfValue(IRandomVariable[] X, int idx, IMap <IRandomVariable, object> x)
        {
            int csize = ProbUtil.expectedSizeOfCategoricalDistribution(X);

            IFiniteDomain fd       = (IFiniteDomain)X[idx].getDomain();
            int           vdoffset = fd.GetOffset(x.Get(X[idx]));
            int           vdosize  = fd.Size();

            int[] indexes = new int[csize / vdosize];

            int blocksize = csize;

            for (int i = 0; i < X.Length; ++i)
            {
                blocksize = blocksize / X[i].getDomain().Size();
                if (i == idx)
                {
                    break;
                }
            }

            for (int i = 0; i < indexes.Length; i += blocksize)
            {
                int offset = ((i / blocksize) * vdosize * blocksize)
                             + (blocksize * vdoffset);
                for (int b = 0; b < blocksize; b++)
                {
                    indexes[i + b] = offset + b;
                }
            }

            return(indexes);
        }
Пример #3
0
        public RandVar(string name, IDomain domain)
        {
            ProbUtil.checkValidRandomVariableName(name);
            if (null == domain)
            {
                throw new IllegalArgumentException("Domain of RandomVariable must be specified.");
            }

            this.name   = name;
            this.domain = domain;
            this.scope.Add(this);
        }
Пример #4
0
 public ProbabilityTable(params IRandomVariable[] vars)
     : this(new double[ProbUtil.expectedSizeOfProbabilityTable(vars)], vars)
 {
 }