Skip to content

Commit

Permalink
add rank-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Nov 28, 2024
1 parent 69cf3d5 commit f0f63ce
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
31 changes: 29 additions & 2 deletions source/include/Ostap/ECDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ namespace Ostap
// ======================================================================
/// access to data
inline const Data& data () const { return m_data ; }
// access to data
inline double data ( const unsigned int index ) const
{ return index < m_data.size() ? m_data[index] : m_data.back() ; }
// ======================================================================
/// complementary?
inline bool complementary () const { return m_complementary ; }
Expand Down Expand Up @@ -200,9 +203,11 @@ namespace Ostap
public:
// ======================================================================
/// The actual type of entry: (y,w)
typedef std::pair<double,double> Entry ;
/// internal storage
typedef std::pair<double,double> Entry ;
/// the actual type of data
typedef std::vector<Entry> Data ;
/// the actual type of indices
typedef ECDF::Indices Indices ;
// ======================================================================
public:
// ======================================================================
Expand Down Expand Up @@ -312,6 +317,10 @@ namespace Ostap
inline double xmin () const { return m_data.front ().first ; }
/// maximal x-value
inline double xmax () const { return m_data.back ().first ; }
// ======================================================================
/// get the abscissa value with the given index
inline const Entry& operator[] ( const unsigned int index ) const
{ return index < m_data.size() ? m_data[index] : m_data.back() ; }
// ======================================================================
public:
// ======================================================================
Expand Down Expand Up @@ -344,6 +353,24 @@ namespace Ostap
/// calculate \f$ \sum_i w^2_i \f$
inline double calc_sumw2 () const { return calc_sumw2 ( m_data.size() ) ; }
// ======================================================================
public:
// ======================================================================
/** number of elements that are less or equal to x
* "rank of x" in the ordered sample
*/
inline Data::size_type rank ( const double x ) const
{ return std::upper_bound ( m_data.begin () ,
m_data.end () ,
Entry ( x , 1.0 ) ,
[] ( const Entry& e1 ,
const Entry& e2 ) -> bool
{ return e1.first < e2.first ; } ) - m_data.begin() ; }
// ======================================================================
/// get ranks for all elements from another sample
ECDF::Indices ranks ( const ECDF& sample ) const ;
/// get ranks for all elements from another sample
ECDF::Indices ranks ( const WECDF& sample ) const ;
// ======================================================================
private:
// ======================================================================
void sort_me () ;
Expand Down
44 changes: 44 additions & 0 deletions source/src/ECDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,50 @@ Ostap::Math::WECDF::estimate ( const double x ) const
Ostap::Math::binomEff2 ( acc , rej ) :
Ostap::Math::binomEff2 ( rej , acc ) ;
}
// ============================================================================
// get ranks of the elements in the (pooled) sample
// ============================================================================
Ostap::Math::WECDF::Indices
Ostap::Math::WECDF::ranks
( const Ostap::Math::ECDF& sample ) const
{
const Data::size_type N = size() ;
// fill outptut array with N
Indices result ( sample.size () , N ) ;
Data::size_type NS = sample.size() ;
for ( Data::size_type i = 0 ; i < NS ; ++i )
{
Data::size_type r = rank ( sample.data ( i ) ) ;
result [ i ] = r ;
// try to be a bit more efficient, the rest of array is filled with N
if ( N <= r ) { break ; }
}
return result ;
}
// ============================================================================
// get ranks of the elements in the (pooled) sample
// ============================================================================
Ostap::Math::WECDF::Indices
Ostap::Math::WECDF::ranks
( const Ostap::Math::WECDF& sample ) const
{
const Data::size_type N = size() ;
// fill output array with N
Indices result ( sample.size () , N ) ;
Data::size_type NS = sample.size() ;
for ( Data::size_type i = 0 ; i < NS ; ++i )
{
Data::size_type r = rank ( sample.data ( i ) ) ;
result [ i ] = r ;
// try to be a bit more efficient, the rest of array is filled with N
if ( N <= r ) { break ; }
}
return result ;
}
// ============================================================================




// ============================================================================
// The END
Expand Down

0 comments on commit f0f63ce

Please sign in to comment.