ssmkit  master-68aed98
hierarchical.hpp
Go to the documentation of this file.
1 
7 #ifndef SSMPACK_PROCESS_HIERARCHICAL_HPP
8 #define SSMPACK_PROCESS_HIERARCHICAL_HPP
9 
13 
14 #include <utility>
15 #include <tuple>
16 
17 namespace ssmkit {
18 namespace process {
19 
30 template <class... Args>
31 class Hierarchical : public BaseProcess<Hierarchical<Args...>> {
32  // check if all argument are process types! (can C++17 concepts used instead)
33  static_assert(AreProcesses<Args...>::value, "");
34 
35  private:
37  using TArities = std::tuple<typename ProcessTraits<Args>::TArity...>;
39  static constexpr size_t depth = sizeof...(Args)-1;
40 
41  public:
46  using TRandomVAR = std::tuple<typename ProcessTraits<Args>::TRandomVAR...>;
47 
48  private:
50  std::tuple<Args...> processes_;
51 
52  public:
66  Hierarchical(Args... processes) : processes_{std::move(processes)...} {}
67 
73  TRandomVAR rvs;
74  detail::GetInitialized<depth>::apply(rvs, processes_);
75  return rvs;
76  }
89  template <class... TVARs>
90  TRandomVAR random(const TVARs &... args) {
91  // what if args are more than what actually required?
92 
93  /* we make a variable and pass its reference to the GetRandom class member
94  * apply which takes random variable of level 0 and recursively passes the
95  * same reference to the GetRandom of other levels until reaches depth.
96  */
97  TRandomVAR rvs;
98  constexpr size_t arity = std::tuple_element<0, TArities>::type::value;
99  detail::GetRandom<0, arity, depth, TArities>::apply(
100  std::make_index_sequence<arity>(), rvs, processes_,
101  std::make_tuple(args...));
102  return rvs;
103  }
104 
119  template <class... TArgs>
120  double likelihood(const TRandomVAR &rvs, const TArgs &... args) {
121  double lik;
122  constexpr size_t arity = std::tuple_element<0, TArities>::type::value;
123  detail::GetLikelihood<0, arity, depth, TArities>::apply(
124  tao::seq::make_index_range<0, arity>(), lik, rvs, processes_,
125  std::make_tuple(args...));
126  return lik;
127  }
128 
138  template<size_t L>
139  typename std::tuple_element<L, std::tuple<Args...>>::type &
140  getProcess() {return std::get<L>(processes_); }
141 };
142 
150 template <class... TArgs>
151 Hierarchical<TArgs...> makeHierarchical(TArgs... args) {
152  return Hierarchical<TArgs...>(args...);
153 }
154 
155 } // namespace process
156 } // namespace ssmkit
157 
158 #endif // SSMPACK_PROCESS_HIERARCHICAL_HPP
A stochastic process constructed as hierarchy of stochastic processes.
TRandomVAR initialize()
Initialize process.
TRandomVAR random(const TVARs &...args)
Sample random variable.
std::tuple_element< L, std::tuple< Args...> >::type & getProcess()
get a reference to the process at level L
Hierarchical(Args...processes)
Constructor.
std::tuple< typename ProcessTraits< Args >::TRandomVAR...> TRandomVAR
Type of the random variable.
Hierarchical< TArgs...> makeHierarchical(TArgs...args)
A convenient builder for Hierarchical process.
double likelihood(const TRandomVAR &rvs, const TArgs &...args)
Calculate likelihood.