IMP logo
IMP Reference Guide  2.13.0
The Integrative Modeling Platform
WeightedSumOfExponential.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/WeightedSumOfExponential.h
3  * \brief Negative logarithm of weighted sum of negative exponential of unary functions.
4  *
5  * Copyright 2007-2020 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCORE_WEIGHTED_SUM_OF_EXPONENTIAL_H
9 #define IMPCORE_WEIGHTED_SUM_OF_EXPONENTIAL_H
10 
11 #include <IMP/core/core_config.h>
12 #include <IMP/UnaryFunction.h>
13 #include <cmath>
14 
15 IMPCORE_BEGIN_NAMESPACE
16 
17 //! Negative logarithm of weighted sum of negative exponential of unary functions.
18 /** Given unary functions \f$ f_i(x) \f$ and weights \f$ w_i \f$, compute the function
19  \f[ F(x) = -d \log\left[ \sum_i{ \left( w_i e^{-f_i(x) / d} \right) } \right] ,\f]
20  where \f$ d \f$ is the denominator of the exponential.
21  This is used when the functions \f$ f_i(x) \f$ are scores (\f$ -\log(p) \f$), and the
22  desired score \f$ F(x) \f$ is the score resulting from the weighted convolution of
23  their probability distributions.
24  \see WeightedSum
25  */
27  public:
28  /** Create with the functions and their respective weights */
30  Floats weights,
31  Float denom = 1.0) : funcs_(funcs), weights_(weights), denom_(denom) {
32  IMP_USAGE_CHECK(weights.size() == funcs.size(),
33  "Number of functions and weights must match.");
34  IMP_USAGE_CHECK(funcs.size() > 1,
35  "More than one function and weight must be provided.");
36  IMP_USAGE_CHECK(denom != 0.,
37  "Exponential denominator must be nonzero.");
38 
39  }
40 
41  virtual DerivativePair evaluate_with_derivative(double feature) const {
42  double exp_sum = 0;
43  double derv_num = 0;
44  double weight_exp;
45  DerivativePair fout;
46  for (unsigned int i = 0; i < funcs_.size(); ++i) {
47  fout = funcs_[i]->evaluate_with_derivative(feature);
48  weight_exp = weights_[i] * std::exp(-fout.first / denom_);
49  exp_sum += weight_exp;
50  derv_num += weight_exp * fout.second;
51  }
52  return DerivativePair(-std::log(exp_sum) * denom_, derv_num / exp_sum);
53  }
54 
55  virtual double evaluate(double feature) const {
56  double exp_sum = 0;
57  for (unsigned int i = 0; i < funcs_.size(); ++i) {
58  exp_sum += weights_[i] * std::exp(-funcs_[i]->evaluate(feature) / denom_);
59  }
60  return -std::log(exp_sum) * denom_;
61  }
62 
63  //! Get the number of functions
64  unsigned int get_function_number() {
65  return funcs_.size();
66  }
67 
68  //! Set the function weights
69  void set_weights(Floats weights) {
70  IMP_USAGE_CHECK(weights.size() == get_function_number(),
71  "Number of weights and functions must match.");
72  weights_ = weights;
73  }
74 
75  //! Get the function weights
76  Floats get_weights() { return weights_; }
77 
78  //! Get function weight at index
79  double get_weight(unsigned int i) const {
80  IMP_USAGE_CHECK(i < weights_.size(), "Invalid weight index");
81  return weights_[i];
82  }
83 
84  //! Get function at index
85  UnaryFunction* get_function(unsigned int i) {
86  IMP_USAGE_CHECK(i < get_function_number(), "Invalid function index");
87  return funcs_[i];
88  }
89 
90  //! Set the denominator of the exponential
91  void set_denominator(double denom) {
92  IMP_USAGE_CHECK(denom != 0.,
93  "Exponential denominator must be nonzero.");
94  denom_ = denom;
95  }
96 
97  //! Get the denominator of the exponential
98  double get_denominator() { return denom_; }
99 
101 
102  private:
103  UnaryFunctions funcs_;
104  Floats weights_;
105  Float denom_;
106 };
107 
108 IMPCORE_END_NAMESPACE
109 
110 #endif /* IMPCORE_WEIGHTED_SUM_OF_EXPONENTIAL_H */
double get_weight(unsigned int i) const
Get function weight at index.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
WeightedSumOfExponential(UnaryFunctions funcs, Floats weights, Float denom=1.0)
Single variable function.
virtual DerivativePair evaluate_with_derivative(double feature) const
Calculate score and derivative with respect to the given feature.
Floats get_weights()
Get the function weights.
void set_denominator(double denom)
Set the denominator of the exponential.
UnaryFunction * get_function(unsigned int i)
Get function at index.
Negative logarithm of weighted sum of negative exponential of unary functions.
double get_denominator()
Get the denominator of the exponential.
void set_weights(Floats weights)
Set the function weights.
virtual double evaluate(double feature) const
Calculate score with respect to the given feature.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:23
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
unsigned int get_function_number()
Get the number of functions.
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:25