[apps/regression] Clean partialDerivate of the models

This commit is contained in:
Léa Saviot
2020-05-13 14:22:16 +02:00
committed by LeaNumworks
parent 980a62da6c
commit 21a4139c19
9 changed files with 83 additions and 109 deletions

View File

@@ -55,24 +55,21 @@ double CubicModel::evaluate(double * modelCoefficients, double x) const {
}
double CubicModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 0) {
// Derivate: x^3
return x*x*x;
}
if (derivateCoefficientIndex == 1) {
// Derivate: x^2
return x*x;
}
if (derivateCoefficientIndex == 2) {
// Derivate: x
return x;
}
if (derivateCoefficientIndex == 3) {
// Derivate: 1
return 1;
}
assert(false);
return 0.0;
switch (derivateCoefficientIndex) {
case 0:
// Derivate with respect to a: x^3
return x*x*x;
case 1:
// Derivate with respect to b: x^2
return x*x;
case 2:
// Derivate with respect to c: x
return x;
default:
// Derivate with respect to d: 1
assert(derivateCoefficientIndex == 3);
return 1.0;
};
}
Expression CubicModel::expression(double * modelCoefficients) {

View File

@@ -86,18 +86,15 @@ void ExponentialModel::fit(Store * store, int series, double * modelCoefficients
}
double ExponentialModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
double a = modelCoefficients[0];
double b = modelCoefficients[1];
const double b = modelCoefficients[1];
if (derivateCoefficientIndex == 0) {
// Derivate: exp(b*x)
// Derivate with respect to a: exp(b*x)
return exp(b*x);
}
if (derivateCoefficientIndex == 1) {
// Derivate: a*x*exp(b*x)
return a*x*exp(b*x);
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 1);
// Derivate with respect to b: a*x*exp(b*x)
double a = modelCoefficients[0];
return a*x*exp(b*x);
}
}

View File

@@ -38,15 +38,12 @@ void LinearModel::fit(Store * store, int series, double * modelCoefficients, Poi
double LinearModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 0) {
// Derivate: x
// Derivate with respect to a: x
return x;
}
if (derivateCoefficientIndex == 1) {
// Derivate: 1;
return 1;
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 1);
// Derivate with respect to b: 1
return 1.0;
}
}

View File

