Esempio n. 1
0
 /// <summary>
 /// Sets the current array of distributions to the given array of distributions
 /// </summary>
 /// <param name="value"></param>
 public void SetTo(DistributionArray <T> value)
 {
     if (IsValueType)
     {
         // Array.Copy does not work properly with reference types.
         if (value.Count != this.Count)
         {
             throw new ArgumentException("value.Count (" + value.Count + ") != this.Count (" + this.Count + ")");
         }
         Array.Copy(value.array, array, array.Length);
     }
     else
     {
         ModifyAll(value.array, delegate(T item, T aItem)
         {
             if (object.ReferenceEquals(item, null))
             {
                 return((T)aItem.Clone());
             }
             else
             {
                 item.SetTo(aItem);
             } return(item);
         });
     }
 }
Esempio n. 2
0
        public static double EvaluateLn <DistributionType, DomainType>(DistributionArray <DistributionType> darray, DomainType[,] array)
            where DistributionType : CanGetLogProb <DomainType>,
        ICloneable,
        SettableTo <DistributionType>,
        SettableToUniform,
        Diffable,
        SettableToProduct <DistributionType>,
        SettableToRatio <DistributionType>,
        SettableToPower <DistributionType>,
        SettableToWeightedSum <DistributionType>,
        CanGetLogAverageOf <DistributionType>,
        CanGetAverageLog <DistributionType>
        {
            if (darray.array2D == null)
            {
                throw new ArgumentException("DistributionArray has rank " + darray.array.Rank + ", point has rank " + array.Rank);
            }
            if (darray.array2D.GetLength(0) != array.GetLength(0))
            {
                throw new ArgumentException("DistributionArray lengths (" +
                                            StringUtil.CollectionToString(darray.GetLengths(), ",") + ") do not match array lengths (" +
                                            StringUtil.CollectionToString(StringUtil.ArrayDimensions(array), ",") + ")");
            }
            double sum = 0.0;

            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    sum += darray[i, j].GetLogProb(array[i, j]);
                }
            }
            return(sum);
        }
