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

A template for a class that encodes and decodes parameters for the Nelder-Mead algorithm. This class does not use a user-specified Newick tree. More...

#include <jade.treeless_controller.hpp>

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

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 
typedef basic_settings< value_typesettings_type
 The settings type. More...
 
typedef basic_controller< TValue >::container_type container_type
 The container type for the controller. More...
 
typedef basic_likelihood< value_typelikelihood_type
 The likelihood type. More...
 
typedef basic_options< value_typeoptions_type
 The options type. More...
 
typedef basic_simplex< value_typesimplex_type
 The simplex type. 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

 basic_treeless_controller (const settings_type &settings)
 Initializes a new instance of the class based on the specified program settings. More...
 
virtual container_type init_parameters () override
 Creates and returns the initial set of parameters for the Nelder- Mead algorithm. 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
 
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

virtual bool _decode_lower (matrix_type &dst, const container_type &src) override
 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_treeless_controller< TValue >

A template for a class that encodes and decodes parameters for the Nelder-Mead algorithm. This class does not use a user-specified Newick tree.

Definition at line 20 of file jade.treeless_controller.hpp.

Member Typedef Documentation

◆ container_type

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

The container type for the controller.

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

◆ exit_condition_type

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

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
inherited

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
inherited

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_treeless_controller< TValue >::matrix_type

The matrix type.

Definition at line 28 of file jade.treeless_controller.hpp.

◆ options_type

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

The options type.

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

◆ settings_type

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

The settings type.

Definition at line 31 of file jade.treeless_controller.hpp.

◆ simplex_type

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

The simplex type.

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

◆ value_type

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

The value type.

Definition at line 25 of file jade.treeless_controller.hpp.

Constructor & Destructor Documentation

◆ basic_treeless_controller()

template<typename TValue >
jade::basic_treeless_controller< TValue >::basic_treeless_controller ( const settings_type settings)
inlineexplicit

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

Parameters
settingsThe program settings.

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

44  : basic_controller<TValue> (settings)
45  {
46  }

Member Function Documentation

◆ _decode_lower()

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

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

Returns
True to indicate this method is successful.
Parameters
dstThe covariance matrix.
srcThe Nelder-Mead container.

Implements jade::basic_controller< TValue >.

Definition at line 104 of file jade.treeless_controller.hpp.

108  {
109  const auto rk = this->get_rk();
110 
111  //
112  // The container contains the lower-right triangle of data,
113  // including the diagonal. This comes to RK * RK (the total number
114  // of items in the matrix), subtract RK (the number of items in the
115  // diagonal), divide by two (only half of these elements), plus RK
116  // (the number of items in the diagonal).
117  //
118  assert(src.size() == rk + ((rk * rk) - rk) / 2);
119 
120  //
121  // Pointers to the start and end of the first column.
122  //
123  auto dst_ptr = dst.get_data();
124  auto dst_end = dst.get_data() + dst.get_length();
125  auto src_ptr = src.data();
126  const auto src_end = src.data() + src.size();
127 
128  //
129  // Loop over every row/column.
130  //
131  while (src_ptr != src_end)
132  {
133  //
134  // Don't lose track of the start of the column data.
135  //
136  const auto anchor = dst_ptr;
137 
138  //
139  // Copy the rows of the column from the diagonal to the end.
140  //
141  for (; dst_ptr != dst_end; dst_ptr += rk)
142  *dst_ptr = *src_ptr++;
143 
144  //
145  // The next start-of-column is below and to the right of the
146  // previous one. The next end-of-column is one to the right.
147  //
148  dst_ptr = anchor + rk + 1;
149  dst_end++;
150  }
151 
152  //
153  // This method is always successful.
154  //
155  return true;
156  }
+ Here is the call graph for this function:

◆ compute_objfunc()

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

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  
)
inlinevirtualinherited

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
inlineinherited
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
inlineinherited
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_treeless_controller< TValue >::init_parameters ( )
inlineoverridevirtual

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

Returns
The initial parameters for the Nelder-Mead algorithm.

Implements jade::basic_controller< TValue >.

Definition at line 49 of file jade.treeless_controller.hpp.

50  {
51  const auto & c = this->get_c();
52  const auto rk = this->get_rk();
53  assert(c.get_height() == rk);
54 
55  //
56  // The container contains the lower-right triangle of data,
57  // including the diagonal. This comes to RK * RK (the total number
58  // of items in the matrix), subtract RK (the number of items in the
59  // diagonal), divide by two (only half of these elements), plus RK
60  // (the number of items in the diagonal).
61  //
62  container_type params;
63  params.reserve(rk + ((rk * rk) - rk) / 2);
64 
65  //
66  // Pointers to the start and end of the first row. Note the
67  // covariance matrix is symmetric.
68  //
69  auto src_ptr = c.get_data();
70  auto src_end = c.get_data() + rk;
71 
72  //
73  // Loop over every row/column.
74  //
75  for (size_t i = 0; i < rk; i++)
76  {
77  //
78  // Skip the columns before the diagonal.
79  //
80  src_ptr += i;
81 
82  //
83  // Copy all columns from the diagonal to the end of the row.
84  //
85  while (src_ptr != src_end)
86  params.push_back(*src_ptr++);
87 
88  //
89  // Advance the next end-of-row RK elements ahead.
90  //
91  src_end += rk;
92  }
93 
94  return params;
95  }
+ Here is the call graph for this function:

◆ log_iteration()

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

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_treeless_controller::container_type
basic_controller< TValue >::container_type container_type
The container type for the controller.
Definition: jade.treeless_controller.hpp:36
jade::basic_controller::get_rk
size_t get_rk() const
Definition: jade.controller.hpp:164
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
jade::basic_controller::get_c
const matrix_type & get_c() const
Definition: jade.controller.hpp:156