public static Quickscore <TCause, TEffect> GetInstance(TCause leak) { Quickscore <TCause, TEffect> quickscore = new Quickscore <TCause, TEffect>(); quickscore.Leak = leak; quickscore.SetCause(leak, 1.0); //adding the "leak cause" return(quickscore); }
public static void Test() { Quickscore <string, string> quickscoreSS = Quickscore <string, string> .GetInstance(""); quickscoreSS.SetCause("cold", .01); quickscoreSS.SetLink("cold", "cough", .1); quickscoreSS.SetLink("cold", "runny nose", .3); AssertNear(.01, quickscoreSS.PosteriorOfCause("cold", new string[] { }, new string[] { })); AssertNear(1, quickscoreSS.PosteriorOfCause("cold", new string[] { "runny nose" }, new string[] { })); AssertNear(.9 * .01 / (1 - .01 * .1), quickscoreSS.PosteriorOfCause("cold", new string[] { }, new string[] { "cough" })); quickscoreSS.SetCause("onion attack", .001); quickscoreSS.SetLink("onion attack", "watery eyes", .9); AssertNear(.001, quickscoreSS.PosteriorOfCause("onion attack", new string[] { }, new string[] { })); AssertNear(.001, quickscoreSS.PosteriorOfCause("onion attack", new string[] { "cough" }, new string[] { })); AssertNear(.001, quickscoreSS.PosteriorOfCause("onion attack", new string[] { }, new string[] { "cough" })); AssertNear(1, quickscoreSS.PosteriorOfCause("onion attack", new string[] { "watery eyes" }, new string[] { })); quickscoreSS.SetCause("allergy", .02); quickscoreSS.SetLink("allergy", "watery eyes", .1); quickscoreSS.SetLink("allergy", "runny nose", .6); AssertNear(0.223055458667595, quickscoreSS.PosteriorOfCause("cold", new string[] { "runny nose" }, new string[] { "watery eyes" })); AssertNear(0.786362050924305, quickscoreSS.PosteriorOfCause("allergy", new string[] { "runny nose" }, new string[] { "watery eyes" })); AssertNear(1.0, quickscoreSS.PosteriorOfCause("cold", new string[] { "runny nose", "cough" }, new string[] { "watery eyes" })); AssertNear(0.042220484753702256, quickscoreSS.PosteriorOfCause("allergy", new string[] { "runny nose", "cough" }, new string[] { "watery eyes" })); Dictionary <string, double> posteriorOfEveryCauseSS = quickscoreSS.PosteriorOfEveryCause(new string[] { "runny nose", "cough" }, new string[] { "watery eyes" }); AssertNear(1.0, posteriorOfEveryCauseSS["cold"]); AssertNear(0.042220484753702256, posteriorOfEveryCauseSS["allergy"]); AssertNear(1, quickscoreSS.PosteriorOfCause("cold", new string[] { "cough" }, new string[] { })); quickscoreSS.SetLeak("cough", 1.0 / 300.0); AssertNear(0.237875288683606, quickscoreSS.PosteriorOfCause("cold", new string[] { "cough" }, new string[] { })); AssertNear(.01, quickscoreSS.PosteriorOfCause("cold", new string[] { }, new string[] { "watery eyes" })); Quickscore <double, int> quickscoreDL = Quickscore <double, int> .GetInstance(double.NaN); quickscoreDL.SetCause(3.1, .01); quickscoreDL.SetLink(3.1, 0, .1); quickscoreDL.SetLink(3.1, 2, .3); AssertNear(.01, quickscoreDL.PosteriorOfCause(3.1, new int[] { }, new int[] { })); AssertNear(1, quickscoreDL.PosteriorOfCause(3.1, new int[] { 0 }, new int[] { })); Dictionary <double, double> posteriorOfEveryCauseDL = quickscoreDL.PosteriorOfEveryCause(new int[] { }, new int[] { 2 }); quickscoreDL.SetLeak(0, .001); AssertNear(0.50475237618810087, quickscoreDL.PosteriorOfCause(3.1, new int[] { 0 }, new int[] { })); TestBigSize(); }
internal static void TestBigSize() { Random random = new Random(023029); Dictionary <int, bool> effectList; Quickscore <int, int> quickscore = GetRandomInstance(random, 600, 10, 10, .0001, out effectList); int[] effectListX = new int[effectList.Count]; effectList.Keys.CopyTo(effectListX, 0); for (int numberOfPositiveFindings = 0; numberOfPositiveFindings < 20; ++numberOfPositiveFindings) { int[] positiveFinding = GetRandomEffects(random, effectListX, numberOfPositiveFindings); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); quickscore.PosteriorOfEveryCause(positiveFinding, new int[] { }); stopwatch.Stop(); Console.WriteLine("{0}\t{1}", numberOfPositiveFindings, stopwatch.Elapsed); } }
internal static Quickscore <int, int> GetRandomInstance(Random random, int cCause, int cEffectsPerCause, int maxJumpBetweenEffects, double leakProbability, out Dictionary <int, bool> effectList) { Quickscore <int, int> quickscore = Quickscore <int, int> .GetInstance(int.MinValue); effectList = new Dictionary <int, bool>(); for (int cause = 0; cause < cCause; ++cause) { quickscore.SetCause(cause, random.NextDouble()); int effect = -1; for (int iEffect = 0; iEffect < cEffectsPerCause; ++iEffect) { effect += 1 + random.Next(maxJumpBetweenEffects); quickscore.SetLink(cause, effect, random.NextDouble()); if (!effectList.ContainsKey(effect)) { quickscore.SetLeak(effect, leakProbability); effectList.Add(effect, true); } } } return(quickscore); }