Esempio n. 3
0
        public static DistributionArray <T> FromCloning(T value, params int[] lengths)
        {
            DistributionArray <T> result = FromLength(lengths);

            result.InitializeTo(value);
            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// Clones the array and all items in the array.
        /// </summary>
        /// <returns>A new DistributionArray.</returns>
        public object Clone()
        {
            Array newArray = (Array)array.Clone();
            DistributionArray <T> result = new DistributionArray <T>(newArray);

            result.ModifyAll(delegate(T item) { return((T)item.Clone()); });
            return(result);
        }
Esempio n. 5
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a power of the corresponding element in a source distribution array
 /// </summary>
 /// <param name="a">The source distribution array</param>
 /// <param name="exponent">The exponent</param>
 public void SetToPower(DistributionArray <T> a, double exponent)
 {
     ModifyAll(a.array,
               delegate(T item, T aItem)
     {
         item.SetToPower(aItem, exponent);
         return(item);
     });
 }
Esempio n. 6
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a product of the corresponding distributions in two given
 /// distribution arrays
 /// </summary>
 /// <param name="a">The first distribution array</param>
 /// <param name="b">The second distribution array</param>
 public void SetToProduct(DistributionArray <T> a, DistributionArray <T> b)
 {
     ModifyAll(a.array, b.array,
               delegate(T item, T aItem, T bItem)
     {
         item.SetToProduct(aItem, bItem);
         return(item);
     });
 }
Esempio n. 7
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a weighted sum of the corresponding distributions in two given
 /// distribution arrays
 /// </summary>
 /// <param name="weight1">The first weight</param>
 /// <param name="a">The first distribution array</param>
 /// <param name="weight2">The second weight</param>
 /// <param name="b">The second distribution array</param>
 public void SetToSum(double weight1, DistributionArray <T> a, double weight2, DistributionArray <T> b)
 {
     ModifyAll(a.array, b.array,
               delegate(T item, T aItem, T bItem)
     {
         item.SetToSum(weight1, aItem, weight2, bItem);
         return(item);
     });
 }
Esempio n. 8
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a ratio of the corrresponding distributions in two given
 /// distribution arrays
 /// </summary>
 /// <param name="numerator">The numerator distribution array</param>
 /// <param name="denominator">The denominator distribution array</param>
 public void SetToRatio(DistributionArray <T> numerator, DistributionArray <T> denominator)
 {
     ModifyAll(numerator.array, denominator.array,
               delegate(T item, T aItem, T bItem)
     {
         item.SetToRatio(aItem, bItem);
         return(item);
     });
 }
Esempio n. 9
0
        /// <summary>
        /// The maximum difference in parameter values between this distribution and that distribution
        /// </summary>
        /// <param name="that">That distribution</param>
        /// <returns>The maximum difference</returns>
        public virtual double MaxDiff(object that)
        {
            DistributionArray <T> thatd = that as DistributionArray <T>;

            if ((object)thatd == null)
            {
                return(Double.PositiveInfinity);
            }
            return(Distribution.MaxDiff(this.array, thatd.array));
        }
Esempio n. 10
0
        public static DistributionArray <T> CloneElementsOf(Array array)
        {
            DistributionArray <T> result = new DistributionArray <T>(StringUtil.ArrayDimensions(array));

            result.ModifyAll(array, delegate(T item, T aItem)
            {
                return((T)aItem.Clone());
            });
            return(result);
        }
Esempio n. 11
0
        /// <summary>
        /// The expected logarithm of that distribution under this distribution.
        /// </summary>
        /// <param name="that">The distribution to take the logarithm of.</param>
        /// <returns><c>sum_x this.Evaluate(x)*Math.Log(that.Evaluate(x))</c></returns>
        /// <remarks>This is also known as the cross entropy.
        /// For a DistributionArray, this specializes to:
        /// <c>sum_i sum_x this[i].Evaluate(x)*Math.Log(that[i].Evaluate(x))</c>
        /// = <c>sum_i this[i].GetAverageLog(that[i])</c>
        /// </remarks>
        public double GetAverageLog(DistributionArray <T> that)
        {
            double sum = 0.0;

            ForEach(that.array, delegate(T item, T thatItem)
            {
                sum += item.GetAverageLog(thatItem);
            });
            return(sum);
        }
Esempio n. 12
0
        public static DistributionArray <T> CloneElementsOfCollection(ICollection <T> array)
        {
            DistributionArray <T> result = new DistributionArray <T>(array.Count);
            int i = 0;

            foreach (T item in array)
            {
                result.array1D[i++] = (T)item.Clone();
            }
            return(result);
        }
Esempio n. 13
0
        /// <summary>
        /// The maximum difference across all corresponding distributions in
        /// this distribution array and that distribution array
        /// </summary>
        /// <param name="that">That distribution array</param>
        /// <returns>The maximum difference</returns>
        public double MaxDiff(object that)
        {
            DistributionArray <T> thatd = that as DistributionArray <T>;

            if ((object)thatd == null)
            {
                return(Double.PositiveInfinity);
            }
            try {
                double diff = 0.0;
                ForEach(thatd.array,
                        delegate(T item, T bItem)
                {
                    diff = Math.Max(diff, item.MaxDiff(bItem));
                });
                return(diff);
            } catch {
                return(Double.PositiveInfinity);
            }
        }
Esempio n. 14
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a power of the corresponding element in a source distribution array
 /// </summary>
 /// <param name="a">The source distribution array</param>
 /// <param name="exponent">The exponent</param>
 public void SetToPower(DistributionArray <T> a, double exponent)
 {
     Distribution.SetToPower(array, a.array, exponent);
 }
Esempio n. 15
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a product of the corresponding distributions in two given
 /// distribution arrays
 /// </summary>
 /// <param name="a">The first distribution array</param>
 /// <param name="b">The second distribution array</param>
 public void SetToProduct(DistributionArray <T> a, DistributionArray <T> b)
 {
     Distribution.SetToProduct(array, a.array, b.array);
 }
Esempio n. 16
0
 /// <summary>
 /// The expected logarithm of that distribution under this distribution.
 /// </summary>
 /// <param name="that">The distribution to take the logarithm of.</param>
 /// <returns><c>sum_x this.Evaluate(x)*Math.Log(that.Evaluate(x))</c></returns>
 /// <remarks>This is also known as the cross entropy.
 /// For a DistributionArray, this specializes to:
 /// <c>sum_i sum_x this[i].Evaluate(x)*Math.Log(that[i].Evaluate(x))</c>
 /// = <c>sum_i this[i].GetAverageLog(that[i])</c>
 /// </remarks>
 public double GetAverageLog(DistributionArray <T> that)
 {
     return(Distribution.GetAverageLog(array, that.array));
 }
Esempio n. 17
0
 /// <summary>
 /// Copy constructor.
 /// </summary>
 /// <param name="that"></param>
 public DistributionArray(DistributionArray <T> that)
     : this(that.GetLengths())
 {
     InitializeTo(that);
 }
Esempio n. 18
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a weighted sum of the corresponding distributions in two given
 /// distribution arrays
 /// </summary>
 /// <param name="weight1">The first weight</param>
 /// <param name="a">The first distribution array</param>
 /// <param name="weight2">The second weight</param>
 /// <param name="b">The second distribution array</param>
 public void SetToSum(double weight1, DistributionArray <T> a, double weight2, DistributionArray <T> b)
 {
     Distribution.SetToSum(array, weight1, a.array, weight2, b.array);
 }
Esempio n. 19
0
 public void InitializeTo(DistributionArray <T> that)
 {
     if (IsValueType)
     {
         //ModifyAll(that.array, delegate(T item, T thatItem) { return thatItem; });
         if (array1D != null)
         {
             for (int i = 0; i < array1D.Length; i++)
             {
                 array1D[i] = that.array1D[i];
             }
         }
         else if (array2D != null)
         {
             for (int i = 0; i < array2D.GetLength(0); i++)
             {
                 for (int j = 0; j < array2D.GetLength(1); j++)
                 {
                     array2D[i, j] = that.array2D[i, j];
                 }
             }
         }
         else
         {
             int[] mIndex = new int[array.Rank];
             for (int index = 0; index < Count; index++)
             {
                 StringUtil.LinearIndexToMultidimensionalIndex(index, strides, mIndex);
                 T item = (T)that.array.GetValue(mIndex);
                 array.SetValue(item, mIndex);
             }
         }
     }
     else
     {
         //ModifyAll(that.array, delegate(T item, T thatItem) { return (T)thatItem.Clone(); });
         if (array1D != null)
         {
             for (int i = 0; i < array1D.Length; i++)
             {
                 array1D[i] = (T)that.array1D[i].Clone();
             }
         }
         else if (array2D != null)
         {
             for (int i = 0; i < array2D.GetLength(0); i++)
             {
                 for (int j = 0; j < array2D.GetLength(1); j++)
                 {
                     array2D[i, j] = (T)that.array2D[i, j].Clone();
                 }
             }
         }
         else
         {
             int[] mIndex = new int[array.Rank];
             for (int index = 0; index < Count; index++)
             {
                 StringUtil.LinearIndexToMultidimensionalIndex(index, strides, mIndex);
                 T item = (T)that.array.GetValue(mIndex);
                 array.SetValue((T)item.Clone(), mIndex);
             }
         }
     }
 }
Esempio n. 20
0
 public void SetTo(DistributionArray <T> that)
 {
     base.SetTo(that);
 }
Esempio n. 21
0
 /// <summary>
 /// Sets the current instance to an array of distributions each element
 /// of which is a ratio of the corrresponding distributions in two given
 /// distribution arrays
 /// </summary>
 /// <param name="numerator">The numerator distribution array</param>
 /// <param name="denominator">The denominator distribution array</param>
 public void SetToRatio(DistributionArray <T> numerator, DistributionArray <T> denominator)
 {
     Distribution.SetToRatio(array, numerator.array, denominator.array);
 }