-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.h
142 lines (116 loc) · 2.15 KB
/
Student.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
//
// FILE: Student.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Arduino library for Student or T-distribution math.
// DATE: 2024-07-22
#include "Arduino.h"
#include "StudentTable.h"
#define STUDENT_LIB_VERSION (F("0.1.0"))
class Student
{
public:
Student()
{
_size = STUDENT_MAX_SIZE;
_count = 0;
}
uint8_t getSize()
{
return _size;
}
uint8_t getCount()
{
return _count;
}
void reset()
{
_count = 0;
}
//
// ADD
//
bool add(float value)
{
if (_count >= _size) return false;
_value[_count++] = value;
return true;
}
bool add(float *array, uint8_t size)
{
if (_count + size > _size) return false;
for (int i = 0; i < size; i++)
{
add(array[i]);
}
return true;
}
//
// MATH
//
float mean()
{
float sum = 0;
for (int i = 0; i < _count; i++)
{
sum += _value[i];
}
return sum / _count;
}
float variance()
{
if (_count < 2) return NAN;
float m = mean();
float var = 0;
for (int i = 0; i < _count; i++)
{
var += (_value[i] - m)*(_value[i] - m);
}
var /= (_count - 1);
return var;
}
float deviation()
{
if (_count < 2) return NAN;
float var = variance();
return sqrt(var);
}
float estimatedDeviation()
{
if (_count < 2) return NAN;
float dev = deviation();
return dev / sqrt(_count);
}
//
// INTERVAL
//
float intervalDelta(int confidence)
{
if (_count < 2) return NAN;
// degrees freedom
int df = _count - 1;
int idx = 2; // 95% default
if (confidence == 80) idx = 0;
if (confidence == 90) idx = 1;
if (confidence == 98) idx = 3;
if (confidence == 99) idx = 4;
// interpolation possible
float t = StudentLUT[df][idx] * 0.001;
return estimatedDeviation() * t;
}
float meanLower(int confidence)
{
return mean() - intervalDelta(confidence);
}
float meanUpper(int confidence)
{
return mean() + intervalDelta(confidence);
}
private:
uint8_t _size;
uint8_t _count;
float _mean = 0;
float _value[STUDENT_MAX_SIZE];
};
// -- END OF FILE --