From cc0294c021e4c9c36588917bc33c00df2dbe129c Mon Sep 17 00:00:00 2001 From: regislebrun Date: Fri, 1 Nov 2024 18:05:54 +0100 Subject: [PATCH] Improved UserDefined Now a dedicated algorithm is used to compute Spearman correlation --- .../Uncertainty/Distribution/UserDefined.cxx | 45 +++++++++++++++++++ .../Distribution/openturns/UserDefined.hxx | 6 +++ 2 files changed, 51 insertions(+) diff --git a/lib/src/Uncertainty/Distribution/UserDefined.cxx b/lib/src/Uncertainty/Distribution/UserDefined.cxx index b683303bef..52b32fed60 100644 --- a/lib/src/Uncertainty/Distribution/UserDefined.cxx +++ b/lib/src/Uncertainty/Distribution/UserDefined.cxx @@ -406,6 +406,51 @@ void UserDefined::computeCovariance() const isAlreadyComputedCovariance_ = true; } +/* Compute the Spearman correlation of the distribution */ +CorrelationMatrix UserDefined::getSpearmanCorrelation() const +{ + const UnsignedInteger size = points_.getSize(); + const UnsignedInteger dimension = getDimension(); + Sample rank(size, dimension); + // Build the correct weighted ranks and compute its mean along the way + Point meanRank(dimension); + for (UnsignedInteger i = 0; i < dimension; ++i) + { + const Distribution marginalI(getMarginal(i)); + for (UnsignedInteger k = 0; k < size; ++k) + { + rank(k, i) = marginalI.computeCDF(points_(k, i)); + meanRank[i] += probabilities_[k] * rank(k, i); + } // k + } // i + // Then, the covariance of the rank + CorrelationMatrix spearman(dimension); + for (UnsignedInteger k = 0; k < size; ++k) + { + const Point xK(rank[k] - meanRank); + const Scalar pK = probabilities_[k]; + for (UnsignedInteger i = 0; i < dimension; ++i) + for (UnsignedInteger j = 0; j <= i; ++j) + spearman(i, j) += pK * xK[i] * xK[j]; + } // k + // Then, the correlation + Point std(dimension); + for (UnsignedInteger i = 0; i < dimension; ++i) + { + std[i] = std::sqrt(spearman(i, i)); + for (UnsignedInteger j = 0; j < i; ++j) + spearman(i, j) /= std[i] * std[j]; + spearman(i, i) = 1.0; + } + return spearman; +} + +/* Compute the Kendall concordance of the distribution */ +CorrelationMatrix UserDefined::getKendallTau() const +{ + return DistributionImplementation::getKendallTau(); +} + /* Parameters value and description accessor */ UserDefined::PointWithDescriptionCollection UserDefined::getParametersCollection() const { diff --git a/lib/src/Uncertainty/Distribution/openturns/UserDefined.hxx b/lib/src/Uncertainty/Distribution/openturns/UserDefined.hxx index 3a3b601a47..729f12e016 100644 --- a/lib/src/Uncertainty/Distribution/openturns/UserDefined.hxx +++ b/lib/src/Uncertainty/Distribution/openturns/UserDefined.hxx @@ -140,6 +140,12 @@ public: /** Tell if the distribution has independent copula */ Bool hasIndependentCopula() const override; + /** Get the Spearman correlation of the distribution */ + CorrelationMatrix getSpearmanCorrelation() const override; + + /** Get the Kendall concordance of the distribution */ + CorrelationMatrix getKendallTau() const override; + /** Method save() stores the object through the StorageManager */ void save(Advocate & adv) const override;