@@ -33,16 +33,13 @@ double LogarithmicModel::levelSet(double * modelCoefficients, double xMin, doubl
double LogarithmicModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 0) {
// Derivate: ln(x)
assert(x >0);
// Derivate with respect to a: ln(x)
assert(x > 0);
return log(x);
}
if (derivateCoefficientIndex == 1) {
// Derivate: 1
return 1;
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 1);
// Derivate with respect to b: 1
return 1.0;
}
bool LogarithmicModel::dataSuitableForFit(Store * store, int series) const {

View File

@@ -61,21 +61,18 @@ double LogisticModel::partialDerivate(double * modelCoefficients, int derivateCo
double a = modelCoefficients[0];
double b = modelCoefficients[1];
double c = modelCoefficients[2];
double denominator = 1.0+a*exp(-b*x);
double denominator = 1.0 + a * exp(-b * x);
if (derivateCoefficientIndex == 0) {
// Derivate: exp(-b*x)*(-1 * c/(1.0+a*exp(-b*x))^2)
return -exp(-b*x) * c/(denominator * denominator);
// Derivate with respect to a: exp(-b*x)*(-1 * c/(1.0+a*exp(-b*x))^2)
return -exp(-b * x) * c / (denominator * denominator);
}
if (derivateCoefficientIndex == 1) {
// Derivate: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2)
return x*a*exp(-b*x)*c/(denominator * denominator);
// Derivate with respect to b: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2)
return x * a * exp(-b * x) * c / (denominator * denominator);
}
if (derivateCoefficientIndex == 2) {
// Derivate: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2)
return 1.0/denominator;
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 2);
// Derivate with respect to c: (-x)*a*exp(-b*x)*(-1/(1.0+a*exp(-b*x))^2)
return 1.0 / denominator;
}
void LogisticModel::specializedInitCoefficientsForFit(double * modelCoefficients, double defaultValue, Store * store, int series) const {
@@ -86,7 +83,7 @@ void LogisticModel::specializedInitCoefficientsForFit(double * modelCoefficients
* and c. Twice the standard vertical deviation is a rough estimate of c
* that is "close enough" to c to seed the coefficient, without being too
* dependent on outliers.*/
modelCoefficients[2] = 2.0*store->standardDeviationOfColumn(series, 1);
modelCoefficients[2] = 2.0 * store->standardDeviationOfColumn(series, 1);
}

View File

@@ -44,19 +44,16 @@ double PowerModel::partialDerivate(double * modelCoefficients, int derivateCoeff
double a = modelCoefficients[0];
double b = modelCoefficients[1];
if (derivateCoefficientIndex == 0) {
// Derivate: pow(x,b)
// Derivate with respect to a: pow(x,b)
return pow(x,b);
}
if (derivateCoefficientIndex == 1) {
assert(x >= 0);
/* We assume all xi are positive.
* For x = 0, a*pow(x,b) = 0, the partial derivate along b is 0
* For x > 0, a*pow(x,b) = a*exp(b*ln(x)), the partial derivate along b is
* ln(x)*a*pow(x,b) */
return x == 0 ? 0 : log(x)*a*pow(x, b);
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 1);
assert(x >= 0);
/* We assume all xi are positive.
* For x = 0, a*pow(x,b) = 0, the partial derivate with respect to b is 0
* For x > 0, a*pow(x,b) = a*exp(b*ln(x)), the partial derivate with respect
* to b is ln(x)*a*pow(x,b) */
return x == 0.0 ? 0.0 : log(x) * a * pow(x, b);
}
void PowerModel::fit(Store * store, int series, double * modelCoefficients, Poincare::Context * context) {

View File

@@ -47,19 +47,16 @@ double QuadraticModel::evaluate(double * modelCoefficients, double x) const {
double QuadraticModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 0) {
// Derivate: x^2
// Derivate with respect to a: x^2
return x*x;
}
if (derivateCoefficientIndex == 1) {
// Derivate: x
// Derivate with respect to b: x
return x;
}
if (derivateCoefficientIndex == 2) {
// Derivate: 1
return 1;
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 2);
// Derivate with respect to c: 1
return 1.0;
}
Expression QuadraticModel::expression(double * modelCoefficients) {

View File

@@ -64,28 +64,24 @@ double QuarticModel::evaluate(double * modelCoefficients, double x) const {
}
double QuarticModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 0) {
// Derivate: x^4
return x*x*x*x;
}
if (derivateCoefficientIndex == 1) {
// Derivate: x^3
return x*x*x;
}
if (derivateCoefficientIndex == 2) {
// Derivate: x^2
return x*x;
}
if (derivateCoefficientIndex == 3) {
// Derivate: x
return x;
}
if (derivateCoefficientIndex == 4) {
// Derivate: 1
return 1;
}
assert(false);
return 0.0;
switch (derivateCoefficientIndex) {
case 0:
// Derivate with respect to a: x^4
return x*x*x*x;
case 1:
// Derivate with respect to b: x^3
return x*x*x;
case 2:
// Derivate with respect to c: x^2
return x*x;
case 3:
// Derivate with respect to d: x
return x;
default:
assert(derivateCoefficientIndex == 4);
// Derivate with respect to e: 1
return 1.0;
};
}
Expression QuarticModel::expression(double * modelCoefficients) {

View File

@@ -46,28 +46,27 @@ double TrigonometricModel::evaluate(double * modelCoefficients, double x) const
}
double TrigonometricModel::partialDerivate(double * modelCoefficients, int derivateCoefficientIndex, double x) const {
if (derivateCoefficientIndex == 3) {
// Derivate with respect to d: 1
return 1.0;
}
double a = modelCoefficients[0];
double b = modelCoefficients[1];
double c = modelCoefficients[2];
double radianX = x * toRadians(Poincare::Preferences::sharedPreferences()->angleUnit());
if (derivateCoefficientIndex == 0) {
// Derivate: sin(b*x+c)
return sin(b*radianX+c);
// Derivate with respect to a: sin(b*x+c)
return sin(b * radianX + c);
}
if (derivateCoefficientIndex == 1) {
// Derivate: x*a*cos(b*x+c);
return radianX*a*cos(b*radianX+c);
// Derivate with respect to b: x*a*cos(b*x+c);
return radianX * a * cos(b * radianX + c);
}
if (derivateCoefficientIndex == 2) {
// Derivate: a*cos(b*x+c)
return a*cos(b*radianX+c);
}
if (derivateCoefficientIndex == 3) {
// Derivate: 1
return 1.0;
}
assert(false);
return 0.0;
assert(derivateCoefficientIndex == 2);
// Derivatewith respect to c: a*cos(b*x+c)
return a * cos(b * radianX + c);
}
void TrigonometricModel::specializedInitCoefficientsForFit(double * modelCoefficients, double defaultValue, Store * store, int series) const {