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

A template for a class that encodes and decodes parameters for the Nelder-Mead algorithm. This class uses an admixture graph input file. More...

#include <jade.agi_controller.hpp>

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

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_agi_reader< value_typeagi_reader_type
 The admixture graph input reader type. More...
 
typedef basic_shunting_yard< value_typeshunting_yard_type
 The shunting-yard algorithm type. More...
 
typedef shunting_yard_type::args_type args_type
 The arguments passed to the shunting-yard algorithm. 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 type for the simplex. More...
 
typedef basic_likelihood< value_typelikelihood_type
 The likelihood type. More...
 
typedef simplex_type::log_args log_args_type
 The loc arguments for the simplex. More...
 

Public Member Functions

 basic_agi_controller (const settings_type &settings)
 Initializes a new instance of the class based on the specified program settings. More...
 
void emit_results (const options_type &opts, const simplex_type &simplex, const exit_condition_type &condition) override
 Writes results to standard output and files. More...
 
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...
 
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

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_agi_controller< TValue >

A template for a class that encodes and decodes parameters for the Nelder-Mead algorithm. This class uses an admixture graph input file.

Definition at line 19 of file jade.agi_controller.hpp.

Member Typedef Documentation

◆ agi_reader_type

template<typename TValue >
typedef basic_agi_reader<value_type> jade::basic_agi_controller< TValue >::agi_reader_type

The admixture graph input reader type.

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

◆ args_type

template<typename TValue >
typedef shunting_yard_type::args_type jade::basic_agi_controller< TValue >::args_type

The arguments passed to the shunting-yard algorithm.

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

◆ container_type

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

The container type for the simplex.

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

◆ exit_condition_type

The exit condition type for the simplex.

Definition at line 51 of file jade.agi_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_agi_controller< TValue >::matrix_type

The matrix type.

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

◆ options_type

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

The options type.

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

◆ settings_type

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

The settings type.

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

◆ shunting_yard_type

template<typename TValue >
typedef basic_shunting_yard<value_type> jade::basic_agi_controller< TValue >::shunting_yard_type

The shunting-yard algorithm type.

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

◆ simplex_type

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

The simplex type.

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

◆ value_type

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

The value type.

Definition at line 24 of file jade.agi_controller.hpp.

Constructor & Destructor Documentation

◆ basic_agi_controller()

template<typename TValue >
jade::basic_agi_controller< TValue >::basic_agi_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 57 of file jade.agi_controller.hpp.

59  : basic_controller<TValue> (settings)
60  , _agi (settings.get_agi())
61  , _args (_agi.get_args())
62  {
63  }

Member Function Documentation

◆ _decode_lower()

template<typename TValue >
bool jade::basic_agi_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 if successful; otherwise, false.
Parameters
dstThe covariance matrix.
srcThe Nelder-Mead container.

Implements jade::basic_controller< TValue >.

Definition at line 116 of file jade.agi_controller.hpp.

120  {
121  //
122  // If any value in the container is not positive, then return false
123  // to indicate these are unacceptable values.
124  //
125  for (const auto item : src)
126  if (item <= value_type(0))
127  return false;
128 
129  //
130  // If any value corresponding to a proportion is not less than one,
131  // then return false to indicate these are unacceptable values.
132  // Note the proportion variables are stored first in the container
133  // (and are followed by the branch variables).
134  //
135  {
136  const auto n = _agi.get_proportion_names().size();
137  for (size_t i = 0; i < n; i++)
138  if (src[i] >= value_type(1))
139  return false;
140  }
141 
142  //
143  // Copy the values from the Nelder-Mead container into the table
144  // of arguments used to evaluate the expression. Since these values
145  // are the same for all expressions, the same container is used for
146  // each expression evaluated.
147  //
148  {
149  size_t i = 0;
150  for (const auto & name : _agi.get_proportion_names())
151  _args[name] = src[i++];
152  for (const auto & name : _agi.get_branch_names())
153  _args[name] = src[i++];
154  }
155 
156  //
157  // Copy the expression output to the lower-triangle of the
158  // covariance matrix:
159  //
160  // | 0 - - |
161  // | 1 2 - |
162  // | 3 4 5 |
163  //
164  // Each value in the matrix is determined by evaluating a
165  // corresponding expression.
166  //
167  {
168  const auto & entries = _agi.get_entries();
169  const auto rk = _agi.get_k() - 1;
170  size_t i = 0;
171  for (size_t row = 0; row < rk; row++)
172  for (size_t col = 0; col <= row; col++)
173  dst(row, col) = entries[i++].evaluate(_args);
174  }
175 
176  //
177  // Return true to indicate the values are acceptable.
178  //
179  return true;
180  }
+ 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 >
void jade::basic_agi_controller< TValue >::emit_results ( const options_type opts,
const simplex_type simplex,
const exit_condition_type condition 
)
inlineoverridevirtual

Writes results to standard output and files.

Parameters
optsThe options.
simplexThe simplex.
conditionThe context.

Reimplemented from jade::basic_controller< TValue >.

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

73  {
74  //
75  // The base class emits the C matrix; this also has the effect of
76  // updating the arguments used to evaluate the expressions.
77  //
78  basic_controller<TValue>::emit_results(opts, simplex, condition);
79 
80  //
81  // Loop over each argument and display its value.
82  //
83  std::cout << "\n[Admixture Graph Output]\n";
85  for (const auto & name : _agi.get_branch_names())
86  std::cout << name << "\t" << _args[name] << "\n";
87  for (const auto & name : _agi.get_proportion_names())
88  std::cout << name << "\t" << _args[name] << "\n";
89  std::cout.flush();
90  }
+ Here is the call 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 >
container_type jade::basic_agi_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 98 of file jade.agi_controller.hpp.

99  {
100  //
101  // Return a container initially filled with 0.5 for both branch and
102  // proportion variables.
103  //
104  container_type out;
105  out.resize(_args.size(), value_type(0.5));
106  return out;
107  }

◆ 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_agi_controller::container_type
simplex_type::container_type container_type
The container type for the simplex.
Definition: jade.agi_controller.hpp:48
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_agi_controller::value_type
TValue value_type
The value type.
Definition: jade.agi_controller.hpp:24
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_agi_reader::get_k
std::size_t get_k() const
Returns the number of components.
Definition: jade.agi_reader.hpp:198
jade::basic_agi_reader::get_branch_names
const std::vector< std::string > & get_branch_names() const
Returns the vector of branch names.
Definition: jade.agi_reader.hpp:180
jade::basic_controller::emit_results
virtual void emit_results(const options_type &opts, const simplex_type &simplex, const exit_condition_type &)
Writes results to standard output and files.
Definition: jade.controller.hpp:110
jade::basic_agi_reader::get_entries
const std::vector< shunting_yard_type > & get_entries() const
Returns the vector of expressions for the C matrix entries.
Definition: jade.agi_reader.hpp:207
jade::basic_agi_reader::get_args
const args_type & get_args() const
Returns a table of arguments that maps variables to values. The table reference returned has all valu...
Definition: jade.agi_reader.hpp:171
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_agi_reader::get_proportion_names
const std::vector< std::string > & get_proportion_names() const
Returns the vector of proportion names.
Definition: jade.agi_reader.hpp:189