IMP logo
IMP Reference Guide  2.20.2
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-2022 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 
42  double feature) const override {
43  double exp_sum = 0;
44  double derv_num = 0;
45  double weight_exp;
46  DerivativePair fout;
47  for (unsigned int i = 0; i < funcs_.size(); ++i) {
48  fout = funcs_[i]->evaluate_with_derivative(feature);
49  weight_exp = weights_[i] * std::exp(-fout.first / denom_);
50  exp_sum += weight_exp;
51  derv_num += weight_exp * fout.second;
52  }
53  return DerivativePair(-std::log(exp_sum) * denom_, derv_num / exp_sum);
54  }
55 
56  virtual double evaluate(double feature) const override {
57  double exp_sum = 0;
58  for (unsigned int i = 0; i < funcs_.size(); ++i) {
59  exp_sum += weights_[i] * std::exp(-funcs_[i]->evaluate(feature) / denom_);
60  }
61  return -std::log(exp_sum) * denom_;
62  }
63 
64  //! Get the number of functions
65  unsigned int get_function_number() {
66  return funcs_.size();
67  }
68 
69  //! Set the function weights
70  void set_weights(Floats weights) {
71  IMP_USAGE_CHECK(weights.size() == get_function_number(),
72  "Number of weights and functions must match.");
73  weights_ = weights;
74  }
75 
76  //! Get the function weights
77  Floats get_weights() { return weights_; }
78 
79  //! Get function weight at index
80  double get_weight(unsigned int i) const {
81  IMP_USAGE_CHECK(i < weights_.size(), "Invalid weight index");
82  return weights_[i];
83  }
84 
85  //! Get function at index
86  UnaryFunction* get_function(unsigned int i) {
87  IMP_USAGE_CHECK(i < get_function_number(), "Invalid function index");
88  return funcs_[i];
89  }
90 
91  //! Set the denominator of the exponential
92  void set_denominator(double denom) {
93  IMP_USAGE_CHECK(denom != 0.,
94  "Exponential denominator must be nonzero.");
95  denom_ = denom;
96  }
97 
98  //! Get the denominator of the exponential
99  double get_denominator() { return denom_; }
100 
102 
103  private:
104  UnaryFunctions funcs_;
105  Floats weights_;
106  Float denom_;
107 };
108 
109 IMPCORE_END_NAMESPACE
110 
111 #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.
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 DerivativePair evaluate_with_derivative(double feature) const override
Calculate score and derivative with respect to the given feature.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
#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:27
virtual double evaluate(double feature) const override
Calculate score with respect to the given feature.