Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MixtureFunc.cc
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 src/core/scoring/constraints/MixtureFunc.cc
11 /// @brief Definition for functions used in definition of constraints.
12 /// @author James Thompson
13 
14 
16 // AUTO-REMOVED #include <core/scoring/constraints/ConstraintIO.hh>
18 
19 #include <core/types.hh>
20 #include <basic/options/option.hh>
21 
22 #include <utility/pointer/ReferenceCount.hh>
23 
24 // AUTO-REMOVED #include <basic/Tracer.hh>
25 
26 #include <ObjexxFCL/format.hh>
27 
28 // C++ Headers
29 
30 #include <iostream>
31 #include <algorithm>
32 
33 
34 // option key includes
35 
36 #include <basic/options/keys/constraints.OptionKeys.gen.hh>
37 
38 #include <utility/vector1.hh>
39 
40 
41 
42 namespace core {
43 namespace scoring {
44 namespace constraints {
45  using namespace basic::options;
46  using namespace basic::options::OptionKeys;
47 
48  void
49  MixtureFunc::read_data( std::istream & in ) {
50  in >> anchor_ >> gaussian_param_ >> exp_param_ >> mixture_param_ >> bg_mean_ >> bg_sd_;
51  verify_parameters_();
52  }
53 
55  // quick check in case bg_mean_ and bg_sd_ aren't properly defined!
56  if ( bg_mean_ <= 0.001 ) { // should never be true, atoms aren't this close!
57  bg_mean_ = 21.0477;
58  bg_sd_ = 6.60232;
59  }
60 
61 
62  rmax_ = anchor_ + 8;
63  fmax_ = func_(rmax_);
64  // std::cout << "(" << rmax_ << "," << fmax_ << ")" << std::endl;
65  }
66 
67  /// @brief private
68  Real MixtureFunc::func_( Real local_x ) const {
69  // halved because an exponential is a one-sided distribution.
70  Real exp_score = 0.5 * dexponential( local_x, anchor_, exp_param_, mixture_param_ );
71  Real gauss_score = dgaussian ( local_x, anchor_, gaussian_param_, (1.0 - mixture_param_) );
72  Real bg_score = dgaussian ( local_x, bg_mean_, bg_sd_, 1.0 );
73 
74  Real score;
75  // std::cout << "score(" << local_x << ") = -1 * log( (" << exp_score << " + " << gauss_score
76  // << ") / " << exp(bg_score) << ")" << std::endl;
77  if ( option[ basic::options::OptionKeys::constraints::normalize_mixture_func ]() ) {
78  // brutal hack, make potential approach zero as we go from 3.8 -> 3.5 angstroms ...
79  static const Real upper( 3.8 );
80  static const Real lower( 3.5 );
81  if ( local_x < upper ) {
82  Real factor = std::min( (upper - local_x) / (upper - lower), 1.0 );
83  bg_score = factor * (exp_score + gauss_score);
84  }
85  score = -1 * log( (exp_score + gauss_score) / bg_score );
86  } else {
87  score = -1 * log (exp_score + gauss_score);
88  // another brutal hack: if log is negative, set it to zero. This
89  // happens when the estimated probabilities are greater than 1.0,
90  // which is a numerical artifact of the function fitting process.
91  if ( score < 0 ) score = 0;
92  }
93  // std::cout << "score = " << score << std::endl << std::endl;
94  return score;
95  }
96 
97  Real
98  MixtureFunc::func( Real const x ) const {
99  Real local_x = x;
100  // Real exp_score = dexponential( local_x, anchor_, exp_param_, mixture_param_ );
101  // Real gauss_score = dgaussian ( local_x, anchor_, gaussian_param_, (1 - mixture_param_) );
102  // Real bg_score = dgaussian( local_x, bg_mean_, bg_sd_, 1 );
103 
104  Real score = func_(local_x);
105  if ( local_x < rmax_ ) {
106  score = func_(local_x);
107  } else {
108  score = fmax_ + local_x - rmax_;
109  }
110 
111  //std::cout << "score(" << local_x << ") = " << func_(local_x) << std::endl;;
112 
113  if ( option[ basic::options::OptionKeys::constraints::penalize_mixture_func ]() ) {
114  return score;
115  } else {
116  return std::min( score, 0.0 );
117  }
118  } // MixtureFunc::func( Real const x )
119 
120 
121  Real
122  MixtureFunc::dfunc( Real const x ) const {
123  //Real g, h, g_prime, h_prime;
124  //Real df = dfunc_component( x, g, h, g_prime, h_prime );
125 
126  //Real local_x = x;
127  Real df = estimate_dfunc( x );
128  // std::cout << "df = " << df << std::endl;
129 
130  return df;
131  } // dfunc_component
132 
133  Real
135  Real const x,
136  Real & g,
137  Real & h,
138  Real & g_prime,
139  Real & h_prime
140  ) const {
141  Real exp_deriv = exponential_deriv( x, anchor_, exp_param_, mixture_param_ );
142  Real gauss_deriv = gaussian_deriv ( x, anchor_, gaussian_param_, (1 - mixture_param_) );
143 
144  Real exp_score = dexponential( x, anchor_, exp_param_, mixture_param_ );
145  Real gauss_score = dgaussian ( x, anchor_, gaussian_param_, (1 - mixture_param_) );
146 
147  // f(x) = log(g(x)) - log(h(x))
148  // f'(x) = g'(x) / g(x) - h'(x) / h(x)
149  g = exp_score + gauss_score;
150  h = dgaussian( x, bg_mean_, bg_sd_, 1 );
151  g_prime = exp_deriv + gauss_deriv;
152  h_prime = gaussian_deriv( x, bg_mean_, bg_sd_, 1 );
153  Real dfunc = g_prime / g - h_prime / h;
154 
155  return dfunc;
156  } // dfunc_component
157 
158  void MixtureFunc::show( std::ostream& out ) const {
159  using namespace ObjexxFCL::fmt;
160 
161  float start = 2;
162  float end = 20;
163  float res = 0.1;
164  int width = 16;
165  int precise = 4;
166  out << A( width, "r" )
167  << A( width, "func" )
168  << A( width, "dfunc" )
169  << A( width, "g" )
170  << A( width, "h" )
171  << A( width, "g_prime" )
172  << A( width, "h_prime" )
173  << A( width, "dfunc_est" )
174  << std::endl;
175  for ( Real r = start; r <= end; r += res ) {
176  Real g, h, g_prime, h_prime;
177  Real dfunc = dfunc_component( r, g, h, g_prime, h_prime );
178 
179  out << I( width, r )
180  << F( width, precise, func(r) )
181  << F( width, precise, dfunc )
182  << F( width, precise, g )
183  << F( width, precise, h )
184  << F( width, precise, g_prime )
185  << F( width, precise, h_prime )
186  << F( width, precise, estimate_dfunc(r) )
187  << std::endl;
188  } // for ( Real r = start; r <= end; r += res )
189  } // void show( std::ostream& out )
190 
191  void MixtureFunc::show_definition( std::ostream &out ) const {
192  out << "MIXTUREFUNC " << anchor_ << ' ' << gaussian_param_
193  << ' ' << exp_param_ << ' ' << mixture_param_ << ' ' << bg_mean_
194  << ' ' << bg_sd_ << std::endl;
195  } // show_definition
196 
198  float res = 0.1;
199  float start = 2;
200  float end = 20;
201 
202  Real kl_divergence = 0;
203  for ( float r = start; r <= end; r += res ) {
204  Real g, h, g_prime, h_prime;
205  dfunc_component( r, g, h, g_prime, h_prime );
206  kl_divergence += g * ( std::log(g) - std::log(h) );
207  } // for ( float r = start; r <= end; r += res )
208 
209  return kl_divergence;
210  } // calc_kl_divergence
211 
212 } // namespace constraints
213 } // namespace scoring
214 } // namespace core
215