From 0e6e844d3bf85da450c930e1b9742a50b9538f14 Mon Sep 17 00:00:00 2001 From: Cristian Lussana Date: Wed, 18 Sep 2024 15:01:10 +0200 Subject: [PATCH] added Transformation StartedBoxCox --- include/gridpp.h | 14 ++++++++++++++ src/api/transform.cpp | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/gridpp.h b/include/gridpp.h index 6cef35ff..03e1cd70 100644 --- a/include/gridpp.h +++ b/include/gridpp.h @@ -2286,6 +2286,20 @@ namespace gridpp { private: float mThreshold; }; + /** Started Box-Cox transformation */ + class StartedBoxCox : public Transform { + public: + /** Initialize started Box-Cox transform + * @param threshold started Box-Cox parameter + */ + StartedBoxCox(float threshold); + using Transform::forward; + using Transform::backward; + float forward(float value) const; + float backward(float value) const; + private: + float mThreshold; + }; /** Gamma transformation. Transforms values to cdf from a gamma distribution and subsequantly * extracts the cdf from a standard normal distribution. */ class Gamma : public Transform { diff --git a/src/api/transform.cpp b/src/api/transform.cpp index 682b7735..3d54b08a 100644 --- a/src/api/transform.cpp +++ b/src/api/transform.cpp @@ -123,6 +123,28 @@ float gridpp::BoxCox::backward(float value) const { rValue = 0; return rValue; } +float gridpp::StartedBoxCox::forward(float value) const { + if(!gridpp::is_valid(value) || mThreshold <= 0) + return gridpp::MV; + if(value <= 0) + value = 0; + if(value <= 1) + return value; + else + return (1 + (((pow(value, 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) + rValue = value; + else + rValue = pow( 1 + mThreshold * (value-1), 1 / mThreshold); + if(rValue <= 0) + rValue = 0; + return rValue; +} gridpp::Gamma::Gamma(float shape, float scale, float tolerance) : m_gamma_dist(1, 1), m_norm_dist(), m_tolerance(tolerance) { // Initialize the gamma distribution to something that works, and then overwrite it so that // we can check for argument errors gracefully