public static void TestLogisticRegression() { const int seed = 39; Rand.Restart(seed); const int d = 10; const int n = 100; const int epIter = 10; Vector w = Vector.Zero(d); Rand.Normal(Vector.Zero(d), PositiveDefiniteMatrix.Identity(d), w); double b = Rand.Normal(0, 1); // double b= 0; Vector[] X; bool[] Y; GenData(n, w, b, out X, out Y); Console.Write("Y: "); StringUtils.PrintArray(Y); VectorGaussian wPost; Gaussian biasPost; Type logisticOp = typeof(KEPLogisticOp); // Type logisticOp = typeof(LogisticOp2); string factorOpPath = Config.PathToFactorOperator( // "serialFactorOp_fm_kgg_joint_irf500_orf1000_n400_iter5_sf1_st20_ntr5000.mat" "serialFactorOp_fm_kgg_joint_irf500_orf1000_proj_n400_iter5_sf1_st20_ntr5000.mat" ); KEPLogisticOpInstance opIns = KEPLogisticOpInstance.LoadLogisticOpInstance(factorOpPath); opIns.SetPrintTrueMessages(true); OpControl.Add(typeof(KEPLogisticOp), opIns); InferCoefficients(X, Y, out wPost, out biasPost, epIter, logisticOp); //print Console.WriteLine("n: {0}", n); Console.WriteLine("d: {0}", d); int t = Y.Sum(o => o ? 1 : 0); Console.WriteLine("number of true: {0}", t); Console.WriteLine("True bias: {0}", b); Console.WriteLine("Inferred bias: {0}", biasPost); Console.WriteLine("True w: {0}", w); Console.WriteLine("Inferred w: "); Console.WriteLine(wPost); }
public static void TestLogisticRegressionNoBias() { const int seed = 2; Rand.Restart(seed); const int d = 10; const int n = 300; const int epIter = 10; Vector w = Vector.Zero(d); Rand.Normal(Vector.Zero(d), PositiveDefiniteMatrix.Identity(d), w); double b = 0; // combine the bias term into W Vector[] X; bool[] Y; GenData(n, w, b, out X, out Y); Console.Write("Y: "); StringUtils.PrintArray(Y); VectorGaussian wPost; // string factorOpPath = Config.PathToFactorOperator( // // "serialFactorOp_fm_kgg_joint_irf500_orf1000_n400_iter5_sf1_st20_ntr5000.mat" // "serialFactorOp_fm_kgg_joint_irf500_orf1000_proj_n400_iter5_sf1_st20_ntr5000.mat" // ); // KEPLogisticOpInstance opIns = KEPLogisticOpInstance.LoadLogisticOpInstance(factorOpPath); // opIns.SetPrintTrueMessages(true); // OpControl.Add(typeof(KEPLogisticOp), opIns); // Type logisticOp = typeof(KEPLogisticOp); LogisticOpRecords records = new LogisticOpRecords(); OpControl.Add(typeof(KEPOnlineLogisticOp), new KEPOnlineISLogisticOpIns(records)); Type logisticOp = typeof(KEPOnlineLogisticOp); // Type logisticOp = typeof(LogisticOp2); InferCoefficientsNoBias(X, Y, out wPost, epIter, logisticOp); //print Console.WriteLine("n: {0}", n); Console.WriteLine("d: {0}", d); int t = Y.Sum(o => o ? 1 : 0); Console.WriteLine("number of true: {0}", t); Console.WriteLine("True bias: {0}", b); // Vector meanW = wPost.GetMean(); Console.WriteLine("True w: {0}", w); Console.WriteLine("Inferred w: "); Console.WriteLine(wPost); // write the records to a file string fname = string.Format("rec_onlinekep_is_logistic_iter{0}_n{1}.mat", epIter, n); string recordPath = Config.PathToSavedFile(fname); var extra = new Dictionary <string, object>(); // MatlabWriter cannot write int extra.Add("d", (double)d); extra.Add("n", (double)n); extra.Add("epIter", (double)epIter); extra.Add("trueW", w); extra.Add("X", MatrixUtils.StackColumns(X)); extra.Add("Y", MatrixUtils.ToDouble(Y)); records.WriteRecords(recordPath, extra); }