-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathxregNormDist.h
128 lines (87 loc) · 3.4 KB
/
xregNormDist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* MIT License
*
* Copyright (c) 2020-2022 Robert Grupp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef XREGNORMDIST_H_
#define XREGNORMDIST_H_
#include "xregDistInterface.h"
namespace xreg
{
/// \brief One dimensional Normal distribution
///
/// Pre-computes as much as possible in the constructor.
class NormalDist1D final : public Dist
{
public:
NormalDist1D();
NormalDist1D(const Scalar m, const Scalar s);
Scalar operator()(const Scalar x) const;
Scalar density(const PtN& x) const override;
Scalar log_density(const PtN& x) const override;
Scalar log_density(const Scalar x) const;
Scalar norm_const() const override;
Scalar log_norm_const() const override;
bool normalized() const override;
size_type dim() const override;
PtN draw_sample(std::mt19937& g) const override;
MatMxN draw_samples(const size_type num_samples, std::mt19937& g) const override;
private:
const Scalar mu_;
const Scalar sigma_;
const Scalar sigma_sq_;
const Scalar minus_one_over_two_sigma_sq_;
const Scalar norm_const_;
const Scalar log_norm_const_;
};
/// \brief Two dimensional Normal distribution with independent dimensions
///
/// This DOES NOT use two NormalDist1D instances, but rather computes as much
/// as possible in the constructor and only evaluates a single std::exp in
/// the evaluation operator.
class NormalDist2DIndep final : public Dist
{
public:
// Takes the mean x and y values and the standard deviations in each direction.
NormalDist2DIndep(const Scalar m_x, const Scalar m_y,
const Scalar s_x, const Scalar s_y);
Scalar operator()(const Scalar x, const Scalar y) const;
Scalar log_density(const Scalar x, const Scalar y) const;
Scalar density(const PtN& x) const override;
Scalar log_density(const PtN& x) const override;
Scalar norm_const() const override;
Scalar log_norm_const() const override;
bool normalized() const override;
size_type dim() const override;
PtN draw_sample(std::mt19937& g) const override;
MatMxN draw_samples(const size_type num_samples, std::mt19937& g) const override;
private:
const Scalar mu_x_;
const Scalar mu_y_;
const Scalar sigma_x_;
const Scalar sigma_y_;
const Scalar one_over_sigma_x_sq_;
const Scalar one_over_sigma_y_sq_;
const Scalar norm_const_; // normalizing constant (2pi s_x * s_y)
const Scalar log_norm_const_;
};
} // xreg
#endif