public virtual void IncrementCount(K1 o1, K2 o2, double count)
        {
            ClassicCounter <K2> c = GetCounter(o1);

            c.IncrementCount(o2, count);
            total += count;
        }
Exemplo n.º 2
0
        public virtual void TestToSortedString()
        {
            ICounter <string> c = new ClassicCounter <string>();

            c.SetCount("b", 0.25);
            c.SetCount("a", 0.5);
            c.SetCount("c", 1.0);
            // check full argument version
            string result = Counters.ToSortedString(c, 5, "%s%.1f", ":", "{%s}");

            NUnit.Framework.Assert.AreEqual("{c1.0:a0.5:b0.3}", result);
            // check version with no wrapper
            result = Counters.ToSortedString(c, 2, "%2$f %1$s", "\n");
            NUnit.Framework.Assert.AreEqual("1.000000 c\n0.500000 a", result);
            // check some equivalences to other Counters methods
            int k = 2;

            result = Counters.ToSortedString(c, k, "%s=%s", ", ", "[%s]");
            NUnit.Framework.Assert.AreEqual(Counters.ToString(c, k), result);
            NUnit.Framework.Assert.AreEqual(Counters.ToBiggestValuesFirstString(c, k), result);
            result = Counters.ToSortedString(c, k, "%2$g\t%1$s", "\n", "%s\n");
            NUnit.Framework.Assert.AreEqual(Counters.ToVerticalString(c, k), result);
            // test sorting by keys
            result = Counters.ToSortedByKeysString(c, "%s=>%.2f", "; ", "<%s>");
            NUnit.Framework.Assert.AreEqual("<a=>0.50; b=>0.25; c=>1.00>", result);
        }
Exemplo n.º 3
0
        private double Percentage(OUT key, ClassicCounter <OUT> guessed, ClassicCounter <OUT> guessedCorrect)
        {
            double thisGuessed        = guessed.GetCount(key);
            double thisGuessedCorrect = guessedCorrect.GetCount(key);

            return((thisGuessed == 0.0) ? 0.0 : thisGuessedCorrect / thisGuessed);
        }
Exemplo n.º 4
0
        public virtual void TestSerializeStringCounter()
        {
            ICounter <string> counts = new ClassicCounter <string>();

            for (int @base = -10; @base < 10; ++@base)
            {
                if (@base == 0)
                {
                    continue;
                }
                for (int exponent = -100; exponent < 100; ++exponent)
                {
                    double number = Math.Pow(Math.Pi * @base, exponent);
                    counts.SetCount(double.ToString(number), number);
                }
            }
            File tmp = File.CreateTempFile("counts", ".tab.gz");

            tmp.DeleteOnExit();
            Counters.SerializeStringCounter(counts, tmp.GetPath());
            ICounter <string> reread = Counters.DeserializeStringCounter(tmp.GetPath());

            foreach (KeyValuePair <string, double> entry in reread.EntrySet())
            {
                double old = counts.GetCount(entry.Key);
                NUnit.Framework.Assert.AreEqual(old, entry.Value, Math.Abs(old) / 1e5);
            }
        }
Exemplo n.º 5
0
        public virtual void TestGetDistributionFromLogValues()
        {
            ICounter <string> c1 = new ClassicCounter <string>();

            c1.SetCount("p", 1.0);
            c1.SetCount("q", 2.0);
            c1.SetCount("r", 3.0);
            c1.SetCount("s", 4.0);
            // take log
            Counters.LogInPlace(c1);
            // now call distribution
            Distribution <string> distribution = Distribution.GetDistributionFromLogValues(c1);

            // test
            NUnit.Framework.Assert.AreEqual(distribution.KeySet().Count, 4);
            // size
            // keys
            NUnit.Framework.Assert.AreEqual(distribution.ContainsKey("p"), true);
            NUnit.Framework.Assert.AreEqual(distribution.ContainsKey("q"), true);
            NUnit.Framework.Assert.AreEqual(distribution.ContainsKey("r"), true);
            NUnit.Framework.Assert.AreEqual(distribution.ContainsKey("s"), true);
            // values
            NUnit.Framework.Assert.AreEqual(distribution.GetCount("p"), 1.0E-1, 1E-10);
            NUnit.Framework.Assert.AreEqual(distribution.GetCount("q"), 2.0E-1, 1E-10);
            NUnit.Framework.Assert.AreEqual(distribution.GetCount("r"), 3.0E-1, 1E-10);
            NUnit.Framework.Assert.AreEqual(distribution.GetCount("s"), 4.0E-1, 1E-10);
        }
