public void DerivativeTest2() { double beta = 4.2; double constant = 0.57; double[] expected = { 0.230745, 0.197519, 0.157726, 0.119128, 0.0862579, 0.0605722, 0.0416275, 0.0281864, 0.0188941, 0.01258, 0.00833836 }; LogitLinkFunction target = new LogitLinkFunction(beta, constant); for (int i = 0; i < 11; i++) { double x = i / 10.0; double y = target.Inverse(x); double d1 = target.Derivative(x); double d2 = target.Derivative2(y); Assert.AreEqual(expected[i], d1, 1e-6); Assert.AreEqual(expected[i], d2, 1e-6); Assert.IsFalse(Double.IsNaN(d1)); Assert.IsFalse(Double.IsNaN(d2)); } }
public void DerivativeTest() { double[] expected = { 0.25, 0.249376, 0.247517, 0.244458, 0.240261, 0.235004, 0.228784, 0.221713, 0.21391, 0.2055, 0.196612 }; LogitLinkFunction target = new LogitLinkFunction(); for (int i = 0; i < 11; i++) { double x = i / 10.0; double y = target.Inverse(x); double d1 = target.Derivative(x); double d2 = target.Derivative2(y); Assert.AreEqual(expected[i], d1, 1e-6); Assert.AreEqual(expected[i], d2, 1e-6); Assert.IsFalse(Double.IsNaN(d1)); Assert.IsFalse(Double.IsNaN(d2)); } }
public static ILinkFunction Get(LinkFunctionType link) { ILinkFunction result = null; switch (link) { case LinkFunctionType.Absolute: result = new AbsoluteLinkFunction(); break; case LinkFunctionType.Cauchit: result = new CauchitLinkFunction(); break; case LinkFunctionType.Identity: result = new IdentityLinkFunction(); break; case LinkFunctionType.Inverse: result = new InverseLinkFunction(); break; case LinkFunctionType.InverseSquared: result = new InverseSquaredLinkFunction(); break; case LinkFunctionType.Logit: result = new LogitLinkFunction(); break; case LinkFunctionType.Log: result = new LogLinkFunction(); break; case LinkFunctionType.LogLog: result = new LogLogLinkFunction(); break; case LinkFunctionType.Probit: result = new ProbitLinkFunction(); break; case LinkFunctionType.Sin: result = new SinLinkFunction(); break; case LinkFunctionType.Threshold: result = new ThresholdLinkFunction(); break; } return(result); }
public void TestException() { var fn = new LogitLinkFunction(); double[] x = { 1, 2 }; fn.Evaluate(x); }
public void TestEvaluate() { var fn = new LogitLinkFunction(); double[] x = { 2 }; double y = fn.Evaluate(x); Assert.AreEqual(0.8807970779778823, y, AIFH.DefaultPrecision); }
public void LogitLinkFunctionConstructorTest() { LogitLinkFunction target = new LogitLinkFunction(); Assert.AreEqual(0, target.A); Assert.AreEqual(1, target.B); for (int i = 0; i < 11; i++) { double x = i / 10.0; double y = Math.Log(x) - Math.Log(1 - x); Assert.AreEqual(y, target.Function(x), 1e-10); Assert.AreEqual(x, target.Inverse(y), 1e-10); } }
public void LogitLinkFunctionConstructorTest1() { double beta = 3.14; double constant = 2.91; LogitLinkFunction target = new LogitLinkFunction(beta, constant); Assert.AreEqual(constant, target.A); Assert.AreEqual(beta, target.B); for (int i = 0; i < 11; i++) { double x = i / 10.0; double y = ((Math.Log(x) - Math.Log(1 - x)) - constant) / beta; Assert.AreEqual(y, target.Function(x), 1e-10); Assert.AreEqual(x, target.Inverse(y), 1e-10); } }
public void scores_probabilities_test() { double[][] input = { new double[] { 55, 0 }, // 0 - no cancer new double[] { 28, 0 }, // 0 new double[] { 65, 1 }, // 0 new double[] { 46, 0 }, // 1 - have cancer new double[] { 86, 1 }, // 1 new double[] { 86, 1 }, // 1 new double[] { 56, 1 }, // 1 new double[] { 85, 0 }, // 0 new double[] { 33, 0 }, // 0 new double[] { 21, 1 }, // 0 new double[] { 42, 1 }, // 1 }; double[] output = { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1 }; double[] weights = { 1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0 }; var teacher = new IterativeReweightedLeastSquares <LogisticRegression>() { Regularization = 0 }; var target = teacher.Learn(input, output, weights); LogitLinkFunction link = (LogitLinkFunction)target.Link; Assert.AreEqual(0, link.A); Assert.AreEqual(1, link.B); Assert.AreEqual(-2.4577464307294092, target.Intercept, 1e-8); Assert.AreEqual(-2.4577464307294092, target.Coefficients[0], 1e-8); Assert.AreEqual(0.020645118265359252, target.Coefficients[1], 1e-8); Assert.AreEqual(1.7678893101571855, target.Coefficients[2], 1e-8); // Test Scores, LogLikelihoods and Probability functions // https://github.com/accord-net/framework/issues/570 double[][] scoresAllSamples = target.Scores(input); double[][] logLikelihoodsAllSamples = target.LogLikelihoods(input); double[][] probabilitiesAllSamples = target.Probabilities(input); Assert.IsTrue(scoresAllSamples.IsEqual(Matrix.Apply(probabilitiesAllSamples, link.Function), rtol: 1e-5)); Assert.IsTrue(probabilitiesAllSamples.IsEqual(logLikelihoodsAllSamples.Exp())); Assert.IsTrue(probabilitiesAllSamples.Sum(dimension: 1).IsEqual(Vector.Ones(11), rtol: 1e-6)); bool[] decideAllSamples = target.Decide(input); double err = new ZeroOneLoss(output).Loss(decideAllSamples); Assert.AreEqual(0.18181818181818182, err, 1e-5); Assert.AreEqual(decideAllSamples, scoresAllSamples.ArgMax(dimension: 1).ToBoolean()); Assert.AreEqual(decideAllSamples.ToInt32(), logLikelihoodsAllSamples.ArgMax(dimension: 1)); Assert.AreEqual(decideAllSamples, probabilitiesAllSamples.ArgMax(dimension: 1).ToBoolean()); double[] scoreAllSamples = target.Score(input); Assert.AreEqual(scoreAllSamples, scoresAllSamples.GetColumn(1)); double[] logLikelihoodAllSamples = target.LogLikelihood(input); Assert.AreEqual(logLikelihoodAllSamples, logLikelihoodsAllSamples.GetColumn(1)); double[] probabilityAllSamples = target.Probability(input); Assert.AreEqual(probabilityAllSamples, probabilitiesAllSamples.GetColumn(1)); for (int i = 0; i < input.Length; i++) { double[] scoresOneSample = target.Scores(input[i]); Assert.AreEqual(scoresOneSample, scoresAllSamples[i]); double[] logLikelihoodsOneSample = target.LogLikelihoods(input[i]); Assert.AreEqual(logLikelihoodsOneSample, logLikelihoodsAllSamples[i]); double[] probabilitiesOneSample = target.Probabilities(input[i]); Assert.AreEqual(probabilitiesOneSample, probabilitiesAllSamples[i]); bool decideOneSample = target.Decide(input[i]); Assert.AreEqual(decideOneSample, decideAllSamples[i]); double scoreOneSample = target.Score(input[i]); Assert.AreEqual(scoreOneSample, scoreAllSamples[i]); double logLikelihoodOneSample = target.LogLikelihood(input[i]); Assert.AreEqual(logLikelihoodOneSample, logLikelihoodAllSamples[i]); double probabilityOneSample = target.Probability(input[i]); Assert.AreEqual(probabilityOneSample, probabilityAllSamples[i]); } bool[] decideScoresAllSamples = null; target.Scores(input, ref decideScoresAllSamples); bool[] decideLogLikelihoodsAllSamples = null; target.LogLikelihoods(input, ref decideLogLikelihoodsAllSamples); Assert.AreEqual(decideScoresAllSamples, decideLogLikelihoodsAllSamples); bool[] decideProbabilitiesAllSamples = null; target.Probabilities(input, ref decideProbabilitiesAllSamples); Assert.AreEqual(decideScoresAllSamples, decideProbabilitiesAllSamples); bool[] decideScoreAllSamples = null; target.Score(input, ref decideScoreAllSamples); Assert.AreEqual(decideScoreAllSamples, decideScoresAllSamples); bool[] decideLogLikelihoodAllSamples = null; target.LogLikelihood(input, ref decideLogLikelihoodAllSamples); Assert.AreEqual(decideScoreAllSamples, decideLogLikelihoodAllSamples); bool[] decideProbabilityAllSamples = null; target.Probability(input, ref decideProbabilityAllSamples); Assert.AreEqual(decideScoreAllSamples, decideProbabilityAllSamples); for (int i = 0; i < input.Length; i++) { bool decideScoresOneSample; target.Scores(input[i], out decideScoresOneSample); Assert.AreEqual(decideScoresOneSample, decideScoresAllSamples[i]); bool decideLogLikelihoodsOneSample; target.LogLikelihoods(input[i], out decideLogLikelihoodsOneSample); Assert.AreEqual(decideLogLikelihoodsOneSample, decideLogLikelihoodsAllSamples[i]); bool decideProbabilitiesOneSample; target.Probabilities(input[i], out decideProbabilitiesOneSample); Assert.AreEqual(decideProbabilitiesOneSample, decideProbabilitiesAllSamples[i]); bool decideScoreOneSample; target.Score(input[i], out decideScoreOneSample); Assert.AreEqual(decideScoreOneSample, decideScoreAllSamples[i]); bool decideLogLikelihoodOneSample; target.LogLikelihood(input[i], out decideLogLikelihoodOneSample); Assert.AreEqual(decideLogLikelihoodOneSample, decideLogLikelihoodAllSamples[i]); bool decideProbabilityOneSample; target.Probability(input: input[i], decision: out decideProbabilityOneSample); Assert.AreEqual(decideProbabilityOneSample, decideProbabilityAllSamples[i]); } //bool[][] decidesScoresAllSamples = null; target.Scores(input, ref decidesScoresAllSamples); //bool[][] decidesLogLikelihoodsAllSamples = null; target.LogLikelihoods(input, ref decidesLogLikelihoodsAllSamples); //bool[][] decidesProbabilitiesAllSamples = null; target.Probabilities(input, ref decidesProbabilitiesAllSamples); //bool[][] decidesScoreAllSamples = null; target.Score(input, ref decidesScoreAllSamples); //bool[][] decidesLogLikelihoodAllSamples = null; target.LogLikelihood(input, ref decidesLogLikelihoodAllSamples); //bool[][] decidesProbabilityAllSamples = null; target.Probability(input, ref decidesProbabilityAllSamples); }