Skip to content

Commit ef6deec

Browse files
committed
rawmoment: progress
1 parent a31654c commit ef6deec

File tree

4 files changed

+131
-110
lines changed

4 files changed

+131
-110
lines changed

src/stdlib_experimental_stats.fypp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ module stdlib_experimental_stats
239239
#:for k1, t1 in RC_KINDS_TYPES
240240
#:for rank in RANKS
241241
#:set RName = rname("moment",rank, t1, k1)
242-
module function ${RName}$(x, order, center, dim, mask) result(res)
242+
module function ${RName}$(x, order, dim, center, mask) result(res)
243243
${t1}$, intent(in) :: x${ranksuffix(rank)}$
244244
integer, intent(in) :: order
245245
integer, intent(in) :: dim
@@ -253,7 +253,7 @@ module stdlib_experimental_stats
253253
#:for k1, t1 in INT_KINDS_TYPES
254254
#:for rank in RANKS
255255
#:set RName = rname("moment",rank, t1, k1, 'dp')
256-
module function ${RName}$(x, order, center, dim, mask) result(res)
256+
module function ${RName}$(x, order, dim, center, mask) result(res)
257257
${t1}$, intent(in) :: x${ranksuffix(rank)}$
258258
integer, intent(in) :: order
259259
integer, intent(in) :: dim
@@ -293,7 +293,7 @@ module stdlib_experimental_stats
293293
#:for k1, t1 in RC_KINDS_TYPES
294294
#:for rank in RANKS
295295
#:set RName = rname("moment_mask",rank, t1, k1)
296-
module function ${RName}$(x, order, center, dim, mask) result(res)
296+
module function ${RName}$(x, order, dim, center, mask) result(res)
297297
${t1}$, intent(in) :: x${ranksuffix(rank)}$
298298
integer, intent(in) :: order
299299
integer, intent(in) :: dim
@@ -307,7 +307,7 @@ module stdlib_experimental_stats
307307
#:for k1, t1 in INT_KINDS_TYPES
308308
#:for rank in RANKS
309309
#:set RName = rname("moment_mask",rank, t1, k1, 'dp')
310-
module function ${RName}$(x, order, center, dim, mask) result(res)
310+
module function ${RName}$(x, order, dim, center, mask) result(res)
311311
${t1}$, intent(in) :: x${ranksuffix(rank)}$
312312
integer, intent(in) :: order
313313
integer, intent(in) :: dim

src/stdlib_experimental_stats.md

Lines changed: 36 additions & 15 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` - central moment of array elements](#moment---central-moment-of-array-elements)
4+
* [`moment` - raw and central moments of array elements](#moment---raw-and-central-moments-of-array-elements)
55
* [`var` - variance of array elements](#var---variance-of-array-elements)
66

77
## `mean` - mean of array elements
@@ -19,7 +19,7 @@ Returns the mean of all the elements of `array`, or of the elements of `array` a
1919
### Arguments
2020

2121
`array`: Shall be an array of type `integer`, `real`, or `complex`.
22-
22+
[, center [,
2323
`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`.
2424

2525
`mask` (optional): Shall be of type `logical` and either by a scalar or an array of the same shape as `array`.
@@ -48,25 +48,34 @@ program demo_mean
4848
end program demo_mean
4949
```
5050

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

5353
### Description
5454

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`.
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`.
5656

