Question about Statistics

I'm writing a header file specialized for working with statistics. I'm a little rusty with my statistics information and was wondering if anyone would mind taking a look at it and seeing about any changes or additions that would help make it easy to add statistical analysis into programs.

Note: I'm not really sure about the comments, I tried to just make them as simple and detailed as possible.

P.S. I know that T_Score is just Z_Score copied and not changed, I haven't finished editing it yet.

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#ifndef STATS_H
#define STATS_H

#include <cmath>

//Calculates Mean and returns the result
template <class T>
double Mean ( T array[], int size ){
	
	double sum = 0;
	int i;
	
	for  ( i = 0; i < size; i++ ){
		
		sum += (double) array[i];
		
	}
	
	return ( sum / size );
	
}


//Calculates the Population Standard Deviation and returns the result
//It is overloaded to call Mean() when not given Mean
template <class T>
double Pop_Std_Dev ( T array[], int size ){
	
	double sum = 0, mean = Mean( array, size );
	int i;
	
	for ( i = 0; i < size; i++ ){
		
		sum += pow( ( mean - (double)array[i] ), 2);
		
	}
	
	return sqrt( ( sum / size ) );
	
}

template <class T>
double Pop_Std_Dev ( T array[], int size, double mean ){
	
	double sum = 0;
	int i;
	
	for ( i = 0; i < size; i++ ){
		
		sum += pow( ( mean - (double)array[i] ), 2);
		
	}
	
	return sqrt( ( sum / size ) );
	
}


//Calculates the Standard Deviation of a Sample and returns the result
//It is overloaded to call Mean() when not given Mean
template <class T>
double Samp_Std_Dev ( T array[], int size ){
	
	double sum = 0, mean = Mean( array, size );
	int i;
	
	for ( i = 0; i < size; i++ ){
		
		sum += pow( ( mean - (double)array[i] ), 2);
		
	}
	
	return sqrt( ( sum / ( size - 1) ) );
	
}

template <class T>
double Samp_Std_Dev ( T array[], int size, double mean ){
	
	double sum = 0;
	int i;
	
	for ( i = 0; i < size; i++ ){
		
		sum += pow( ( mean - (double)array[i] ), 2);
		
	}
	
	return sqrt( ( sum / ( size - 1) ) );
	
}


//Calculates the Z Score of a value, or array of values, then returns the result(s)
//It is overloaded to call Mean(), or Pop_Std_Dev() depending on what values it is called with
//Due to the Z Score only being values with a known population mean it calls Pop_Std_Dev() instead of Samp_Std_Dev()
template <class T>
double Z_Score ( T value, double mean, double Std ){
	
	return (double)( ( value - mean ) / Std );
	
}

template <class T>
double Z_Score ( T array[], int size ){
	
	int i;
	double mean = Mean( array, size ), Std = Pop_Std_Dev( array, size, mean );
	double * Z_array = new double [size];
	
	for ( i = 0; i < size; i++ )
	{
		
		Z_array[i] = ( ( array[i] - mean ) / Std );
		
	}
	
	return Z_array;
	
}

template <class T>
double Z_Score ( T array[], int size, double mean ){
	
	int i;
	double Std = Pop_Std_Dev( array, size, mean );
	double * Z_array = new double [size];
	
	for ( i = 0; i < size; i++ )
	{
		
		Z_array[i] = ( ( array[i] - mean ) / Std );
		
	}
	
	return Z_array;
	
}

template <class T>
double Z_Score ( T array[], int size, double mean, double Std ){
	
	int i;
	double * Std_array = new double [size];
	
	for ( i = 0; i < size; i++ )
	{
		
		Std_array[i] = ( ( array[i] - mean ) / Std );
		
	}
	
	return Std_array;
	
}


//Calculates the T Score of a value, or array of values, then returns the result(s)
//It is overloaded to call Mean(), or Samp_Std_Dev() depending on what values it is called with
//Due to the T Score being values with a sample mean not population it calls Samp_Std_Dev() instead of Pop_Std_Dev()
//***Note***: Since mean and standard deviation are inputed for the first function and last they are identical to their Z_Score() companions
template <class T>
double T_Score ( T value, double mean, double Std ){
	
	return (double)( ( value - mean ) / Std );
	
}

template <class T>
double T_Score ( T array[], int size ){
	
	int i;
	double mean = Mean( array, size ), Std = Samp_Std_Dev( array, size, mean );
	double * Z_array = new double [size];
	
	for ( i = 0; i < size; i++ )
	{
		
		Z_array[i] = ( ( array[i] - mean ) / Std );
		
	}
	
	return Z_array;
	
}

template <class T>
double T_Score ( T array[], int size, double mean ){
	
	int i;
	double Std = Samp_Std_Dev( array, size, mean );
	double * Z_array = new double [size];
	
	for ( i = 0; i < size; i++ )
	{
		
		Z_array[i] = ( ( array[i] - mean ) / Std );
		
	}
	
	return Z_array;
	
}

template <class T>
double T_Score ( T array[], int size, double mean, double Std ){
	
	int i;
	double * Std_array = new double [size];
	
	for ( i = 0; i < size; i++ )
	{
		
		Std_array[i] = ( ( array[i] - mean ) / Std );
		
	}
	
	return Std_array;
	
}


#endif 
Why are the functions parameterised by a template when you just ignore that type and convert it double? Isn't T the type of number? I don't think templates should be used here, just use double. If you don't want to fix it, you can use a typedef'd name like:
typedef double number_type;

later you can change it to long double or float.

I cursory glance at some of the algorithms look ok, but I haven't looked that closely.

The array being passed in should be passed by const ref rather than by ref.

Making them a template makes it so that any type of array can be passed not just an int or double array, I want it to be versatile.

After reading a bit, I can see how using a const ref instead of a ref would be better. I'll probably make that change right away.

Thanks for your suggestions.
Last edited on
So does this mean that no one is interested?
Topic archived. No new replies allowed.