Skip to content

Commit

Permalink
1. Some tweaks for moments & counters
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Aug 4, 2024
1 parent 5c82ee3 commit f6dadbe
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 64 deletions.
3 changes: 2 additions & 1 deletion ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
1. Add `roc_curve` for making ROC curves, and corrresponsing test module
1. Add `eff_graph` for 1D historgams for creation of the efficiency graph
from the 1D-distribution.

1. Some tweaks for moments & counters

## Backward incompatible

1. `project`(&`draw`) for 2 and 3-dimession now follows the natural order of varibales:
Expand Down
55 changes: 19 additions & 36 deletions source/include/Ostap/Moments.h
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,9 @@ namespace Ostap
public:
// ======================================================================
/// add a single value
inline WMoment_& add ( const double /* x */ , const double w = 1 )
inline WMoment_& add
( const double /* x */ ,
const double w = 1 )
{
if ( !w ) { return *this ; } // ZERO weights are ignored
//
Expand Down Expand Up @@ -1478,8 +1480,6 @@ namespace Ostap
// ======================================================================
inline GeometricMean& operator+=( const double x ) { return add ( x ) ; }
inline GeometricMean& operator+=( const GeometricMean& x ) { return add ( x ) ; }
inline GeometricMean& operator*=( const double x ) { return add ( x ) ; }
inline GeometricMean& operator*=( const GeometricMean& x ) { return add ( x ) ; }
// ======================================================================
public:
// ======================================================================
Expand All @@ -1489,11 +1489,8 @@ namespace Ostap
public:
// ======================================================================
/// accumulate only positive entries
inline GeometricMean& add ( const double x )
{
if ( 0 < x ) { m_log.add ( std::log2 ( x ) ) ; }
return *this ;
}
GeometricMean& add ( const double x ) ;
/// sum of two counters
inline GeometricMean& add ( const GeometricMean& x )
{
m_log.add ( x.m_log ) ;
Expand Down Expand Up @@ -1559,7 +1556,7 @@ namespace Ostap
public:
// ======================================================================
/// accumulate only non-zero entries
HarmonicMean& add ( const double x ) ;
HarmonicMean& add ( const double x ) ;
/// add two counters togather
inline HarmonicMean& add ( const HarmonicMean& x )
{
Expand Down Expand Up @@ -1636,11 +1633,7 @@ namespace Ostap
public:
// ======================================================================
/// accumulate only positive entries
inline PowerMean& add ( const double x )
{
if ( 0 < x ) { m_pow.add ( std::pow ( x , m_p ) ) ; }
return *this ;
}
PowerMean& add ( const double x ) ;
/// add two counters togather if p is common
PowerMean& add ( const PowerMean& x ) ;
// ======================================================================
Expand Down Expand Up @@ -1724,15 +1717,7 @@ namespace Ostap
public:
// ======================================================================
/// accumulate only positive entries
inline LehmerMean& add ( const double x )
{
if ( 0 < x )
{
m_lp .add ( std::pow ( x , m_p ) ) ;
m_lpm1.add ( std::pow ( x , m_p - 1 ) ) ;
}
return *this ;
}
LehmerMean& add ( const double x ) ;
/// add two counters togather if p is common
LehmerMean& add ( const LehmerMean& x ) ;
// ======================================================================
Expand Down Expand Up @@ -1805,12 +1790,10 @@ namespace Ostap
public:
// ======================================================================
/// accumulate only positive entries
inline WGeometricMean& add ( const double x ,
const double w = 1 )
{
if ( 0 < x ) { m_log.add ( std::log2 ( x ) , w ) ; }
return *this ;
}
WGeometricMean& add
( const double x ,
const double w = 1 ) ;
// sum of to counters
inline WGeometricMean& add ( const WGeometricMean& x )
{
m_log.add ( x.m_log ) ;
Expand Down Expand Up @@ -1845,9 +1828,6 @@ namespace Ostap
Counter m_log {} ;
// ======================================================================
};

// ========================================================================

// ========================================================================
/** @class WHarmonicMean
* Calcualet the weighted harmonic mean
Expand Down Expand Up @@ -2251,15 +2231,18 @@ namespace Ostap
// ======================================================================
public:
// ======================================================================
/// accumulate only positive entries
/// accumulate only no-zero weights
inline WMinMaxValue& add
( const double x ,
const double w = 1 )
{
if ( !w ) { return *this ; } // ZERO weights are ignored
m_min = std::min ( m_min , x ) ;
m_max = std::max ( m_max , x ) ;
const unsigned long long sbefore = m_cnt.size() ;
m_cnt.add ( x , w ) ;
if ( m_cnt.size() != sbefore )
{
m_min = std::min ( m_min , x ) ;
m_max = std::max ( m_max , x ) ;
}
return *this ;
}
inline WMinMaxValue& add ( const WMinMaxValue& x )
Expand Down
102 changes: 75 additions & 27 deletions source/src/Moments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,20 @@ Ostap::Math::WLehmerMean::WLehmerMean
"Inconsistent structure of two conuters!" ,
"Ostap::Math::WLehmerMean" ) ;
}


// ===========================================================================
// GeometricMean::accumulate only positive entries
// ===========================================================================
Ostap::Math::GeometricMean&
Ostap::Math::GeometricMean::add
( const double x )
{
if ( 0 < x && !s_zero ( x ) ) { m_log.add ( std::log2 ( x ) ) ; }
return *this ;
}
// ===========================================================================
// HarmoinicMean:: accumulate only non-zero entries
// HarmonicMean:: accumulate only non-zero entries
// ===========================================================================
Ostap::Math::HarmonicMean&
Ostap::Math::HarmonicMean::add ( const double x )
Expand All @@ -202,7 +214,41 @@ Ostap::Math::HarmonicMean::add ( const double x )
return *this ;
}
// ===========================================================================
// WHarmoincMean:: accumulate only non-zero entries
// PowerMean::accumulate only positive entries
// ===========================================================================
Ostap::Math::PowerMean&
Ostap::Math::PowerMean::add
( const double x )
{
if ( ( 0 < x ) && !s_zero ( x ) ) { m_pow.add ( std::pow ( x , m_p ) ) ; }
return *this ;
}
// ===========================================================================
// LehmerMean: accumulate only positive entries
// ===========================================================================
Ostap::Math::LehmerMean&
Ostap::Math::LehmerMean::add ( const double x )
{
if ( ( 0 < x ) && !s_zero( x ) )
{
m_lp .add ( std::pow ( x , m_p ) ) ;
m_lpm1.add ( std::pow ( x , m_p - 1 ) ) ;
}
return *this ;
}
// ===========================================================================
// WGeometricMean:: accumulate only positive entries
// ===========================================================================
Ostap::Math::WGeometricMean&
Ostap::Math::WGeometricMean::add
( const double x ,
const double w )
{
if ( ( 0 < x ) && !s_zero ( x ) ) { m_log.add ( std::log2 ( x ) , w ) ; }
return *this ;
}
// ===========================================================================
// WHarmonicMean:: accumulate only non-zero entries
// ===========================================================================
Ostap::Math::WHarmonicMean&
Ostap::Math::WHarmonicMean::add
Expand All @@ -212,8 +258,32 @@ Ostap::Math::WHarmonicMean::add
if ( !s_zero ( x ) ) { m_inv.add ( 1/x , w ) ; }
return *this ;
}


// ===========================================================================
// WPowerMean:: accumulate only positive entries
// ===========================================================================
Ostap::Math::WPowerMean&
Ostap::Math::WPowerMean::add
( const double x ,
const double w )
{
if ( ( 0 < x ) && !s_zero ( x ) ) { m_pow.add ( std::pow ( x , m_p ) , w ) ; }
return *this ;
}
// ===========================================================================
// LehmerMean: accumulate only positive entries
// ===========================================================================
Ostap::Math::WLehmerMean&
Ostap::Math::WLehmerMean::add
( const double x ,
const double w )
{
if ( ( 0 < x ) && !s_zero ( x ) )
{
m_lp .add ( std::pow ( x , m_p ) , w ) ;
m_lpm1.add ( std::pow ( x , m_p - 1 ) , w ) ;
}
return *this ;
}
// ===========================================================================
// add two counters togather if p is common
// ===========================================================================
Expand All @@ -239,15 +309,6 @@ Ostap::Math::WPowerMean::add ( const Ostap::Math::WPowerMean& x )
return *this ;
}
// ===========================================================================
Ostap::Math::WPowerMean&
Ostap::Math::WPowerMean::add
( const double x ,
const double w )
{
if ( 0 < x && !s_zero ( w ) ) { m_pow.add ( std::pow ( x , m_p ) , w ) ; }
return *this ;
}
// ===========================================================================
// add two counters togather if p is common
// ===========================================================================
Ostap::Math::LehmerMean&
Expand All @@ -273,20 +334,7 @@ Ostap::Math::WLehmerMean::add
return *this ;
}
// ===========================================================================
// accumulate only positive entries with non-zero weight
// ===========================================================================
Ostap::Math::WLehmerMean&
Ostap::Math::WLehmerMean::add
( const double x ,
const double w )
{
if ( ( 0 < x ) && !s_zero ( x ) && !s_zero ( w ) )
{
m_lp .add ( std::pow ( x , m_p ) , w ) ;
m_lpm1.add ( std::pow ( x , m_p - 1 ) , w ) ;
}
return *this ;
}

// ===========================================================================
// default constructor
// ===========================================================================
Expand Down

0 comments on commit f6dadbe

Please sign in to comment.