mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-18 16:27:34 +01:00
[apps/regression] Clean partialDerivate of the models
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user