コード例 #1
0
        public void TestSample()
        {
            Dictionary <int, int> fakeData = new Dictionary <int, int>();

            fakeData.Add(1, 2); //20%
            fakeData.Add(2, 4); //40%
            fakeData.Add(3, 3); //30%
            fakeData.Add(4, 1); //10%

            EmpiricalDistribution <int> toSample = new EmpiricalDistribution <int>();

            toSample.CreateDistribution(fakeData);
            //dist should look like: (.1, 4), (.3, 1), (.6, 3), (1, 2)

            int shouldBeFour = toSample.GetValue(.05);

            Assert.AreEqual(shouldBeFour, 4);
            shouldBeFour = toSample.GetValue(.1);
            Assert.AreEqual(shouldBeFour, 4);

            int shouldBeOne = toSample.GetValue(.11);

            Assert.AreEqual(shouldBeOne, 1);
            shouldBeOne = toSample.GetValue(.3);
            Assert.AreEqual(shouldBeOne, 1);

            int shouldBeThree = toSample.GetValue(.304);

            Assert.AreEqual(shouldBeThree, 3);

            int shouldBeTwo = toSample.GetValue(.7);

            Assert.AreEqual(shouldBeTwo, 2);

            shouldBeTwo = toSample.GetValue(1);
            Assert.AreEqual(shouldBeTwo, 2);
        }