mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#3302 Weighted Geometric Mean Calculator. Improved algorithm
This commit is contained in:
parent
26b3065974
commit
2c5c007048
@ -26,7 +26,7 @@
|
|||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RiaWeightedGeometricMeanCalculator::RiaWeightedGeometricMeanCalculator()
|
RiaWeightedGeometricMeanCalculator::RiaWeightedGeometricMeanCalculator()
|
||||||
: m_aggregatedWeightedValue(1.0)
|
: m_aggregatedWeightedValue(0.0)
|
||||||
, m_aggregatedWeight(0.0)
|
, m_aggregatedWeight(0.0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ void RiaWeightedGeometricMeanCalculator::addValueAndWeight(double value, double
|
|||||||
CVF_ASSERT(weight >= 0.0);
|
CVF_ASSERT(weight >= 0.0);
|
||||||
|
|
||||||
// This can be a very big number, consider other algorithms if that becomes a problem
|
// This can be a very big number, consider other algorithms if that becomes a problem
|
||||||
m_aggregatedWeightedValue *= std::pow(value, weight);
|
m_aggregatedWeightedValue += (std::log(value) * weight);
|
||||||
m_aggregatedWeight += weight;
|
m_aggregatedWeight += weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ double RiaWeightedGeometricMeanCalculator::weightedMean() const
|
|||||||
{
|
{
|
||||||
if (m_aggregatedWeight > 1e-7)
|
if (m_aggregatedWeight > 1e-7)
|
||||||
{
|
{
|
||||||
return std::pow(m_aggregatedWeightedValue, 1 / m_aggregatedWeight);
|
return std::exp(m_aggregatedWeightedValue / m_aggregatedWeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0.0;
|
return 0.0;
|
||||||
|
@ -33,6 +33,30 @@ TEST(RiaWeightedGeometricMeanCalculator, BasicUsage)
|
|||||||
);
|
);
|
||||||
|
|
||||||
EXPECT_DOUBLE_EQ(5.0, calc.aggregatedWeight());
|
EXPECT_DOUBLE_EQ(5.0, calc.aggregatedWeight());
|
||||||
EXPECT_DOUBLE_EQ(expectedValue, calc.weightedMean());
|
EXPECT_NEAR(expectedValue, calc.weightedMean(), 1e-10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
TEST(RiaWeightedGeometricMeanCalculator, BigValues)
|
||||||
|
{
|
||||||
|
RiaWeightedGeometricMeanCalculator calc;
|
||||||
|
|
||||||
|
std::vector<double> values{ 3000000.0, 6000000.0, 1250000, 2200000 };
|
||||||
|
std::vector<double> weights{ 1.5, 3.5, 7, 5 };
|
||||||
|
|
||||||
|
for (size_t i = 0; i < values.size(); i++)
|
||||||
|
{
|
||||||
|
calc.addValueAndWeight(values[i], weights[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
double expectedValue = std::pow(
|
||||||
|
std::pow(3000000.0, 1.5) * std::pow(6000000.0, 3.5) * std::pow(1250000.0, 7) * std::pow(2200000.0, 5),
|
||||||
|
1 / (1.5 + 3.5 + 7 + 5)
|
||||||
|
);
|
||||||
|
|
||||||
|
EXPECT_DOUBLE_EQ(17.0, calc.aggregatedWeight());
|
||||||
|
EXPECT_NEAR(expectedValue, calc.weightedMean(), 1e-8);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user