57-
The _k_-th order central moment is defined as :
57+
If an array `center` is provided, the function returns the _k_-th order central moment (about the array `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`.
58+
59+
60+
The _k_-th order raw moment is defined as :
5861

5962
```
60-
moment(array) = 1/n sum_i (array(i) - mean(array))^k
63+
moment(array) = 1/n sum_i (array(i))^k
6164
```
6265

6366
where n is the number of elements.
6467

68+
The _k_-th order central moment is defined as :
69+
70+
```
71+
moment(array) = 1/n sum_i (array(i) - center)^k
72+
```
73+
6574
### Syntax
6675

67-
`result = moment(array, order [, mask])`
76+
`result = moment(array, order [, center [, mask]])`
6877

69-
`result = moment(array, order, dim [, mask])`
78+
`result = moment(array, order, ,dim [, center [, mask]])`
7079

7180
### Arguments
7281

@@ -76,29 +85,41 @@ where n is the number of elements.
7685

7786
`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`.
7887

88+
`center` (optional): Shall be a scalar or an array of the same type of `array`, if `array` is `real` or `complex, or a scalar or an array of type `real(dp)` if `array` is of type `integer`.
89+
7990
`mask` (optional): Shall be of type `logical` and either by a scalar or an array of the same shape as `array`.
8091

8192
### Return value
8293

8394
If `array` is of type `real` or `complex`, the result is of the same type as `array`.
8495
If `array` is of type `integer`, the result is of type `real(dp)`.
8596

86-
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.
97+
If `dim` is absent, a scalar with the _k_-th raw (or central if `center` is present) 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.
8798

88-
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`.
99+
If `mask` is specified, the result is the _k_-th raw (or central if `center` is present) moment of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
89100

90101
### Example
91102

92103
```fortran
93104
program demo_moment
94-
use stdlib_experimental_stats, only: moment
105+
use stdlib_experimental_stats, only: mean, moment
95106
implicit none
96107
real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ]
97-
print *, moment(x, 2) !returns 2.9167
98-
print *, moment( reshape(x, [ 2, 3 ] ), 2) !returns 2.9167
99-
print *, moment( reshape(x, [ 2, 3 ] ), 2, 1) !returns [0.25, 0.25, 0.25]
108+
print *, moment(x, 2, center = mean(x))
109+
!returns 2.9167
110+
111+
print *, moment( reshape(x, [ 2, 3 ] ), 2,&
112+
center = mean(reshape(x, [ 2, 3 ])))
113+
!returns 2.9167
114+
115+
print *, moment( reshape(x, [ 2, 3 ] ), 2, 1,&
116+
center = mean(reshape(x, [ 2, 3 ] ), 1))
117+
!returns [0.25, 0.25, 0.25]
118+
100119
print *, moment( reshape(x, [ 2, 3 ] ), 2, 1,&
101-
reshape(x, [ 2, 3 ] ) > 3.) !returns [NaN, 0., 0.25]
120+
center = mean(reshape(x, [2, 3]), 1, reshape(x, [ 2, 3 ] ) > 3.),&
121+
mask = (reshape(x, [ 2, 3 ] ) > 3.))
122+
!returns [NaN, 0., 0.25]
102123
end program demo_moment
103124
```
104125

src/stdlib_experimental_stats_moment.fypp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ contains
7373
#:for k1, t1 in RC_KINDS_TYPES
7474
#:for rank in RANKS
7575
#:set RName = rname("moment",rank, t1, k1)
76-
module function ${RName}$(x, order, center, dim, mask) result(res)
76+
module function ${RName}$(x, order, dim, center, mask) result(res)
7777
${t1}$, intent(in) :: x${ranksuffix(rank)}$
7878
integer, intent(in) :: order
7979
integer, intent(in) :: dim
@@ -119,7 +119,7 @@ contains
119119
#:for k1, t1 in INT_KINDS_TYPES
120120
#:for rank in RANKS
121121
#:set RName = rname("moment",rank, t1, k1, 'dp')
122-
module function ${RName}$(x, order, center, dim, mask) result(res)
122+
module function ${RName}$(x, order, dim, center, mask) result(res)
123123
${t1}$, intent(in) :: x${ranksuffix(rank)}$
124124
integer, intent(in) :: order
125125
integer, intent(in) :: dim
@@ -215,7 +215,7 @@ contains
215215
#:for k1, t1 in RC_KINDS_TYPES
216216
#:for rank in RANKS
217217
#:set RName = rname("moment_mask",rank, t1, k1)
218-
module function ${RName}$(x, order, center, dim, mask) result(res)
218+
module function ${RName}$(x, order, dim, center, mask) result(res)
219219
${t1}$, intent(in) :: x${ranksuffix(rank)}$
220220
integer, intent(in) :: order
221221
integer, intent(in) :: dim
@@ -268,7 +268,7 @@ contains
268268
#:for k1, t1 in INT_KINDS_TYPES
269269
#:for rank in RANKS
270270
#:set RName = rname("moment_mask",rank, t1, k1, 'dp')
271-
module function ${RName}$(x, order, center, dim, mask) result(res)
271+
module function ${RName}$(x, order, dim, center, mask) result(res)
272272
${t1}$, intent(in) :: x${ranksuffix(rank)}$
273273
integer, intent(in) :: order
274274
integer, intent(in) :: dim

0 commit comments

Comments
 (0)