Ejemplo n.º 1
0
        public void TestAgainstReference()
        {
            try
            {
                // All this boilerplate is just to load JSON

                double   maxTol         = 5e-3;
                string   path           = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Ebisu/Ebisu_test.json");
                string[] testData       = File.ReadAllLines(path);
                JArray   expectedResult = (JArray)JsonConvert.DeserializeObject(testData[0]);

                foreach (var child in expectedResult)
                {
                    //subtest might be either
                    // a) ["update", [3.3, 4.4, 1.0], [0, 5, 0.1], {"post": [7.333641958415551, 8.949256654818793,
                    // 0.4148304099305316]}] or b) ["predict", [34.4, 34.4, 1.0], [5.5], {"mean": 0.026134289032202798}]
                    //
                    // In both cases, the first two elements are a string and an array of numbers. Then the remaining vary depend on
                    // what that string is. where the numbers are arbitrary. So here we go...
                    String operation = child[0].ToString();

                    JArray     second = (JArray)child[1];
                    EbisuModel ebisu  = new EbisuModel(double.Parse(second[2].ToString()), double.Parse(second[0].ToString()), double.Parse(second[1].ToString()));

                    if (operation.Equals("update"))
                    {
                        int        successes = Convert.ToInt32(child[2][0].ToString());
                        int        total     = Convert.ToInt32(child[2][1].ToString());
                        double     t         = Convert.ToDouble(child[2][2].ToString());
                        JArray     third     = (JArray)child[3].Last.Last;//subtest.get(3).get("post");
                        EbisuModel expected  = new EbisuModel(double.Parse(third[2].ToString()), double.Parse(third[0].ToString()), double.Parse(third[1].ToString()));

                        IEbisu actual = Ebisu.UpdateRecall(ebisu, successes, total, t);

                        Assert.AreEqual(expected.getAlpha(), actual.getAlpha(), maxTol);
                        Assert.AreEqual(expected.getBeta(), actual.getBeta(), maxTol);
                        Assert.AreEqual(expected.getTime(), actual.getTime(), maxTol);
                    }
                    else if (operation.Equals("predict"))
                    {
                        double t        = Convert.ToDouble(child[2][0].ToString());
                        double expected = Convert.ToDouble(child[3].First.Last.ToString());
                        double actual   = Ebisu.PredictRecall(ebisu, t, true);
                        Assert.AreEqual(expected, actual, maxTol);
                    }
                    else
                    {
                        throw new Exception("unknown operation");
                    }
                }
            }
            catch (Exception ex)
            {
                ex.StackTrace.ToString();
                Console.WriteLine("¡¡¡OOOPS SOMETHING BAD HAPPENED!!!");
                Assert.IsTrue(false);
            }
        }