comparison OrthancStone/Sources/Toolbox/LinearAlgebra.cpp @ 1984:187a261d7ae2

computation of mean and stddev in rectangle probes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 31 Oct 2022 14:42:46 +0100
parents 6ce81914f7e4
children 07964689cb0b
comparison
equal deleted inserted replaced
1983:20fa913272b7 1984:187a261d7ae2
39 39
40 namespace OrthancStone 40 namespace OrthancStone
41 { 41 {
42 namespace LinearAlgebra 42 namespace LinearAlgebra
43 { 43 {
44 void OnlineVarianceEstimator::AddSample(double value)
45 {
46 count_++;
47 sum_ += value;
48 sumOfSquares_ += value * value;
49 }
50
51
52 void OnlineVarianceEstimator::Clear()
53 {
54 count_ = 0;
55 sum_ = 0;
56 sumOfSquares_ = 0;
57 }
58
59
60 double OnlineVarianceEstimator::GetMean() const
61 {
62 if (count_ == 0)
63 {
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
65 }
66 else
67 {
68 return sum_ / static_cast<double>(count_);
69 }
70 }
71
72
73 double OnlineVarianceEstimator::GetVariance() const
74 {
75 if (count_ == 0)
76 {
77 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
78 }
79 else if (count_ == 1)
80 {
81 return 0;
82 }
83 else
84 {
85 const double n = static_cast<double>(count_);
86 return (sumOfSquares_ * n - sum_ * sum_) / (n * (n - 1.0));
87 }
88 }
89
90
91 double OnlineVarianceEstimator::GetStandardDeviation() const
92 {
93 return sqrt(GetVariance());
94 }
95
96
44 void Print(const Vector& v) 97 void Print(const Vector& v)
45 { 98 {
46 for (size_t i = 0; i < v.size(); i++) 99 for (size_t i = 0; i < v.size(); i++)
47 { 100 {
48 printf("%g\n", v[i]); 101 printf("%g\n", v[i]);