static void Assumption() { //int[] X1 = new int[] { 1, 1, 2, 2, 2, 2, 1, 2 }; //int[] X2 = new int[] { 2, 1, 1, 2, 1, 1, 2, 2 }; int[] X1 = new int[] { 1, 1, 1, 1, 2, 2, 2, 2 }; int[] X2 = new int[] { 1, 1, 1, 1, 2, 2, 2, 2 }; int numberOfX1Is1 = 0; int numberOfX1Is2 = 0; int numberOfX2Is1 = 0; int numberOfX2Is2 = 0; int numberOfX1Is1X2Is1 = 0; int numberOfX1Is2X2Is1 = 0; int numberOfX1Is1X2Is2 = 0; int numberOfX1Is2X2Is2 = 0; double N = Convert.ToDouble(X1.Length); for (int i = 0; i < X1.Length; ++i) { if (X1[i] == 1) { ++numberOfX1Is1; if (X2[i] == 1) { ++numberOfX2Is1; ++numberOfX1Is1X2Is1; } else { ++numberOfX2Is2; ++numberOfX1Is1X2Is2; } } else { ++numberOfX1Is2; if (X2[i] == 1) { ++numberOfX2Is1; ++numberOfX1Is2X2Is1; } else { ++numberOfX2Is2; ++numberOfX1Is2X2Is2; } } } double MI = (numberOfX1Is1X2Is1 / N) * Math.Log((numberOfX1Is1X2Is1 * N) / (numberOfX1Is1 * numberOfX2Is1), 2) //+ (numberOfX1Is1X2Is2 / N) * Math.Log((numberOfX1Is1X2Is2 * N) / (numberOfX1Is1 * numberOfX2Is2), 2) //+ (numberOfX1Is2X2Is1 / N) * Math.Log((numberOfX1Is2X2Is1 * N) / (numberOfX1Is2 * numberOfX2Is1), 2) + (numberOfX1Is2X2Is2 / N) * Math.Log((numberOfX1Is2X2Is2 * N) / (numberOfX1Is2 * numberOfX2Is2), 2); double X = SpecialFunction.chisqc(1, 2 * N * MI); //(右尾面积)越小越有联系(不独立),越大越独立 }
public bool IsAWitnessByMI(IList <Label> witness, int group, double threshold)//BN用,PCAlgorithm { double MI = 0; double m = 0; for (int i = 0; i < Math.Pow(2, witness.Count); ++i) { //初始化 int numberOfXTrue = 0; int numberOfXFalse = 0; int numberOfYTrue = 0; int numberOfYFalse = 0; int numberOfXTrueYTrue = 0; int numberOfXTrueYFalse = 0; int numberOfXFalseYTrue = 0; int numberOfXFalseAYFalse = 0; int N = 0; int numberOfZ = 0; Labelset labelset = new Labelset(witness, i); //统计数量信息 foreach (Sentence sentence in Variable.Sentences) { foreach (Annotation annotation in sentence.AnnotaitonGroups[group].AnnotatorAnnotationDic.Values) { ++N; if (annotation.IsAccordingToLabelset(labelset)) { ++numberOfZ; if (annotation.Labels[First]) { ++numberOfXTrue; if (annotation.Labels[Second]) { ++numberOfYTrue; ++numberOfXTrueYTrue; } else { ++numberOfYFalse; ++numberOfXTrueYFalse; } } else { ++numberOfXFalse; if (annotation.Labels[Second]) { ++numberOfYTrue; ++numberOfXFalseYTrue; } else { ++numberOfYFalse; ++numberOfXFalseAYFalse; } } } } } MI += getConditionalMI(numberOfXTrue, numberOfXFalse, numberOfYTrue, numberOfYFalse, numberOfXTrueYTrue, numberOfXTrueYFalse, numberOfXFalseYTrue, numberOfXFalseAYFalse, numberOfZ, N); m = N; } return(SpecialFunction.chisqc(Math.Pow(2, witness.Count), 2 * m * MI) >= threshold);//(右尾面积)越小越有联系(不独立),越大越独立 }