Exemplo n.º 6
0
 public DirichletProcess(IProbabilityDistribution <E> baseMeasure, double alpha)
 {
     this.baseMeasure = baseMeasure;
     this.alpha       = alpha;
     this.sampled     = new ClassicCounter <E>();
     sampled.IncrementCount(null, alpha);
 }
        public virtual void SubtractAll(K1 key, ICounter <K2> c)
        {
            ClassicCounter <K2> myInner = GetCounter(key);

            Counters.SubtractInPlace(myInner, c);
            total -= c.TotalCount();
        }
Exemplo n.º 8
0
        /// <summary>for testing purposes only</summary>
        public static void Main(string[] args)
        {
            object[] a1 = new object[] { "a", "b" };
            object[] a2 = new object[] { "a", "b" };
            System.Console.Out.WriteLine(Arrays.Equals(a1, a2));
            GeneralizedCounter <string> gc = new GeneralizedCounter <string>(3);

            gc.IncrementCount(Arrays.AsList(new string[] { "a", "j", "x" }), 3.0);
            gc.IncrementCount(Arrays.AsList(new string[] { "a", "l", "x" }), 3.0);
            gc.IncrementCount(Arrays.AsList(new string[] { "b", "k", "y" }), 3.0);
            gc.IncrementCount(Arrays.AsList(new string[] { "b", "k", "z" }), 3.0);
            System.Console.Out.WriteLine("incremented counts.");
            System.Console.Out.WriteLine(gc.DumpKeys());
            System.Console.Out.WriteLine("string representation of generalized counter:");
            System.Console.Out.WriteLine(gc.ToString());
            gc.PrintKeySet();
            System.Console.Out.WriteLine("entry set:\n" + gc.EntrySet());
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "a", "j", "x" })));
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "a", "j", "z" })));
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "b", "k", "w" })));
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "b", "k", "z" })));
            GeneralizedCounter <string> gc1 = gc.Conditionalize(Arrays.AsList(new string[] { "a" }));

            gc1.IncrementCount(Arrays.AsList(new string[] { "j", "x" }));
            gc1.IncrementCount2D("j", "z");
            GeneralizedCounter <string> gc2 = gc1.Conditionalize(Arrays.AsList(new string[] { "j" }));

            gc2.IncrementCount1D("x");
            System.Console.Out.WriteLine("Pretty-printing gc after incrementing gc1:");
            gc.PrettyPrint();
            System.Console.Out.WriteLine("Total: " + gc.TotalCount());
            gc1.PrintKeySet();
            System.Console.Out.WriteLine("another entry set:\n" + gc1.EntrySet());
            ClassicCounter <IList <string> > c = gc.CounterView();

            System.Console.Out.WriteLine("string representation of counter view:");
            System.Console.Out.WriteLine(c.ToString());
            double d1 = c.GetCount(Arrays.AsList(new string[] { "a", "j", "x" }));
            double d2 = c.GetCount(Arrays.AsList(new string[] { "a", "j", "w" }));

            System.Console.Out.WriteLine(d1 + " " + d2);
            ClassicCounter <IList <string> > c1 = gc1.CounterView();

            System.Console.Out.WriteLine("Count of {j,x} -- should be 3.0\t" + c1.GetCount(Arrays.AsList(new string[] { "j", "x" })));
            System.Console.Out.WriteLine(c.KeySet() + " size " + c.KeySet().Count);
            System.Console.Out.WriteLine(c1.KeySet() + " size " + c1.KeySet().Count);
            System.Console.Out.WriteLine(c1.Equals(c));
            System.Console.Out.WriteLine(c.Equals(c1));
            System.Console.Out.WriteLine(c.Equals(c));
            System.Console.Out.WriteLine("### testing equality of regular Counter...");
            ClassicCounter <string> z1 = new ClassicCounter <string>();
            ClassicCounter <string> z2 = new ClassicCounter <string>();

            z1.IncrementCount("a1");
            z1.IncrementCount("a2");
            z2.IncrementCount("b");
            System.Console.Out.WriteLine(z1.Equals(z2));
            System.Console.Out.WriteLine(z1.ToString());
            System.Console.Out.WriteLine(z1.KeySet().ToString());
        }
