Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
prob_util.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/prob_util.hh
11 /// @author Christopher Miles (cmiles@uw.edu)
12 
13 #ifndef INCLUDED_NUMERIC_PROB_UTIL_hh
14 #define INCLUDED_NUMERIC_PROB_UTIL_hh
15 
16 // C/C++ headers
17 #include <iostream>
18 
19 // Utility headers
20 #include <utility/vector1.hh>
21 
22 /// A collection of functions for working with probabilities
23 namespace numeric {
24 
25 /// @brief Returns the sum of all elements on the range [first, last)
26 template <class InputIterator>
27 double sum(InputIterator first, InputIterator last) {
28  double sum = 0;
29  for ( ; first != last; ++first ) {
30  sum += *first;
31  }
32  return sum;
33 }
34 
35 /// @brief Normalizes elements on the range [first, last)
36 template <class InputIterator>
37 void normalize(InputIterator first, InputIterator last) {
38  const double div = sum(first, last);
39  for ( ; first != last; ++first ) {
40  *first /= div;
41  }
42 }
43 
44 /// @brief Converts pdf to cdf
45 template <class RandomAccessIterator>
46 void cumulative(RandomAccessIterator first, RandomAccessIterator last) {
47  normalize(first, last);
48  for ( RandomAccessIterator i = first + 1; i != last; ++i ) {
49  *i += *(i - 1);
50  }
51 }
52 
53 /// @brief Multiplies two probability vectors with one another.
54 /// Probability vectors are assumed to have equal lengths.
55 template <class ForwardIterator>
56 void product(ForwardIterator probs1_first, ForwardIterator probs1_last,
57  ForwardIterator probs2_first, ForwardIterator probs2_last) {
58  normalize(probs1_first, probs1_last);
59  normalize(probs2_first, probs2_last);
60 
61  ForwardIterator i = probs1_first;
62  ForwardIterator j = probs2_first;
63  for ( ; i != probs1_last; ++i, ++j ) {
64  *i *= *j;
65  }
66  normalize(probs1_first, probs1_last);
67 }
68 
69 /// @brief Loads normalized, per-residue probabilities from filename,
70 /// storing the result in probs. Assumes line i holds the probability
71 /// of sampling residue i. There must be 1 line for each residue in the
72 /// pose on which this data will be used.
73 void read_probabilities_or_die(const std::string& filename, utility::vector1<double>* probs);
74 
75 /// @brief Writes probs to the specified ostream
76 void print_probabilities(const utility::vector1<double>& probs, std::ostream& out);
77 
78 } // namespace numeric
79 
80 #endif // NUMERIC_PROB_UTIL_hh_
void cumulative(RandomAccessIterator first, RandomAccessIterator last)
Converts pdf to cdf.
Definition: prob_util.hh:46
void print_probabilities(const utility::vector1< double > &probs, std::ostream &out)
Writes probs to the specified ostream.
Definition: prob_util.cc:46
void normalize(InputIterator first, InputIterator last)
Normalizes elements on the range [first, last)
Definition: prob_util.hh:37
void product(ForwardIterator probs1_first, ForwardIterator probs1_last, ForwardIterator probs2_first, ForwardIterator probs2_last)
Multiplies two probability vectors with one another. Probability vectors are assumed to have equal le...
Definition: prob_util.hh:56
vector1: std::vector with 1-based indexing
double sum(InputIterator first, InputIterator last)
Returns the sum of all elements on the range [first, last)
Definition: prob_util.hh:27
void read_probabilities_or_die(const std::string &filename, utility::vector1< double > *probs)
Loads normalized, per-residue probabilities from filename, storing the result in probs. Assumes line i holds the probability of sampling residue i. There must be 1 line for each residue in the pose on which this data will be used.
Definition: prob_util.cc:27