Math.hpp 10.6 KB
Newer Older
1
2
3
#pragma once

#include "elsaDefines.h"
andibraimllari's avatar
andibraimllari committed
4
5
6
7
8
#include "DataContainer.h"

#include <numeric>
#include <cmath>
#include <utility>
9
10
11

namespace elsa
{
David Frank's avatar
David Frank committed
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
    namespace math
    {
        /// Compute factorial \f$n!\f$ recursively
        constexpr inline index_t factorial(index_t n) noexcept
        {
            return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
        }

        /// Compute binomial coefficient
        constexpr inline index_t binom(index_t n, index_t k) noexcept
        {
            return (k > n)
                       ? 0
                       : (k == 0 || k == n) ? 1
                                            : (k == 1 || k == n - 1)
                                                  ? n
                                                  : (k + k < n) ? (binom(n - 1, k - 1) * n) / k
                                                                : (binom(n - 1, k) * n) / (n - k);
        }

        /// Compute Heaviside-function
        /// \f[
        /// x \mapsto
        /// \begin{cases}
        /// 0: & x < 0 \\
        /// c: & x = 0 \\
        /// 1: & x > 0
        /// \end{cases}
        /// \f]
        template <typename data_t>
        constexpr data_t heaviside(data_t x1, data_t c)
        {
            if (x1 == 0) {
                return c;
            } else if (x1 < 0) {
                return 0;
            } else {
                return 1;
            }
        }
    } // namespace math

andibraimllari's avatar
andibraimllari committed
54
    namespace statistics
55
    {
andibraimllari's avatar
andibraimllari committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        /**
         * @brief Compute the Mean Squared Error of two given signals.
         *
         * Calculate it based on the formula of @f$ \frac{1}{n} \sum_{i=1}^n (x_{i} - y_{i})^{2}
         * @f$.
         *
         * @param dc1 DataContainer signal
         * @param dc2 DataContainer signal
         *
         * @author Andi Braimllari - initial code
         *
         * @tparam data_t data type for the domain and range of the problem, defaulting to real_t
         */
        template <typename data_t = real_t>
        data_t meanSquaredError(DataContainer<data_t> dc1, DataContainer<data_t> dc2)
        {
            if (dc1.getDataDescriptor() != dc2.getDataDescriptor()) {
73
                throw InvalidArgumentError(
AndiBraimllari's avatar
AndiBraimllari committed
74
                    "Statistics::meanSquaredError: shapes of both signals should match");
andibraimllari's avatar
andibraimllari committed
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
            }

            DataContainer<data_t> diff = dc1 - dc2;
            return diff.squaredL2Norm() / dc1.getSize();
        }

        /// @brief Calculate mean and standard deviation of a container
        /// @param v Any container, such as `std::vector`
        /// @tparam Container Container type of argument (e.g. `std::vector`)
        /// @return a pair of mean and standard deviation (of type `Container::value_type`)
        template <typename Container>
        constexpr auto calculateMeanStddev(Container v)
            -> std::pair<typename Container::value_type, typename Container::value_type>
        {
            // value type of the vector
            using T = typename Container::value_type;

            // Sum all elements and divide by the size to get mean
            auto sum = std::accumulate(std::begin(v), std::end(v), T());
            auto mean = sum / static_cast<T>(std::size(v));

            // New vector with x_i - mean entries
            std::vector<T> diff(v.size());
            std::transform(std::begin(v), std::end(v), std::begin(diff),
                           [mean](auto x) { return x - mean; });

            // Sum of product of each element
            auto sqSum =
                std::inner_product(std::begin(diff), std::end(diff), std::begin(diff), T());
104
            auto stdev = std::sqrt(sqSum / static_cast<T>(std::size(v)));
andibraimllari's avatar
andibraimllari committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

            return std::make_pair(mean, stdev);
        }

        /**
         * @brief Compute the 95% confidence interval for a given number of samples `n`
         * and the mean and standard deviation of the measurements.
         *
         * Compute it as \f$mean - c(n) * stddev, mean + c(n) * stddev\f$, where
         * \f$c(n)\f$ is the n-th entry in the two tails T distribution table. For \f$n > 30\f$,
         * it is assumed that \f$n = 1.96\f$.
         *
         * @param n Number of of samples
         * @param mean mean of samples
         * @param stddev standard deviation of samples
         * @return pair of lower and upper bound of 95% confidence interval
         */
        template <typename data_t = real_t>
        std::pair<real_t, real_t> confidenceInterval95(std::size_t n, data_t mean, data_t stddev)
        {
            // Do we run often enough to assume a large sample size?
            if (n > 30) {
                // 1.96 is a magic number for 95% confidence intervals, equivalent to c in the other
                // branch
                const auto lower = mean - 1.96 * stddev;
                const auto upper = mean + 1.96 * stddev;
                return std::make_pair(lower, upper);
            } else {
                // t Table for 95% with offset to handle 1 iteration
                // In that case the mean is the lower and upper bound
                constexpr std::array c = {
                    0.0,      12.70620, 4.302653, 3.182446, 2.776445, 2.570582, 2.446912, 2.364624,
                    2.306004, 2.262157, 2.228139, 2.200985, 2.178813, 2.160369, 2.144787, 2.131450,
                    2.119905, 2.109816, 2.100922, 2.093024, 2.085963, 2.079614, 2.073873, 2.068658,
                    2.063899, 2.059539, 2.055529, 2.051831, 2.048407, 2.045230};

                const auto degreeOfFreedome = n - 1;
                const auto lower = mean - c[degreeOfFreedome] * stddev;
                const auto upper = mean + c[degreeOfFreedome] * stddev;
                return std::make_pair(lower, upper);
            }
        }

        /**
         * @brief Compute the Relative Error between two given signals.
         *
         * Calculate it based on the formula of @f$ \| x - y \|_{2} / \| y \|_{2} @f$.
         *
         * @param dc1 DataContainer signal
         * @param dc2 DataContainer signal
         *
         * @author Andi Braimllari - initial code
         *
         * @tparam data_t data type for the domain and range of the problem, defaulting to real_t
         */
        template <typename data_t = real_t>
        data_t relativeError(DataContainer<data_t> dc1, DataContainer<data_t> dc2)
        {
            if (dc1.getDataDescriptor() != dc2.getDataDescriptor()) {
                throw InvalidArgumentError(
AndiBraimllari's avatar
AndiBraimllari committed
165
                    "statistics::relativeError: shapes of both signals should match");
andibraimllari's avatar
andibraimllari committed
166
167
168
169
            }

            DataContainer<data_t> diff = dc1 - dc2;
            return diff.l2Norm() / dc2.l2Norm();
170
        }
andibraimllari's avatar
andibraimllari committed
171
172
173
174

        /**
         * @brief Compute the Peak Signal-to-Noise Ratio of a given signal S.
         *
AndiBraimllari's avatar
AndiBraimllari committed
175
176
177
         * Calculate it based on the formula of @f$ 20 * log_{10}(MAX_{I}) - 10 * log_{10}(MSE) @f$
         * in which @f$ MAX_{I} @f$ is the maximum possible pixel value of the image and @f$ MSE @f$
         * is the mean squared error.
andibraimllari's avatar
andibraimllari committed
178
179
180
181
182
183
184
185
186
187
188
189
190
         *
         * @param dc1 DataContainer signal
         * @param dc2 DataContainer signal
         *
         * @author Andi Braimllari - initial code
         *
         * @tparam data_t data type for the domain and range of the problem, defaulting to real_t
         */
        template <typename data_t = real_t>
        data_t peakSignalToNoiseRatio(DataContainer<data_t> dc1, DataContainer<data_t> dc2)
        {
            if (dc1.getDataDescriptor() != dc2.getDataDescriptor()) {
                throw InvalidArgumentError(
AndiBraimllari's avatar
AndiBraimllari committed
191
                    "statistics::peakSignalToNoiseRatio: shapes of both signals should match");
andibraimllari's avatar
andibraimllari committed
192
193
            }

AndiBraimllari's avatar
AndiBraimllari committed
194
            return 20 * std::log10(dc1.maxElement()) - 10 * std::log10(meanSquaredError(dc1, dc2));
andibraimllari's avatar
andibraimllari committed
195
196
        }
    } // namespace statistics
197
198
199