Exemplo n.º 9
0
        public virtual Edu.Stanford.Nlp.Stats.Dirichlet <E> GetPosteriorDistribution(ICounter <E> counts)
        {
            ICounter <E> newParameters = new ClassicCounter <E>(parameters);

            Counters.AddInPlace(newParameters, counts);
            return(new Edu.Stanford.Nlp.Stats.Dirichlet <E>(newParameters));
        }
Exemplo n.º 10
0
        public virtual void TestPointwiseMutualInformation()
        {
            ICounter <string> x = new ClassicCounter <string>();

            x.IncrementCount("0", 0.8);
            x.IncrementCount("1", 0.2);
            ICounter <int> y = new ClassicCounter <int>();

            y.IncrementCount(0, 0.25);
            y.IncrementCount(1, 0.75);
            ICounter <Pair <string, int> > joint;

            joint = new ClassicCounter <Pair <string, int> >();
            joint.IncrementCount(new Pair <string, int>("0", 0), 0.1);
            joint.IncrementCount(new Pair <string, int>("0", 1), 0.7);
            joint.IncrementCount(new Pair <string, int>("1", 0), 0.15);
            joint.IncrementCount(new Pair <string, int>("1", 1), 0.05);
            // Check that correct PMI values are calculated, using tables from
            // http://en.wikipedia.org/wiki/Pointwise_mutual_information
            double             pmi;
            Pair <string, int> pair;

            pair = new Pair <string, int>("0", 0);
            pmi  = Counters.PointwiseMutualInformation(x, y, joint, pair);
            NUnit.Framework.Assert.AreEqual(-1, pmi, 10e-5);
            pair = new Pair <string, int>("0", 1);
            pmi  = Counters.PointwiseMutualInformation(x, y, joint, pair);
            NUnit.Framework.Assert.AreEqual(0.222392421, pmi, 10e-5);
            pair = new Pair <string, int>("1", 0);
            pmi  = Counters.PointwiseMutualInformation(x, y, joint, pair);
            NUnit.Framework.Assert.AreEqual(1.584962501, pmi, 10e-5);
            pair = new Pair <string, int>("1", 1);
            pmi  = Counters.PointwiseMutualInformation(x, y, joint, pair);
            NUnit.Framework.Assert.AreEqual(-1.584962501, pmi, 10e-5);
        }
        public virtual void AddAll(K1 key, ICounter <K2> c)
        {
            ClassicCounter <K2> myInner = GetCounter(key);

            Counters.AddInPlace(myInner, c);
            total += c.TotalCount();
        }
Exemplo n.º 12
0
        public virtual ClassicCounter <OUT> LastPrecision()
        {
            ClassicCounter <OUT> result = new ClassicCounter <OUT>();

            Counters.AddInPlace(result, previousGuessedCorrect);
            Counters.DivideInPlace(result, previousGuessed);
            return(result);
        }
