ʻOhana
Population structure, admixture history, and selection using learning methods.
Public Types | Public Member Functions | Protected Member Functions
jade::basic_controller< TValue > Class Template Referenceabstract

A template for a class that performs the Nelder-Mead optimization. This class implements shared features of the two concrete controllers of this module, one that uses a Newick tree as input and another that does not. More...

#include <jade.controller.hpp>

+ Inheritance diagram for jade::basic_controller< TValue >:
+ Collaboration diagram for jade::basic_controller< TValue >:

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_likelihood< value_typelikelihood_type
 The likelihood type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 
typedef basic_options< value_typeoptions_type
 The options type. More...
 
typedef basic_settings< value_typesettings_type
 The settings type. More...
 
typedef basic_simplex< value_typesimplex_type
 The simplex type. More...
 
typedef simplex_type::container_type container_type
 The container type for the simplex. More...
 
typedef simplex_type::exit_condition_type exit_condition_type
 The exit condition for the simplex. More...
 
typedef simplex_type::log_args log_args_type
 The loc arguments for the simplex. More...
 

Public Member Functions

virtual ~basic_controller ()
 Reclaims resources, if any, used by the instance. More...
 
value_type compute_objfunc (const container_type &params)
 Computes the objective function by decoding the specified Nelder- Mead parameters into a covariance matrix, performing a Cholskey square root, calculating the determinant, computing the inverse, and calling the likelihood function. More...
 
virtual void emit_results (const options_type &opts, const simplex_type &simplex, const exit_condition_type &)
 Writes results to standard output and files. More...
 
const matrix_typeget_c () const
 
size_t get_rk () const
 
virtual container_type init_parameters ()=0
 Creates and returns the initial set of parameters for the Nelder- Mead algorithm. More...
 
void log_iteration (const log_args_type &log_args)
 Logs information about the specified context for one iteration of the Nelder-Mead algorithm. More...
 

Protected Member Functions

 basic_controller (const settings_type &settings)
 Initializes a new instance of the class based on the specified settings. More...
 
virtual bool _decode_lower (matrix_type &dst, const container_type &src)=0
 Decodes the specified Nelder-Mead container and stores the result into the lower triangle, including the diagonal, of the covariance matrix. More...
 

Detailed Description

template<typename TValue>
class jade::basic_controller< TValue >

A template for a class that performs the Nelder-Mead optimization. This class implements shared features of the two concrete controllers of this module, one that uses a Newick tree as input and another that does not.

Definition at line 23 of file jade.controller.hpp.

Member Typedef Documentation

◆ container_type

template<typename TValue >
typedef simplex_type::container_type jade::basic_controller< TValue >::container_type

The container type for the simplex.

Definition at line 45 of file jade.controller.hpp.

◆ exit_condition_type

template<typename TValue >
typedef simplex_type::exit_condition_type jade::basic_controller< TValue >::exit_condition_type

The exit condition for the simplex.

Definition at line 48 of file jade.controller.hpp.

◆ likelihood_type

template<typename TValue >
typedef basic_likelihood<value_type> jade::basic_controller< TValue >::likelihood_type

The likelihood type.

Definition at line 30 of file jade.controller.hpp.

◆ log_args_type

template<typename TValue >
typedef simplex_type::log_args jade::basic_controller< TValue >::log_args_type

The loc arguments for the simplex.

Definition at line 51 of file jade.controller.hpp.

◆ matrix_type

template<typename TValue >
typedef basic_matrix<value_type> jade::basic_controller< TValue >::matrix_type

The matrix type.

Definition at line 33 of file jade.controller.hpp.

◆ options_type

template<typename TValue >
typedef basic_options<value_type> jade::basic_controller< TValue >::options_type

The options type.

Definition at line 36 of file jade.controller.hpp.

◆ settings_type

template<typename TValue >
typedef basic_settings<value_type> jade::basic_controller< TValue >::settings_type

The settings type.

Definition at line 39 of file jade.controller.hpp.

