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

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

#include <jade.tree_controller.hpp>

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

Public Types

typedef TValue value_type
 The value 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_tree_path< value_typepath_type
 The path type. More...
 
typedef basic_simplex< value_typesimplex_type
 The simplex type. More...
 
typedef basic_rerooted_tree< value_typererooted_tree_type
 The rerooted tree type. More...
 
typedef basic_newick_node< value_typeunrooted_tree_type
 The unrooted tree type. More...
 
typedef std::unique_ptr< unrooted_tree_typeunrooted_tree_ptr_type
 The pointer to an unrooted tree 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_tree_controller (const settings_type &settings)
 Initializes a new instance of the class based on the specified program settings. More...
 
virtual 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...
 
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...
 
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_tree_controller< TValue >

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

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

Member Typedef Documentation

◆ container_type

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

The container type for the simplex.

Definition at line 52 of file jade.tree_controller.hpp.

◆ exit_condition_type

The exit condition type for the simplex.

Definition at line 55 of file jade.tree_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_tree_controller< TValue >::matrix_type

The matrix type.

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

◆ options_type

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

The options type.

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

◆ path_type

template<typename TValue >
typedef basic_tree_path<value_type> jade::basic_tree_controller< TValue >::path_type

The path type.

Definition at line 37 of file jade.tree_controller.hpp.

◆ rerooted_tree_type

template<typename TValue >
typedef basic_rerooted_tree<value_type> jade::basic_tree_controller< TValue >::rerooted_tree_type

The rerooted tree type.

Definition at line 43 of file jade.tree_controller.hpp.

◆ settings_type

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

The settings type.

Definition at line 34 of file jade.tree_controller.hpp.

◆ simplex_type

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

The simplex type.

Definition at line 40 of file jade.tree_controller.hpp.

◆ unrooted_tree_ptr_type

template<typename TValue >
typedef std::unique_ptr<unrooted_tree_type> jade::basic_tree_controller< TValue >::unrooted_tree_ptr_type

The pointer to an unrooted tree type.

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

◆ unrooted_tree_type

template<typename TValue >
typedef basic_newick_node<value_type> jade::basic_tree_controller< TValue >::unrooted_tree_type

The unrooted tree type.

Definition at line 46 of file jade.tree_controller.hpp.

◆ value_type

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

The value type.

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

Constructor & Destructor Documentation

◆ basic_tree_controller()

template<typename TValue >
jade::basic_tree_controller< TValue >::basic_tree_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 61 of file jade.tree_controller.hpp.

63  : basic_controller<TValue> (settings)
64  , _settings (settings)
65  , _table ()
66  , _unrooted_tree_ptr ()
67  , _rerooted_tree ()
68  {
69  const auto rk = this->get_rk();
70 
71  //
72  // Parse the Newick-formatted file into a tree structure.
73  //
74  _unrooted_tree_ptr.reset(
76  settings.get_options().get_tin()));
77 
78  //
79  // Initialize the rerooted tree.
80  //
81  _rerooted_tree.reset(*_unrooted_tree_ptr);
82 
83  //
84  // Loop over the rows and columns of the lower triangle, and for
85  // each cell in the covariance matrix, create a corresponding
86  // table entry.
87  //
88  for (size_t r = 0; r < rk; r++)
89  for (size_t c = 0; c <= r; c++)
90  _table.emplace_back(_rerooted_tree, r, c);
91  }
+ Here is the call graph for this function:

Member Function Documentation

◆ _decode_lower()

template<typename TValue >
virtual bool jade::basic_tree_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 153 of file jade.tree_controller.hpp.

157  {
158  //
159  // Copy the container values into the tree, which will be used to
160  // build the covariance matrix.
161  //
162  auto iterator = src.begin();
163  _copy_container_to_tree(_rerooted_tree.get_tree(), iterator);
164 
165  //
166  // Loop over every table entry, which corresponds to the lower
167  // triangle of the covariance matrix; compute and store the sum
168  // of the lengths from the nodes that contribute to its value.
169  //
170  for (const auto & entry : _table)
171  dst[entry.i] = entry.p.get_length();
172 
173  //
174  // This method is always successful.
175  //
176  return true;
177  }
+ 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_tree_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 96 of file jade.tree_controller.hpp.

101  {
102  //
103  // The base class emits the C matrix; this also has the effect of
104  // updating the tree with the optimized lengths.
105  //
106  basic_controller<TValue>::emit_results(opts, simplex, condition);
107 
108  //
109  // Copy values back to the original tree. First reroot the tree to
110  // its original structure so the order of the children is
111  // consistent with the file specified by the user.
112  //
113  const auto root_id = _unrooted_tree_ptr->get_id();
114  unrooted_tree_ptr_type source_root (
115  _rerooted_tree.get_tree().find_id(root_id)->reroot());
116  _unrooted_tree_ptr->for_each([&](node_type * const target) -> void
117  {
118  const auto id = target->get_id();
119  const auto source = source_root->find_id(id);
120  if (source->has_length())
121  target->set_length(source->get_length());
122  else
123  target->erase_length();
124  });
125 
126  if (opts.is_tout_specified())
127  {
128  const auto & tout = opts.get_tout();
129  std::cout << "Writing tree to " << tout << std::endl;
130  _unrooted_tree_ptr->write(tout);
131  }
132  else
133  {
134  std::cout << "\n[Tree]\n" << *_unrooted_tree_ptr << std::endl;
135  }
136  }
+ 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 >
virtual container_type jade::basic_tree_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 139 of file jade.tree_controller.hpp.

140  {
141  container_type container;
142  _copy_tree_to_container(container, _rerooted_tree.get_tree());
143  return container;
144  }
+ 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_tree_controller::container_type
simplex_type::container_type container_type
The container type for the simplex.
Definition: jade.tree_controller.hpp:52
jade::basic_newick_node::reroot
pointer_type reroot() const
Creates a new tree based on the tree associated with this node; in the tree returned,...
Definition: jade.newick.hpp:433
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_rerooted_tree::reset
void reset(const node_type &node)
Resets the tree maintained by this instance.
Definition: jade.rerooted_tree.hpp:123
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_controller::get_rk
size_t get_rk() const
Definition: jade.controller.hpp:164
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_rerooted_tree::get_tree
node_type & get_tree()
Definition: jade.rerooted_tree.hpp:107
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_newick_node::find_id
const_pointer_type find_id(const int id) const
Definition: jade.newick.hpp:206
jade::basic_controller::value_type
TValue value_type
The value type.
Definition: jade.controller.hpp:27
jade::basic_newick_node::from_file
static pointer_type from_file(char const *const path)
Definition: jade.newick.hpp:327
jade::basic_tree_controller::unrooted_tree_ptr_type
std::unique_ptr< unrooted_tree_type > unrooted_tree_ptr_type
The pointer to an unrooted tree type.
Definition: jade.tree_controller.hpp:49