Exemplo n.º 13
0
        public virtual ClassicCounter <OUT> LastRecall()
        {
            ClassicCounter <OUT> result = new ClassicCounter <OUT>();

            Counters.AddInPlace(result, previousGoldCorrect);
            Counters.DivideInPlace(result, previousGold);
            return(result);
        }
        public virtual void SetCount(K1 o1, K2 o2, double count)
        {
            ClassicCounter <K2> c = GetCounter(o1);
            double oldCount       = GetCount(o1, o2);

            total -= oldCount;
            c.SetCount(o2, count);
            total += count;
        }
        // it's empty, get rid of it!
        public virtual void Remove(K1 key)
        {
            ClassicCounter <K2> counter = map[key];

            if (counter != null)
            {
                total -= counter.TotalCount();
            }
            Sharpen.Collections.Remove(map, key);
        }
        /// <summary>
        /// Returns the counters with keys as the first key and count as the
        /// total count of the inner counter for that key
        /// </summary>
        /// <returns>counter of type K1</returns>
        public virtual ICounter <K1> SumInnerCounter()
        {
            ICounter <K1> summed = new ClassicCounter <K1>();

            foreach (K1 key in this.FirstKeySet())
            {
                summed.IncrementCount(key, this.GetCounter(key).TotalCount());
            }
            return(summed);
        }
 public virtual void AddAll(ITwoDimensionalCounterInterface <K1, K2> c)
 {
     foreach (K1 key in c.FirstKeySet())
     {
         ICounter <K2>       inner   = c.GetCounter(key);
         ClassicCounter <K2> myInner = GetCounter(key);
         Counters.AddInPlace(myInner, inner);
         total += inner.TotalCount();
     }
 }
        public virtual double GetCount(K1 o1, K2 o2)
        {
            ClassicCounter <K2> c = GetCounter(o1);

            if (c.TotalCount() == 0.0 && !c.KeySet().Contains(o2))
            {
                return(DefaultReturnValue());
            }
            return(c.GetCount(o2));
        }
        public virtual bool ContainsKey(K1 o1, K2 o2)
        {
            if (!map.Contains(o1))
            {
                return(false);
            }
            ClassicCounter <K2> c = map[o1];

            return(c.ContainsKey(o2));
        }
Exemplo n.º 20
0
        public static double SampleBeta(double a, double b, Random random)
        {
            ICounter <bool> c = new ClassicCounter <bool>();

            c.SetCount(true, a);
            c.SetCount(false, b);
            Multinomial <bool> beta = (new Edu.Stanford.Nlp.Stats.Dirichlet <bool>(c)).DrawSample(random);

            return(beta.ProbabilityOf(true));
        }
Exemplo n.º 21
0
        public virtual ClassicCounter <OUT> LastF1()
        {
            ClassicCounter <OUT> result = new ClassicCounter <OUT>();
            ICollection <OUT>    keys   = Sets.Union(previousGuessed.KeySet(), previousGold.KeySet());

            foreach (OUT key in keys)
            {
                result.SetCount(key, LastF1(key));
            }
            return(result);
        }
        /// <returns>total number of entries (key pairs)</returns>
        public virtual int Size()
        {
            int result = 0;

            foreach (K1 o in FirstKeySet())
            {
                ClassicCounter <K2> c = map[o];
                result += c.Size();
            }
            return(result);
        }
        /// <returns>the inner Counter associated with key o</returns>
        public virtual ClassicCounter <K2> GetCounter(K1 o)
        {
            ClassicCounter <K2> c = map[o];

            if (c == null)
            {
                c = new ClassicCounter <K2>(innerMF);
                c.SetDefaultReturnValue(defaultValue);
                map[o] = c;
            }
            return(c);
        }
Exemplo n.º 24
0
        public virtual void TestL2Norm()
        {
            ClassicCounter <string> c = new ClassicCounter <string>();

            c.IncrementCount("a", 3);
            c.IncrementCount("b", 4);
            NUnit.Framework.Assert.AreEqual(5.0, Counters.L2Norm(c), Tolerance);
            c.IncrementCount("c", 6);
            c.IncrementCount("d", 4);
            c.IncrementCount("e", 2);
            NUnit.Framework.Assert.AreEqual(9.0, Counters.L2Norm(c), Tolerance);
        }