◆ simplex_type

template<typename TValue >
typedef basic_simplex<value_type> jade::basic_controller< TValue >::simplex_type

The simplex type.

Definition at line 42 of file jade.controller.hpp.

◆ value_type

template<typename TValue >
typedef TValue jade::basic_controller< TValue >::value_type

The value type.

Definition at line 27 of file jade.controller.hpp.

Constructor & Destructor Documentation

◆ ~basic_controller()

template<typename TValue >
virtual jade::basic_controller< TValue >::~basic_controller ( )
inlinevirtual

Reclaims resources, if any, used by the instance.

Definition at line 56 of file jade.controller.hpp.

57  {
58  }

◆ basic_controller()

template<typename TValue >
jade::basic_controller< TValue >::basic_controller ( const settings_type settings)
inlineexplicitprotected

Initializes a new instance of the class based on the specified settings.

Parameters
settingsThe algorithm settings.

Definition at line 207 of file jade.controller.hpp.

209  : _rk (settings.get_rf().get_height())
210  , _c (settings.get_c())
211  , _lle (0)
212  , _likelihood (settings.get_rf(), settings.get_mu())
213  , _iteration_time ()
214  {
215  assert(_rk > 0);
216  assert(_c.is_size(_rk, _rk));
217  }
+ Here is the call graph for this function:

Member Function Documentation

◆ _decode_lower()

template<typename TValue >
virtual bool jade::basic_controller< TValue >::_decode_lower ( matrix_type dst,
const container_type src 
)
protectedpure virtual

Decodes the specified Nelder-Mead container and stores the result into the lower triangle, including the diagonal, of the covariance matrix.

Returns
True if successful; otherwise, false.
Parameters
dstThe covariance matrix.
srcThe Nelder-Mead container.

Implemented in jade::basic_treeless_controller< TValue >, jade::basic_tree_controller< TValue >, and jade::basic_agi_controller< TValue >.

+ Here is the caller graph for this function:

◆ compute_objfunc()

template<typename TValue >
value_type jade::basic_controller< TValue >::compute_objfunc ( const container_type params)
inline

Computes the objective function by decoding the specified Nelder- Mead parameters into a covariance matrix, performing a Cholskey square root, calculating the determinant, computing the inverse, and calling the likelihood function.

Returns
The negation of the log likelihood.
Parameters
paramsThe parameters.

Definition at line 68 of file jade.controller.hpp.

