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

A template for a class that efficiently computes a log-likelihood based on 1) the log of the determinant for a covariance matrix and 2) the inverse of the covariance matrix. The class caches values based on a [K-1 x J] rooted F matrix for major allele frequencies and a [J x 1] mu vector. More...

#include <jade.likelihood.hpp>

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

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 

Public Member Functions

 basic_likelihood (const matrix_type &rf, const matrix_type &mu)
 Initializes the class based on a rooted F matrix for major allele frequencies and a mu vector; the constructor caches values based on the supplied arguments. More...
 
value_type operator() (const matrix_type &c_inv, const value_type log_c_det)
 

Detailed Description

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

A template for a class that efficiently computes a log-likelihood based on 1) the log of the determinant for a covariance matrix and 2) the inverse of the covariance matrix. The class caches values based on a [K-1 x J] rooted F matrix for major allele frequencies and a [J x 1] mu vector.

Definition at line 22 of file jade.likelihood.hpp.

Member Typedef Documentation

◆ matrix_type

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

The matrix type.

Definition at line 29 of file jade.likelihood.hpp.

◆ value_type

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

The value type.

Definition at line 26 of file jade.likelihood.hpp.

Constructor & Destructor Documentation

◆ basic_likelihood()

template<typename TValue >
jade::basic_likelihood< TValue >::basic_likelihood ( const matrix_type rf,
const matrix_type mu 
)
inline

Initializes the class based on a rooted F matrix for major allele frequencies and a mu vector; the constructor caches values based on the supplied arguments.

Parameters
rfThe rooted F matrix.
muThe mu vector.

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

39  : _rf (rf)
40  , _mux (_init_mux(mu))
41  , _rkltmux (_init_rkltmux(rf.get_height(), _mux))
42  , _mul (rf.get_height(), rf.get_width())
43  {
44  assert(mu.get_height() == rf.get_width());
45  }

Member Function Documentation

◆ operator()()

template<typename TValue >
value_type jade::basic_likelihood< TValue >::operator() ( const matrix_type c_inv,
const value_type  log_c_det 
)
inline
Returns
The log-likelihood using values cached during the initializaiton of this instance and the supplied arguments.
Parameters
c_invThe inverted C matrix.
log_c_detThe log of det(C).

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

54  {
55  const auto RK = _rf.get_height();
56  const auto J = _rf.get_width();
57 
58  assert(c_inv.is_size(RK, RK));
59 
60  //
61  // Use LAPACK to multiply c_inv and _rf; store the result in the
62  // pre-allocated _mul matrix, which is of size [RK x J].
63  //
64  matrix_type::gemm(c_inv, _rf, _mul);
65 
66  auto sum = value_type(0);
67 
68  //
69  // Loop over the J columns of _mul.
70  //
71  auto rkltmux_ptr = _rkltmux.get_data();
72  auto mux_ptr = _mux.get_data();
73  const auto mux_end = _mux.get_data() + J;
74  auto rf_ptr = _rf.get_data();
75  auto mul_ptr = _mul.get_data();
76  while (mux_ptr != mux_end)
77  {
78  //
79  // Do not process columns with non-positive mux[j].
80  //
81  const auto rkltmux = *rkltmux_ptr++;
82  const auto mux = *mux_ptr++;
83  if (mux <= value_type(0))
84  continue;
85 
86  auto zip = value_type(0);
87 
88  //
89  // Loop over the RK rows of _mul and _rf and sum the products of
90  // rf[rk, j] * mul[rk, j].
91  //
92  const auto rf_anchor = rf_ptr;
93  const auto mul_anchor = mul_ptr;
94  const auto mul_end = mul_ptr + (RK * J);
95  while (mul_ptr != mul_end)
96  {
97  zip += *rf_ptr * *mul_ptr;
98  rf_ptr += J;
99  mul_ptr += J;
100  }
101 
102  //
103  // For this column of J, add
104  // RK*log(2*pi * mux[j]) + (zip / mux[j]).
105  //
106  sum += rkltmux + (zip / mux);
107 
108  //
109  // Advance to the next columns.
110  //
111  rf_ptr = rf_anchor + 1;
112  mul_ptr = mul_anchor + 1;
113  }
114 
115  //
116  // The log-likelihood: -0.5 * (J * log(det(C)) + sum).
117  //
118  return value_type(-0.5) * ((value_type(J) * log_c_det) + sum);
119  }

The documentation for this class was generated from the following file:
jade::basic_matrix::get_data
const value_type * get_data() const
Definition: jade.matrix.hpp:542
jade::basic_matrix::get_width
size_t get_width() const
Definition: jade.matrix.hpp:757
jade::basic_matrix::get_height
size_t get_height() const
Definition: jade.matrix.hpp:603
jade::basic_likelihood::value_type
TValue value_type
The value type.
Definition: jade.likelihood.hpp:26
jade::basic_matrix< value_type >::gemm
static void gemm(const basic_matrix &lhs, const basic_matrix &rhs, basic_matrix &dst, const value_type alpha=value_type(1), const value_type beta=value_type(0))
Multiplies a left matrix by a right matrix and stores the result into a destination matrix....
Definition: jade.matrix.hpp:459