-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_standard_score.f90
378 lines (286 loc) · 16.1 KB
/
mo_standard_score.f90
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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
!> \file mo_standard_score.f90
!> \brief Routines for calculating the normalization (anomaly)/standard score/z score and the
!> deseasonalized (standard score on monthly basis) values of a time series.
!> \details In environmental research often the centralization and standardization are estimated
!> for characterizing the dynamics of a signal.
!> \author Matthias Zink
!> \date May 2015
MODULE mo_standard_score
! This module contains routines for the masked calculation of
! the standard_score of a time series (centralized and standardized time series).
! Written May 2015, Matthias Zink
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2015 Matthias Zink
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, sp, dp
IMPLICIT NONE
PUBLIC :: standard_score ! standard score of a population
PUBLIC :: classified_standard_score ! standard score for classes of a population (e.g. classes=months)
! ------------------------------------------------------------------
! NAME
! standard_score
! PURPOSE
!> \brief Calculates the standard score / normalization (anomaly) / z-score.
!> \details In statistics, the standard score is the (signed) number of standard deviations an observation
!> or datum is above the mean. Thus, a positive standard score indicates a datum above the mean,
!> while a negative standard score indicates a datum below the mean.
!> It is a dimensionless quantity obtained by subtracting the population mean from
!> an individual raw score and then dividing the difference by the population standard deviation.
!> This conversion process is called standardizing or normalizing (however, "normalizing" can
!> refer to many types of ratios).\n
!> Standard scores are also called z-values, z-scores, normal scores, and standardized variables; the use
!> of "Z" is because the normal distribution is also known as the "Z distribution". They are most frequently
!> used to compare a sample to a standard normal deviate, though they can be defined without assumptions of
!> normality (Wikipedia, May 2015).
!>
!> \f[ standard\_score = \frac{x - \mu_x}{\sigma_x} \f]
!> where \f$ \mu_x \f$ is the mean of a population \f$ x \f$ and \f$ \sigma_x \f$ its standard deviation.
!>
!> If an optional mask is given, the calculations are over those locations that correspond
!> to true values in the mask.
! CALLING SEQUENCE
! out = standard_score(data, mask=mask)
! INDENT(IN)
!> \param[in] "real(sp/dp), dimension(:) :: data" data to calculate the standard score for
! INDENT(INOUT)
! None
! INDENT(OUT)
! None
! INDENT(IN), OPTIONAL
!> \param[in] "logical, dimension(:),optinal :: mask" indication which cells to use for calculation
!> If present, only those locations in mask having true values in mask are evaluated.
! INDENT(INOUT), OPTIONAL
! None
! INDENT(OUT), OPTIONAL
! None
! RETURN
!> \return real(sp/dp) :: standard_score — standard score / normalization (anomaly) / z-score
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! data = (/ 1., 2, 3., -999., 5., 6. /)
! out = standard_score(data, mask=(data >= 0.))
! -> see also example in test directory
! LITERATURE
!> \note Richard J. Larsen and Morris L. Marx (2000) An Introduction to Mathematical Statistics and Its
!> Applications, Third Edition, ISBN 0-13-922303-7. p. 282.
! HISTORY
!> \author Matthias Zink
!> \date May 2015
INTERFACE standard_score
MODULE PROCEDURE standard_score_sp, standard_score_dp
END INTERFACE standard_score
! ------------------------------------------------------------------
! NAME
! classified_standard_score
! PURPOSE
!> \brief Calculates the classified standard score (e.g. classes are months).
!> \details In statistics, the standard score is the (signed) number of standard deviations an observation
!> or datum is above the mean. Thus, a positive standard score indicates a datum above the mean,
!> while a negative standard score indicates a datum below the mean.
!> It is a dimensionless quantity obtained by subtracting the population mean from
!> an individual raw score and then dividing the difference by the population standard deviation.
!> This conversion process is called standardizing or normalizing (however, "normalizing" can
!> refer to many types of ratios).\n
!> Standard scores are also called z-values, z-scores, normal scores, and standardized variables; the use
!> of "Z" is because the normal distribution is also known as the "Z distribution". They are most frequently
!> used to compare a sample to a standard normal deviate, though they can be defined without assumptions of
!> normality (Wikipedia, May 2015).\n
!> In this particular case the standard score is calculated for means and standard deviations derived from
!> classes of the time series. Such classes could be for example months. Thus, the output would be a
!> deseasonalized time series.
!>
!> \f[ classified\_standard\_score = \frac{x_i - \mu_{c_{x_i}}}{\sigma_{c_{x_i}}} \f]
!> where \f$ x_i \f$ is an element of class \f$ c_{x_i} \f$. \f$ x \f$ is a population, \f$ \mu_{c_{x_i}} \f$
!> is the mean of all members of a class \f$ c_{x_i} \f$ and \f$ \sigma_{c_{x_i}} \f$ its standard deviation.
!>
!> If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! CALLING SEQUENCE
! out = classified_standard_score(data, mask=mask)
! INDENT(IN)
!> \param[in] "integer, dimension(:) :: classes" classes to categorize data (e.g. months)
!> \param[in] "real(sp/dp), dimension(:) :: data" data to calculate the standard score for
! INDENT(INOUT)
! None
! INDENT(OUT)
! None
! INDENT(IN), OPTIONAL
!> \param[in] "logical, dimension(:), optional :: mask" indication which cells to use for calculation
!> If present, only those locations in mask having true values in mask are evaluated.
! INDENT(INOUT), OPTIONAL
! None
! INDENT(OUT), OPTIONAL
! None
! RETURN
!> \return real(sp/dp) :: classified_standard_score — classified standard score (e.g. deseasonalized
!> time series)
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! data = (/ 1., 2, 3., -999., 5., 6. /)
! classes = (/ 1, 1, 1, 2, 2 , 2 /)
! out = classified_standard_score(data, classes, mask=(data >= 0.))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Zink
!> \date May 2015
INTERFACE classified_standard_score
MODULE PROCEDURE classified_standard_score_sp, classified_standard_score_dp
END INTERFACE classified_standard_score
! ------------------------------------------------------------------
PRIVATE
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
FUNCTION standard_score_sp(data, mask)
use mo_moment, only: average, stddev
implicit none
real(sp), dimension(:), intent(in) :: data ! data arrau input
logical, dimension(:), optional, intent(in) :: mask ! optional input
real(sp), dimension(size(data, dim=1)) :: standard_score_sp
! local
logical, dimension(size(data, dim=1)) :: maske
! check if optional mask matches shape of data
if (present(mask)) then
if (size(mask) .ne. size(data)) stop '***Error: standard_score_sp: size(mask) .ne. size(data)'
maske = mask
else
maske(:) = .true.
endif
! check if enough values (>1) are available
if (count(maske) .LE. 2) stop '***Error: standard_score_sp: less than 2 elements avaiable'
standard_score_sp = ( data(:) - average(data, mask=maske) ) / stddev(data, mask=maske, ddof=1_i4)
END FUNCTION standard_score_sp
FUNCTION standard_score_dp(data, mask)
use mo_moment, only: average, stddev
implicit none
real(dp), dimension(:), intent(in) :: data ! data arrau input
logical, dimension(:), optional, intent(in) :: mask ! optional input
real(dp), dimension(size(data, dim=1)) :: standard_score_dp
! local
logical, dimension(size(data, dim=1)) :: maske
! check if optional mask matches shape of data
if (present(mask)) then
if (size(mask) .ne. size(data)) stop '***Error: standard_score_dp: size(mask) .ne. size(data)'
maske = mask
else
maske(:) = .true.
endif
! check if enough values (>1) are available
if (count(maske) .LE. 2) stop '***Error: standard_score_dp: less than 2 elements avaiable'
standard_score_dp = ( data(:) - average(data, mask=maske) ) / stddev(data, mask=maske, ddof=1_i4)
END FUNCTION standard_score_dp
! ------------------------------------------------------------------
FUNCTION classified_standard_score_sp(data, classes, mask)
use mo_moment, only: average, stddev
use mo_orderpack, only: unista
implicit none
real(sp), dimension(:), intent(in) :: data ! data array with input
integer, dimension(:), intent(in) :: classes ! array indicateing classes
logical, dimension(:), optional, intent(in) :: mask ! array masking elements of data
real(sp), dimension(size(data, dim=1)) :: classified_standard_score_sp
! local
integer(i4) :: iclass, ielem ! loop variable
integer(i4) :: number_of_classes ! number of unique classes in vector
! classes
integer(i4), dimension(size(data, dim=1)) :: unique_classes ! vector of uniqe classes
real(sp) :: class_mean ! mean of class
real(sp) :: class_stddev ! standard deviation of class
logical, dimension(size(data, dim=1)) :: maske ! data mask
logical, dimension(size(data, dim=1)) :: mask_class_maske ! combined mask for current class and
! maske
! check if optional mask matches shape of data
if (present(mask)) then
if (size(mask) .ne. size(data)) stop '***Error: classified_standard_score_sp: size(mask) .ne. size(data)'
maske = mask
else
maske(:) = .true.
endif
! check if enough values (>1) are available
if (count(maske) .LE. 2) stop '***Error: classified_standard_score_sp: less than 2 elements available'
! initialization
classified_standard_score_sp = 0.0_sp
! write classes to new array for getting unique array elements
unique_classes = classes
call unista(unique_classes, number_of_classes) ! (unique arry elements in the 1:number_of_classes
! ! indexes of array unique_classes)
! loop over classes
do iclass = 1, number_of_classes
! calculate mean and standard deviation for class
mask_class_maske = (maske .AND. (classes==unique_classes(iclass)))
class_mean = average(data, mask=mask_class_maske)
class_stddev = stddev(data, mask=mask_class_maske, ddof=1_i4)
! loop over array elements
do ielem = 1, size(data, dim=1)
if (.NOT. mask_class_maske(ielem)) cycle ! skip masked values and other classes
classified_standard_score_sp(ielem) = ( data(ielem) - class_mean ) / class_stddev
end do
end do
END FUNCTION classified_standard_score_sp
FUNCTION classified_standard_score_dp(data, classes, mask)
use mo_moment, only: average, stddev
use mo_orderpack, only: unista
implicit none
real(dp), dimension(:), intent(in) :: data ! data array with input
integer, dimension(:), intent(in) :: classes ! array indicateing classes
logical, dimension(:), optional, intent(in) :: mask ! array masking elements of data
real(dp), dimension(size(data, dim=1)) :: classified_standard_score_dp
! local
integer(i4) :: iclass, ielem ! loop variable
integer(i4) :: number_of_classes ! number of unique classes in vector classes
integer(i4), dimension(size(data, dim=1)) :: unique_classes ! vector of uniqe classes
real(dp) :: class_mean ! mean of class
real(dp) :: class_stddev ! standard deviation of class
logical, dimension(size(data, dim=1)) :: maske ! data mask
logical, dimension(size(data, dim=1)) :: mask_class_maske ! combined mask for current class and maske
! check if optional mask matches shape of data
if (present(mask)) then
if (size(mask) .ne. size(data)) stop '***Error: classified_standard_score_dp: size(mask) .ne. size(data)'
maske = mask
else
maske(:) = .true.
endif
! check if enough values (>1) are available
if (count(maske) .LE. 2) stop '***Error: classified_standard_score_dp: less than 2 elements avaiable'
! initialization
classified_standard_score_dp = 0.0_dp
! write classes to new array for getting unique array elements
unique_classes = classes
call unista(unique_classes, number_of_classes) ! (unique arry elements in the 1:number_of_classes
! ! indexes of array unique_classes)
! loop over classes
do iclass = 1, number_of_classes
! calculate mean and standard deviation for class
mask_class_maske = (maske .AND. (classes==unique_classes(iclass)))
class_mean = average(data, mask=mask_class_maske)
class_stddev = stddev(data, mask=mask_class_maske, ddof=1_i4)
! loop over array elements
do ielem = 1, size(data, dim=1)
if (.NOT. mask_class_maske(ielem)) cycle ! skip masked values and other classes
classified_standard_score_dp(ielem) = ( data(ielem) - class_mean ) / class_stddev
end do
end do
END FUNCTION classified_standard_score_dp
END MODULE mo_standard_score