70  {
71  static const auto inf = std::numeric_limits<value_type>::max();
72 
73  //
74  // The LAPACK routines need only the lower triangle stored in
75  // the matrix. If these routines are successful, the lower
76  // triangle is copied to the upper triangle before calculating
77  // the likelihood.
78  //
79  if (!_decode_lower(_c, params))
80  return inf;
81 
82  //
83  // Variance and covariance values must be positive; if one is not,
84  // return infinity to reject these parameters.
85  //
86  for (size_t row = 0; row < _rk; row++)
87  for (size_t col = 0; col <= row; col++)
88  if (_c(row, col) <= value_type(0))
89  return inf;
90 
91  //
92  // Compute the inverse and the log of the determinant. If this
93  // fails, the matrix is not positive semidefinite; return Infinity
94  // to indicate these are unacceptable parameters.
95  //
96  value_type log_c_det;
97  if (!_c.invert(log_c_det))
98  return inf;
99 
100  //
101  // The Nelder-Mead algorithm minimizes the objective function, so
102  // return the negation of the log-likelihood function.
103  //
104  return -_likelihood(_c, log_c_det);
105  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emit_results()

template<typename TValue >
virtual void jade::basic_controller< TValue >::emit_results ( const options_type opts,
const simplex_type simplex,
const exit_condition_type  
)
inlinevirtual

Writes results to standard output and files.

Parameters
optsThe options.
simplexThe simplex.

Reimplemented in jade::basic_tree_controller< TValue >, and jade::basic_agi_controller< TValue >.

Definition at line 110 of file jade.controller.hpp.

114  {
115  //
116  // Decode the final set of parameters (the optimized matrix).
117  //
118  _decode_lower(_c, simplex.get_vertex());
119  _c.copy_lower_to_upper();
120 
121  //
122  // Display the log-likelihood, which is the negative of the last
123  // value from the objective function.
124  //
125  std::cout
126  << "\nlog likelihood = "
127  << -simplex.get_objval()
128  << std::endl;
129 
130  //
131  // Display or save the covariance matrix.
132  //
133  if (opts.is_cout_specified())
134  {
135  const auto & cout = opts.get_cout();
136  std::cout << "Writing C matrix to " << cout << std::endl;
137 
138  std::ofstream out (cout);
139  if (!out.good())
140  throw error() << "failed to create matrix '" << cout << "'";
141 
143  out << _c;
144  }
145  else
146  {
147  std::cout << "[C Matrix]\n";
149  std::cout << _c << std::endl;
150  }
151  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_c()

template<typename TValue >
const matrix_type& jade::basic_controller< TValue >::get_c ( ) const
inline
Returns
A reference to the covariance matrix.

Definition at line 156 of file jade.controller.hpp.

157  {
158  return _c;
159  }
+ Here is the caller graph for this function:

◆ get_rk()

template<typename TValue >
size_t jade::basic_controller< TValue >::get_rk ( ) const
inline
Returns
The rooted K value for this instance.

Definition at line 164 of file jade.controller.hpp.

165  {
166  return _rk;
167  }
+ Here is the caller graph for this function:

◆ init_parameters()

template<typename TValue >
virtual container_type jade::basic_controller< TValue >::init_parameters ( )
pure virtual

Creates and returns the initial set of parameters for the Nelder- Mead algorithm.

Returns
The initial parameters for the Nelder-Mead algorithm.

Implemented in jade::basic_treeless_controller< TValue >, jade::basic_tree_controller< TValue >, and jade::basic_agi_controller< TValue >.

+ Here is the caller graph for this function:

◆ log_iteration()

template<typename TValue >
void jade::basic_controller< TValue >::log_iteration ( const log_args_type log_args)
inline

Logs information about the specified context for one iteration of the Nelder-Mead algorithm.

Parameters
log_argsThe Nelder-Mead arguments.

Definition at line 181 of file jade.controller.hpp.

183  {
184  const auto lle = -log_args.simplex->get_objval();
185 
186  const auto dlle = log_args.iteration == 1
187  ? value_type(0)
188  : lle - _lle;
189 
190  std::ostringstream line;
191  line << log_args.iteration
192  << std::fixed << std::setprecision(6)
193  << '\t' << _iteration_time;
195  line << '\t' << dlle << '\t' << lle;
196  std::cout << line.str() << std::endl;
197 
198  _lle = lle;
199  _iteration_time = stopwatch();
200  }
+ Here is the call graph for this function:

The documentation for this class was generated from the following file:
jade::basic_controller::_decode_lower
virtual bool _decode_lower(matrix_type &dst, const container_type &src)=0
Decodes the specified Nelder-Mead container and stores the result into the lower triangle,...
jade::basic_matrix< value_type >::set_high_precision
static void set_high_precision(std::ostream &out)
Sets the specified stream to scientific notation with a high precision.
Definition: jade.matrix.hpp:1154
jade::basic_matrix::is_size
bool is_size(const basic_matrix &other) const
Definition: jade.matrix.hpp:896
jade::basic_matrix::invert
bool invert(value_type &log_det)
Computes and stores the inverse of this matrix using the Cholesky square root method....
Definition: jade.matrix.hpp:796
jade::basic_controller::value_type
TValue value_type
The value type.
Definition: jade.controller.hpp:27
jade::basic_matrix::copy_lower_to_upper
void copy_lower_to_upper()
Copies the elements in the lower triangle of the square matrix to the corresponding upper triangle el...
Definition: jade.matrix.hpp:266