Skip to content

Commit 5e35830

Browse files
committed
Changed default result from raw moment to central moment
1) Current default result: central moment: 2) `center`: optional. Must be a scalar if `dim` is not provided, OR Can be a scalar or an array if `dim` is provided.
2 parents cb69df8 + aafbcc6 commit 5e35830

File tree

5 files changed

+1019
-694
lines changed

5 files changed

+1019
-694
lines changed

src/stdlib_experimental_stats.fypp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#:include "common.fypp"
22
#:set RANKS = range(1, MAXRANK + 1)
3+
#:set REDRANKS = range(2, MAXRANK + 1)
34
#:set RC_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES
45
module stdlib_experimental_stats
56
use stdlib_experimental_kinds, only: sp, dp, qp, &
@@ -236,6 +237,20 @@ module stdlib_experimental_stats
236237
#:endfor
237238
#:endfor
238239

240+
#:for k1, t1 in RC_KINDS_TYPES
241+
#:for rank in REDRANKS
242+
#:set RName = rname("moment_scalar",rank, t1, k1)
243+
module function ${RName}$(x, order, dim, center, mask) result(res)
244+
${t1}$, intent(in) :: x${ranksuffix(rank)}$
245+
integer, intent(in) :: order
246+
integer, intent(in) :: dim
247+
${t1}$, intent(in) :: center
248+
logical, intent(in), optional :: mask
249+
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$
250+
end function ${RName}$
251+
#:endfor
252+
#:endfor
253+
239254
#:for k1, t1 in RC_KINDS_TYPES
240255
#:for rank in RANKS
241256
#:set RName = rname("moment",rank, t1, k1)
@@ -250,6 +265,20 @@ module stdlib_experimental_stats
250265
#:endfor
251266
#:endfor
252267

268+
#:for k1, t1 in INT_KINDS_TYPES
269+
#:for rank in REDRANKS
270+
#:set RName = rname("moment_scalar",rank, t1, k1, 'dp')
271+
module function ${RName}$(x, order, dim, center, mask) result(res)
272+
${t1}$, intent(in) :: x${ranksuffix(rank)}$
273+
integer, intent(in) :: order
274+
integer, intent(in) :: dim
275+
real(dp),intent(in) :: center
276+
logical, intent(in), optional :: mask
277+
real(dp) :: res${reduced_shape('x', rank, 'dim')}$
278+
end function ${RName}$
279+
#:endfor
280+
#:endfor
281+
253282
#:for k1, t1 in INT_KINDS_TYPES
254283
#:for rank in RANKS
255284
#:set RName = rname("moment",rank, t1, k1, 'dp')
@@ -290,6 +319,20 @@ module stdlib_experimental_stats
290319
#:endfor
291320
#:endfor
292321

322+
#:for k1, t1 in RC_KINDS_TYPES
323+
#:for rank in REDRANKS
324+
#:set RName = rname("moment_mask_scalar",rank, t1, k1)
325+
module function ${RName}$(x, order, dim, center, mask) result(res)
326+
${t1}$, intent(in) :: x${ranksuffix(rank)}$
327+
integer, intent(in) :: order
328+
integer, intent(in) :: dim
329+
${t1}$, intent(in) :: center
330+
logical, intent(in) :: mask${ranksuffix(rank)}$
331+
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$
332+
end function ${RName}$
333+
#:endfor
334+
#:endfor
335+
293336
#:for k1, t1 in RC_KINDS_TYPES
294337
#:for rank in RANKS
295338
#:set RName = rname("moment_mask",rank, t1, k1)
@@ -304,6 +347,20 @@ module stdlib_experimental_stats
304347
#:endfor
305348
#:endfor
306349

350+
#:for k1, t1 in INT_KINDS_TYPES
351+
#:for rank in REDRANKS
352+
#:set RName = rname("moment_mask_scalar",rank, t1, k1, 'dp')
353+
module function ${RName}$(x, order, dim, center, mask) result(res)
354+
${t1}$, intent(in) :: x${ranksuffix(rank)}$
355+
integer, intent(in) :: order
356+
integer, intent(in) :: dim
357+
real(dp), intent(in) :: center
358+
logical, intent(in) :: mask${ranksuffix(rank)}$
359+
real(dp) :: res${reduced_shape('x', rank, 'dim')}$
360+
end function ${RName}$
361+
#:endfor
362+
#:endfor
363+
307364
#:for k1, t1 in INT_KINDS_TYPES
308365
#:for rank in RANKS
309366
#:set RName = rname("moment_mask",rank, t1, k1, 'dp')

src/stdlib_experimental_stats.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Descriptive statistics
22

