Skip to content

Commit

Permalink
revised StartedBoxCox transformation, added scaling_factor
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianlussana committed Oct 2, 2024
1 parent 4854d6f commit ef9144b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions include/gridpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2335,19 +2335,20 @@ namespace gridpp {
private:
float mThreshold;
};
/** Started Box-Cox transformation */
/** Started Box-Cox transformation. No transformation between 0 and scaling_factor, then Box-Cox (like) transformation with parameter equal to threshold */
class StartedBoxCox : public Transform {
public:
/** Initialize started Box-Cox transform
* @param threshold started Box-Cox parameter
*/
StartedBoxCox(float threshold);
StartedBoxCox(float threshold, float scaling_factor);
using Transform::forward;
using Transform::backward;
float forward(float value) const;
float backward(float value) const;
private:
float mThreshold;
float mScaling;
};
/** Gamma transformation. Transforms values to cdf from a gamma distribution and subsequantly
* extracts the cdf from a standard normal distribution. */
Expand Down
20 changes: 12 additions & 8 deletions src/api/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,32 @@ float gridpp::BoxCox::backward(float value) const {
rValue = 0;
return rValue;
}
gridpp::StartedBoxCox::StartedBoxCox(float threshold) : mThreshold(threshold) {

gridpp::StartedBoxCox::StartedBoxCox(float threshold, float scaling_factor) : mThreshold(threshold), mScaling(scaling_factor) {
// No transformation between 0 and scaling_factor, then Box-Cox transformation with parameter equal to threshold
if(!gridpp::is_valid(threshold) || threshold <= 0)
throw std::invalid_argument("threshold parameter must be > 0 in the started Box-Cox distribution");
if(!gridpp::is_valid(scaling_factor) || scaling_factor <= 0)
throw std::invalid_argument("Scaling factor parameter must be > 0 in the started Box-Cox distribution");
}
float gridpp::StartedBoxCox::forward(float value) const {
if(!gridpp::is_valid(value) || mThreshold <= 0)
return gridpp::MV;
if(value <= 0)
if(value < 0)
value = 0;
if(value <= 1)
if(value <= mScaling)
return value;
else
return (1 + (((pow(value, mThreshold)) - 1) / mThreshold));
return mScaling * (1 + (((pow(value / mScaling, mThreshold)) - 1) / mThreshold));
}
float gridpp::StartedBoxCox::backward(float value) const {
if(!gridpp::is_valid(value) || mThreshold <= 0)
return gridpp::MV;
float rValue = 0;
if(value <= 1)
if(value <= mScaling)
rValue = value;
else
rValue = pow( 1 + mThreshold * (value-1), 1 / mThreshold);
if(rValue <= 0)
rValue = mScaling * pow( 1 + mThreshold / mScaling * (value-mScaling), 1 / mThreshold);
if(rValue < 0)
rValue = 0;
return rValue;
}
Expand Down

0 comments on commit ef9144b

Please sign in to comment.