DotNetDupe 5.0.4
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
MathStatistics.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "System/Math.h"
5#include <cmath>
6
7namespace DotNetDupe {
8 namespace System {
9
12 int iCount = listValues.GetCount();
13 if (iCount == 0) throw ArgumentException("Values list cannot be empty.");
14 double dSum = 0.0;
15 for (int i = 0; i < iCount; ++i) {
16 dSum += listValues[i];
17 }
18 return dSum / static_cast<double>(iCount);
19 }
20
21 double Math::Variance(const Collections::Generic::List<double>& listValues, bool bIsSample) {
23 int iCount = listValues.GetCount();
24 int iMinCount = bIsSample ? 2 : 1;
25 if (iCount < iMinCount) throw ArgumentException("Insufficient values to compute variance.");
26 double dMean = Mean(listValues);
27 double dSumSquares = 0.0;
28 for (int i = 0; i < iCount; ++i) {
29 double dDiff = listValues[i] - dMean;
30 dSumSquares += dDiff * dDiff;
31 }
32 int iDivisor = bIsSample ? (iCount - 1) : iCount;
33 return dSumSquares / static_cast<double>(iDivisor);
34 }
35
36 double Math::StandardDeviation(const Collections::Generic::List<double>& listValues, bool bIsSample) {
38 return std::sqrt(Variance(listValues, bIsSample));
39 }
40
41 double Math::StdDev(const Collections::Generic::List<double>& listValues, bool bIsSample) {
43 return StandardDeviation(listValues, bIsSample);
44 }
45
46 double Math::MovingAverage(const Collections::Generic::List<double>& listValues, int iPeriod) {
48 if (iPeriod <= 0) throw ArgumentOutOfRangeException("Period must be greater than zero.");
49 int iCount = listValues.GetCount();
50 if (iCount < iPeriod) throw ArgumentException("Not enough data points for moving average window.");
51 double dSum = 0.0;
52 int iStart = iCount - iPeriod;
53 for (int i = iStart; i < iCount; ++i) {
54 dSum += listValues[i];
55 }
56 return dSum / static_cast<double>(iPeriod);
57 }
58
60 const Collections::Generic::List<double>& listValues, int iPeriod) {
62 if (iPeriod <= 0) throw ArgumentOutOfRangeException("Period must be greater than zero.");
64 int iCount = listValues.GetCount();
65 if (iCount < iPeriod) return listResult;
66 double dCurrentSum = 0.0;
67 for (int i = 0; i < iPeriod; ++i) {
68 dCurrentSum += listValues[i];
69 }
70 listResult.Add(dCurrentSum / static_cast<double>(iPeriod));
71 for (int i = iPeriod; i < iCount; ++i) {
72 dCurrentSum += listValues[i] - listValues[i - iPeriod];
73 listResult.Add(dCurrentSum / static_cast<double>(iPeriod));
74 }
75 return listResult;
76 }
77
78 static double ComputeInitialSma(const Collections::Generic::List<double>& listValues, int iPeriod) {
80 double dSum = 0.0;
81 for (int i = 0; i < iPeriod; ++i) {
82 dSum += listValues[i];
83 }
84 return dSum / static_cast<double>(iPeriod);
85 }
86
88 const Collections::Generic::List<double>& listValues, int iPeriod, double dSmoothing) {
90 if (iPeriod <= 0) throw ArgumentOutOfRangeException("Period must be greater than zero.");
91 int iCount = listValues.GetCount();
92 if (iCount < iPeriod) throw ArgumentException("Not enough data points for EMA.");
93 double dMultiplier = dSmoothing / (static_cast<double>(iPeriod) + 1.0);
94 double dEma = ComputeInitialSma(listValues, iPeriod);
95 for (int i = iPeriod; i < iCount; ++i) {
96 dEma = (listValues[i] - dEma) * dMultiplier + dEma;
97 }
98 return dEma;
99 }
100
102 const Collections::Generic::List<double>& listValues, int iPeriod, double dSmoothing) {
104 if (iPeriod <= 0) throw ArgumentOutOfRangeException("Period must be greater than zero.");
106 int iCount = listValues.GetCount();
107 if (iCount < iPeriod) return listResult;
108 double dMultiplier = dSmoothing / (static_cast<double>(iPeriod) + 1.0);
109 double dEma = ComputeInitialSma(listValues, iPeriod);
110 listResult.Add(dEma);
111 for (int i = iPeriod; i < iCount; ++i) {
112 dEma = (listValues[i] - dEma) * dMultiplier + dEma;
113 listResult.Add(dEma);
114 }
115 return listResult;
116 }
117
118 }
119}
Defines the exception thrown when an invalid argument is provided to a method.
Defines the exception thrown when an argument value is outside the acceptable range of values.
Provides constants and static methods for trigonometric, logarithmic, and other common mathematical f...
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
ArgumentOutOfRangeException(const String &sMessage)
Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message.
Represents a strongly typed list of objects accessible by index.
Definition List.h:31
void Add(const T &item) override
Adds an object to the end of the List.
Definition List.h:173
int GetCount() const override
Gets the number of elements contained in the List.
Definition List.h:131
static double Mean(const Collections::Generic::List< double > &listValues)
Computes the arithmetic mean of a sequence of double values.
static Collections::Generic::List< double > ExponentialMovingAverageSeries(const Collections::Generic::List< double > &listValues, int iPeriod, double dSmoothing=2.0)
Computes the rolling exponential moving average (EMA) series across the sequence.
static double Variance(const Collections::Generic::List< double > &listValues, bool bIsSample=false)
Computes the variance of a sequence of double values.
static double StandardDeviation(const Collections::Generic::List< double > &listValues, bool bIsSample=false)
Computes the standard deviation of a sequence of double values.
static double StdDev(const Collections::Generic::List< double > &listValues, bool bIsSample=false)
Computes the standard deviation of a sequence of double values (abbreviated alias).
static double MovingAverage(const Collections::Generic::List< double > &listValues, int iPeriod)
Computes the simple moving average (SMA) over the final window of length iPeriod.
static Collections::Generic::List< double > MovingAverageSeries(const Collections::Generic::List< double > &listValues, int iPeriod)
Computes the rolling simple moving average (SMA) series across all values.
static double ExponentialMovingAverage(const Collections::Generic::List< double > &listValues, int iPeriod, double dSmoothing=2.0)
Computes the exponential moving average (EMA) value for the sequence.
static double ComputeInitialSma(const Collections::Generic::List< double > &listValues, int iPeriod)