33
* [`mean` - mean of array elements](#mean---mean-of-array-elements)
4-
* [`moment` - raw and central moments of array elements](#moment---raw-and-central-moments-of-array-elements)
4+
* [`moment` - central moments of array elements](#moment---central-moments-of-array-elements)
55
* [`var` - variance of array elements](#var---variance-of-array-elements)
66

77
## `mean` - mean of array elements
@@ -48,24 +48,24 @@ program demo_mean
4848
end program demo_mean
4949
```
5050

51-
## `moment` - raw and central moments of array elements
51+
## `moment` - central moments of array elements
5252

5353
### Description
5454

55-
Returns the _k_-th order raw moment of all the elements of `array`, or of the elements of `array` along dimension `dim` if provided, and if the corresponding element in `mask` is `true`.
55+
Returns the _k_-th order central moment of all the elements of `array`, or of the elements of `array` along dimension `dim` if provided, and if the corresponding element in `mask` is `true`.
5656

57-
If an array `center` is provided, the function returns the _k_-th order central moment of all the elements of `array`, or of the elements of `array` along dimension `dim` if provided, and if the corresponding element in `mask` is `true`.
57+
If a scalar or an array `center` is provided, the function returns the _k_-th order moment about 'center', of all the elements of `array`, or of the elements of `array` along dimension `dim` if provided, and if the corresponding element in `mask` is `true`.
5858

5959

60-
The _k_-th order raw moment is defined as :
60+
The _k_-th order central moment is defined as :
6161

6262
```
63-
moment(array) = 1/n sum_i (array(i))^k
63+
moment(array) = 1/n sum_i (array(i) - mean(array))^k
6464
```
6565

6666
where n is the number of elements.
6767

68-
The _k_-th order central moment is defined as :
68+
The _k_-th order moment about `center` is defined as :
6969

7070
```
7171
moment(array) = 1/n sum_i (array(i) - center)^k
@@ -85,7 +85,7 @@ The _k_-th order central moment is defined as :
8585

8686
`dim`: Shall be a scalar of type `integer` with a value in the range from 1 to n, where n is the rank of `array`.
8787

88-
`center` (optional): Shall be a scalar of the same type of `array` if `array` is `real` or `complex`, or of type `real(dp)` if `array` is of type `integer`. If `dim` is provided, `center` shall be an array (with a shape similar to that of `array` with dimension `dim` dropped) of the same type of `array` if `array` is `real` or `complex`, or of type `real(dp)` if `array` is of type `integer`.
88+
`center` (optional): Shall be a scalar of the same type of `result` if `dim` is not provided. If `dim` is provided, `center` shall be a scalar or an array (with a shape similar to that of `array` with dimension `dim` dropped) of the same type of `result`.
8989

9090
`mask` (optional): Shall be of type `logical` and either by a scalar or an array of the same shape as `array`.
9191

@@ -94,9 +94,9 @@ The _k_-th order central moment is defined as :
9494
If `array` is of type `real` or `complex`, the result is of the same type as `array`.
9595
If `array` is of type `integer`, the result is of type `real(dp)`.
9696

97-
If `dim` is absent, a scalar with the _k_-th raw (or central if `center` is provided) moment of all elements in `array` is returned. Otherwise, an array of rank n-1, where n equals the rank of `array`, and a shape similar to that of `array` with dimension `dim` dropped is returned.
97+
If `dim` is absent, a scalar with the _k_-th (central) moment of all elements in `array` is returned. Otherwise, an array of rank n-1, where n equals the rank of `array`, and a shape similar to that of `array` with dimension `dim` dropped is returned.
9898

99-
If `mask` is specified, the result is the _k_-th raw (or central if `center` is provided) moment of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
99+
If `mask` is specified, the result is the _k_-th (central) moment of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
100100

101101
### Example
102102

@@ -106,14 +106,12 @@ program demo_moment
106106
implicit none
107107
real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ]
108108
real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3])
109-
print *, moment(x, 2, center = mean(x)) !returns 2.9167
110-
print *, moment( y, 2,&
111-
center = mean(y)) !returns 2.9167
112-
print *, moment( y, 2, 1,&
113-
center = mean(y, 1)) !returns [0.25, 0.25, 0.25]
114-
print *, moment( y, 2, 1,&
115-
center = mean(y, 1, y > 3.),&
116-
mask = (y > 3.)) !returns [NaN, 0., 0.25]
109+
print *, moment(x, 2) !returns 2.9167
110+
print *, moment( y, 2) !returns 2.9167
111+
print *, moment( y, 2, 1) !returns [0.25, 0.25, 0.25]
112+
print *, moment( y, 2, 1, mask = (y > 3.)) !returns [NaN, 0., 0.25]
113+
print *, moment(x, 2, center = 0.) !returns 15.1667
114+
print *, moment( y, 1, 1, center = 0.) !returns [1.5, 3.5, 5.5]
117115
end program demo_moment
118116
```
119117

0 commit comments

Comments
 (0)