Exemplo n.º 25
0
        /* Helper to simpleGoodTuringSmoothedCounter() */
        private static ICounter <int> CollectCountCounts <E>(ICounter <E> counts)
        {
            ICounter <int> cc = new ClassicCounter <int>();

            // counts of counts
            foreach (KeyValuePair <E, double> entry in counts.EntrySet())
            {
                //E item = entry.getKey();
                int count = (int)Math.Round(entry.Value);
                cc.IncrementCount(count);
            }
            return(cc);
        }
        public virtual double Remove(K1 o1, K2 o2)
        {
            ClassicCounter <K2> c = GetCounter(o1);
            double oldCount       = GetCount(o1, o2);

            total -= oldCount;
            c.Remove(o2);
            if (c.Size() == 0)
            {
                Sharpen.Collections.Remove(map, o1);
            }
            return(oldCount);
        }
 public virtual void SubtractAll(ITwoDimensionalCounterInterface <K1, K2> c, bool removeKeys)
 {
     foreach (K1 key in c.FirstKeySet())
     {
         ICounter <K2>       inner   = c.GetCounter(key);
         ClassicCounter <K2> myInner = GetCounter(key);
         Counters.SubtractInPlace(myInner, inner);
         if (removeKeys)
         {
             Counters.RetainNonZeros(myInner);
         }
         total -= inner.TotalCount();
     }
 }
        public virtual void RemoveZeroCounts()
        {
            ICollection <K1> firstKeySet = Generics.NewHashSet(FirstKeySet());

            foreach (K1 k1 in firstKeySet)
            {
                ClassicCounter <K2> c = GetCounter(k1);
                Counters.RetainNonZeros(c);
                if (c.Size() == 0)
                {
                    Sharpen.Collections.Remove(map, k1);
                }
            }
        }
Exemplo n.º 29
0
        /// <summary>Returns a new Distribution<K> with counts averaged from the two given Distributions.</summary>
        /// <remarks>
        /// Returns a new Distribution<K> with counts averaged from the two given Distributions.
        /// The average Distribution<K> will contain the union of keys in both
        /// source Distributions, and each count will be the weighted average of the two source
        /// counts for that key,  a missing count in one Distribution
        /// is treated as if it has probability equal to that returned by the probabilityOf() function.
        /// </remarks>
        /// <returns>
        /// A new distribution with counts that are the mean of the resp. counts
        /// in the given distributions with the remaining probability mass adjusted accordingly.
        /// </returns>
        public static Distribution <K> WeightedAverage <K>(Distribution <K> d1, double w1, Distribution <K> d2)
        {
            double          w2      = 1.0 - w1;
            ICollection <K> allKeys = GetSetOfAllKeys(d1, d2);
            int             numKeys = d1.GetNumberOfKeys();
            ICounter <K>    c       = new ClassicCounter <K>();

            foreach (K key in allKeys)
            {
                double newProbability = d1.ProbabilityOf(key) * w1 + d2.ProbabilityOf(key) * w2;
                c.SetCount(key, newProbability);
            }
            return(Distribution.GetDistributionFromPartiallySpecifiedCounter(c, numKeys));
        }
Exemplo n.º 30
0
        public virtual void TestL2Normalize()
        {
            ClassicCounter <string> c = new ClassicCounter <string>();

            c.IncrementCount("a", 4.0);
            c.IncrementCount("b", 2.0);
            c.IncrementCount("c", 1.0);
            c.IncrementCount("d", 2.0);
            ICounter <string> d = Counters.L2Normalize(c);

            NUnit.Framework.Assert.AreEqual(d.GetCount("a"), 0.8, Tolerance);
            NUnit.Framework.Assert.AreEqual(d.GetCount("b"), 0.4, Tolerance);
            NUnit.Framework.Assert.AreEqual(d.GetCount("c"), 0.2, Tolerance);
            NUnit.Framework.Assert.AreEqual(d.GetCount("d"), 0.4, Tolerance);
        }