    namespace shearlet
    {
andibraimllari's avatar
andibraimllari committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        /// proposed in Y. Meyer, Oscillating Patterns in Image Processing and Nonlinear Evolution
        /// Equations. AMS, 2001
        template <typename data_t>
        data_t meyerFunction(data_t x)
        {
            if (x < 0) {
                return 0;
            } else if (0 <= x && x <= 1) {
                return 35 * std::pow(x, 4) - 84 * std::pow(x, 5) + 70 * std::pow(x, 6)
                       - 20 * std::pow(x, 7);
            } else {
                return 1;
            }
        }

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        /// defined in Sören Häuser and Gabriele Steidl, Fast Finite Shearlet Transform: a
        /// tutorial, 2014
        template <typename data_t>
        data_t b(data_t w)
        {
            if (1 <= std::abs(w) && std::abs(w) <= 2) {
                return std::sin(pi<data_t> / 2.0 * meyerFunction(std::abs(w) - 1));
            } else if (2 < std::abs(w) && std::abs(w) <= 4) {
                return std::cos(pi<data_t> / 2.0 * meyerFunction(1.0 / 2 * std::abs(w) - 1));
            } else {
                return 0;
            }
        }

        /// defined in Sören Häuser and Gabriele Steidl, Fast Finite Shearlet Transform: a
        /// tutorial, 2014
        template <typename data_t>
        data_t phi(data_t w)
        {
            if (std::abs(w) <= 1.0 / 2) {
                return 1;
            } else if (1.0 / 2 < std::abs(w) && std::abs(w) < 1) {
                return std::cos(pi<data_t> / 2.0 * meyerFunction(2 * std::abs(w) - 1));
            } else {
                return 0;
            }
        }

        /// defined in Sören Häuser and Gabriele Steidl, Fast Finite Shearlet Transform: a
        /// tutorial, 2014
        template <typename data_t>
        data_t phiHat(data_t w, data_t h)
        {
            if (std::abs(h) <= std::abs(w)) {
                return phi(w);
            } else {
                return phi(h);
            }
        }

        /// defined in Sören Häuser and Gabriele Steidl, Fast Finite Shearlet Transform: a
        /// tutorial, 2014
        template <typename data_t>
        data_t psiHat1(data_t w)
        {
            return std::sqrt(std::pow(b(2 * w), 2) + std::pow(b(w), 2));
        }

        /// defined in Sören Häuser and Gabriele Steidl, Fast Finite Shearlet Transform: a
        /// tutorial, 2014
        template <typename data_t>
        data_t psiHat2(data_t w)
        {
            if (w <= 0) {
                return std::sqrt(meyerFunction(1 + w));
            } else {
                return std::sqrt(meyerFunction(1 - w));
            }
        }

        /// defined in Sören Häuser and Gabriele Steidl, Fast Finite Shearlet Transform: a
        /// tutorial, 2014
        template <typename data_t>
        data_t psiHat(data_t w, data_t h)
        {
            if (w == 0) {
                return 0;
            } else {
                return psiHat1(w) * psiHat2(h / w);
            }
        }
    } // namespace shearlet
} // namespace elsa