cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
generate_pD.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef cMHN_UTILITY_GENERATE_PD_H
4#define cMHN_UTILITY_GENERATE_PD_H
5
8
9#include <prc.hpp>
10
11namespace cMHN
12{
27 template<class T, pRC::Size D, class F>
28 static inline auto generatePD(pRC::RandomEngine<F> &rng,
29 pRC::Tensor<T, D, D> const &smallThetaGT, pRC::Size const &size)
30 {
31 using Subscripts =
33 [](auto const... seq)
34 {
35 return pRC::Subscripts<seq...>();
36 }));
37
38 std::map<Subscripts, T> pD;
40
41 // rng setup
43
44 pRC::Bool worked = true;
45 pRC::Index at_try = 0;
46 do
47 {
48 ++at_try;
49 Subscripts check{};
50
51 pD = std::map<Subscripts, T>();
52 pDSum = 0;
53
54 for(pRC::Index i = 0; i < size; ++i)
55 {
56 Subscripts sample{};
57
58 pRC::Tensor<T, D> transitionRates =
59 exp(extractDiagonal(smallThetaGT));
60
61 // simulate until we terminate
62 while(true)
63 {
64 // simulate one step
65 T const rateSum =
66 reduce<pRC::Add>(transitionRates)() + pRC::unit<T>();
67 T const rand = pRC::random<T>(rng, dist);
68 T sumRejected = pRC::zero();
69 pRC::Index newEvent = 0;
70 while(sumRejected + transitionRates(newEvent) <
71 rand * rateSum)
72 {
73 sumRejected += transitionRates(newEvent);
74 ++newEvent;
75 if(newEvent == D)
76 break;
77 }
78 if(newEvent == D)
79 break;
80 sample[newEvent] = 1;
81 check[newEvent] = 1;
82
83 // update transitionRates
84 transitionRates[newEvent] = pRC::zero();
85 for(pRC::Index j = 0; j < D; ++j)
86 {
87 transitionRates[j] *= exp(smallThetaGT(j, newEvent));
88 }
89 }
90
91 pD.try_emplace(sample, pRC::zero<T>());
92 pD[sample] += pRC::unit<T>();
93
95 }
96
97 // check if there is a zero column
98 worked = true;
99 for(pRC::Index i = 0; i < D; ++i)
100 {
101 if(check[i] == 0)
102 {
104 "Zero column detected in generated dataset, "
105 "regenerating!");
106 worked = false;
107 }
108 }
109 }
110 while(!worked && at_try < 10);
111
112 // normalize pD
113 for(auto &[k, v] : pD)
114 {
115 v /= pDSum;
116 }
117
118 return pD;
119 }
120
134 template<class T, pRC::Size D>
135 static inline auto generatePD(pRC::Tensor<T, D, D> const &smallThetaGT,
136 pRC::Size const &size)
137 {
138 // rng setup
139 pRC::SeedSequence seq(8, 16);
141
142 return generatePD(rng, smallThetaGT, size);
143 }
144}
145
146#endif // cMHN_UTILITY_GENERATE_PD_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition value.hpp:12
Definition value.hpp:15
Definition engine.hpp:13
Definition seq.hpp:13
Definition subscripts.hpp:21
Definition tensor.hpp:25
Definition threefry.hpp:22
Definition uniform.hpp:18
int i
Definition gmock-matchers-comparisons_test.cc:603
Definition calculate_pTheta.hpp:20
static auto generatePD(pRC::RandomEngine< F > &rng, pRC::Tensor< T, D, D > const &smallThetaGT, pRC::Size const &size)
Generates a data distribution from a given ground truth model.
Definition generate_pD.hpp:28
static void warning(Xs &&...args)
Definition log.hpp:21
static constexpr auto unit()
Definition unit.hpp:13
bool Bool
Definition basics.hpp:29
static constexpr auto makeConstantSequence()
Definition sequence.hpp:444
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
static constexpr auto zero()
Definition zero.hpp:12
static constexpr auto random(URNG &rng, D &distribution)
Definition random.hpp:13