cMHN 1.1
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{
26 template<class T, pRC::Size D>
27 static inline auto generatePD(pRC::RandomEngine &rng,
28 pRC::Tensor<T, D, D> const &smallThetaGT, pRC::Size const &size)
29 {
30 using Subscripts =
32 [](auto const... seq)
33 {
34 return pRC::Subscripts<seq...>();
35 }));
36
37 std::map<Subscripts, T> pD;
39
40 // rng setup
42
43 pRC::Bool worked = true;
44 pRC::Index at_try = 0;
45 do
46 {
47 ++at_try;
48 Subscripts check{};
49
50 pD = std::map<Subscripts, T>();
51 pDSum = 0;
52
53 for(pRC::Index i = 0; i < size; ++i)
54 {
55 Subscripts sample{};
56
57 pRC::Tensor<T, D> transitionRates =
58 exp(extractDiagonal(smallThetaGT));
59
60 // simulate until we terminate
61 while(true)
62 {
63 // simulate one step
64 T const rateSum =
65 reduce<pRC::Add>(transitionRates)() + pRC::unit<T>();
66 T const rand = pRC::random<T>(rng, dist);
67 T sumRejected = pRC::zero();
68 pRC::Index newEvent = 0;
69 while(sumRejected + transitionRates(newEvent) <
70 rand * rateSum)
71 {
72 sumRejected += transitionRates(newEvent);
73 ++newEvent;
74 if(newEvent == D)
75 break;
76 }
77 if(newEvent == D)
78 break;
79 sample[newEvent] = 1;
80 check[newEvent] = 1;
81
82 // update transitionRates
83 transitionRates[newEvent] = pRC::zero();
84 for(pRC::Index j = 0; j < D; ++j)
85 {
86 transitionRates[j] *= exp(smallThetaGT(j, newEvent));
87 }
88 }
89
90 pD.try_emplace(sample, pRC::zero<T>());
91 pD[sample] += pRC::unit<T>();
92
94 }
95
96 // check if there is a zero column
97 worked = true;
98 for(pRC::Index i = 0; i < D; ++i)
99 {
100 if(check[i] == 0)
101 {
103 "Zero column detected in generated dataset, "
104 "regenerating!");
105 worked = false;
106 }
107 }
108 }
109 while(!worked && at_try < 10);
110
111 // normalize pD
112 for(auto &[k, v] : pD)
113 {
114 v /= pDSum;
115 }
116
117 return pD;
118 }
119
133 template<class T, pRC::Size D>
134 static inline auto generatePD(pRC::Tensor<T, D, D> const &smallThetaGT,
135 pRC::Size const &size)
136 {
137 // rng setup
138 pRC::SeedSequence seq(8, 16);
139 pRC::RandomEngine rng(seq);
140
141 return generatePD(rng, smallThetaGT, size);
142 }
143}
144
145#endif // cMHN_UTILITY_GENERATE_PD_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition integer.hpp:22
Definition seq.hpp:13
Definition subscripts.hpp:20
Definition tensor.hpp:28
Definition threefry.hpp:24
Definition type_traits.hpp:49
Definition calculate_pTheta.hpp:16
static auto generatePD(pRC::RandomEngine &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:27
static void warning(Xs &&...args)
Definition log.hpp:21
bool Bool
Definition type_traits.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::size_t Size
Definition type_traits.hpp:20
static constexpr auto zero()
Definition zero.hpp:12