Skip to content

Commit 406336b

Browse files
committed
Squashed commit of the following:
commit 155ddc2 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 19:12:07 2020 +0100 rawmoment: update spec commit a365ea0 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 18:56:46 2020 +0100 rawmoment: small change in spec commit d955c0c Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 18:03:49 2020 +0100 rawmoment: correction commit 0480bc1 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 17:57:24 2020 +0100 rawmoment: small correction commit fcc6a43 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 17:52:30 2020 +0100 rawmoment: cleaning commit c63ea07 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 15:48:40 2020 +0100 rawmoment: progress in test files commit ef6deec Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 15:42:46 2020 +0100 rawmoment: progress commit a31654c Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 14:03:50 2020 +0100 rawmoment: start tests raw moment commit 78d7fb9 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 13:37:02 2020 +0100 rawmoment: addition of test commit 0e93187 Author: Vandenplas, Jeremie <jeremie.vandenplas@gmail.com> Date: Sun Mar 22 11:55:31 2020 +0100 rawmoment: start to add center in test
1 parent 5d1e091 commit 406336b

File tree

6 files changed

+1032
-490
lines changed

6 files changed

+1032
-490
lines changed

src/stdlib_experimental_stats.fypp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ module stdlib_experimental_stats
213213
#:for k1, t1 in RC_KINDS_TYPES
214214
#:for rank in RANKS
215215
#:set RName = rname("moment_all",rank, t1, k1)
216-
module function ${RName}$(x, order, mask) result(res)
216+
module function ${RName}$(x, order, center, mask) result(res)
217217
${t1}$, intent(in) :: x${ranksuffix(rank)}$
218218
integer, intent(in) :: order
219+
${t1}$, intent(in), optional :: center
219220
logical, intent(in), optional :: mask
220221
${t1}$ :: res
221222
end function ${RName}$
@@ -225,9 +226,10 @@ module stdlib_experimental_stats
225226
#:for k1, t1 in INT_KINDS_TYPES
226227
#:for rank in RANKS
227228
#:set RName = rname("moment_all",rank, t1, k1, 'dp')
228-
module function ${RName}$(x, order, mask) result(res)
229+
module function ${RName}$(x, order, center, mask) result(res)
229230
${t1}$, intent(in) :: x${ranksuffix(rank)}$
230231
integer, intent(in) :: order
232+
real(dp), intent(in), optional :: center
231233
logical, intent(in), optional :: mask
232234
real(dp) :: res
233235
end function ${RName}$
@@ -237,10 +239,11 @@ module stdlib_experimental_stats
237239
#:for k1, t1 in RC_KINDS_TYPES
238240
#:for rank in RANKS
239241
#:set RName = rname("moment",rank, t1, k1)
240-
module function ${RName}$(x, order, dim, mask) result(res)
242+
module function ${RName}$(x, order, dim, center, mask) result(res)
241243
${t1}$, intent(in) :: x${ranksuffix(rank)}$
242244
integer, intent(in) :: order
243245
integer, intent(in) :: dim
246+
${t1}$, intent(in), optional :: center${reduced_shape('x', rank, 'dim')}$
244247
logical, intent(in), optional :: mask
245248
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$
246249
end function ${RName}$
@@ -250,10 +253,11 @@ module stdlib_experimental_stats
250253
#:for k1, t1 in INT_KINDS_TYPES
251254
#:for rank in RANKS
252255
#:set RName = rname("moment",rank, t1, k1, 'dp')
253-
module function ${RName}$(x, order, dim, mask) result(res)
256+
module function ${RName}$(x, order, dim, center, mask) result(res)
254257
${t1}$, intent(in) :: x${ranksuffix(rank)}$
255258
integer, intent(in) :: order
256259
integer, intent(in) :: dim
260+
real(dp),intent(in), optional :: center${reduced_shape('x', rank, 'dim')}$
257261
logical, intent(in), optional :: mask
258262
real(dp) :: res${reduced_shape('x', rank, 'dim')}$
259263
end function ${RName}$
@@ -263,9 +267,10 @@ module stdlib_experimental_stats
263267
#:for k1, t1 in RC_KINDS_TYPES
264268
#:for rank in RANKS
265269
#:set RName = rname("moment_mask_all",rank, t1, k1)
266-
module function ${RName}$(x, order, mask) result(res)
270+
module function ${RName}$(x, order, center, mask) result(res)
267271
${t1}$, intent(in) :: x${ranksuffix(rank)}$
268272
integer, intent(in) :: order
273+
${t1}$, intent(in), optional :: center
269274
logical, intent(in) :: mask${ranksuffix(rank)}$
270275
${t1}$ :: res
271276
end function ${RName}$
@@ -275,9 +280,10 @@ module stdlib_experimental_stats
275280
#:for k1, t1 in INT_KINDS_TYPES
276281
#:for rank in RANKS
277282
#:set RName = rname("moment_mask_all",rank, t1, k1, 'dp')
278-
module function ${RName}$(x, order, mask) result(res)
283+
module function ${RName}$(x, order, center, mask) result(res)
279284
${t1}$, intent(in) :: x${ranksuffix(rank)}$
280285
integer, intent(in) :: order
286+
real(dp),intent(in), optional :: center
281287
logical, intent(in) :: mask${ranksuffix(rank)}$
282288
real(dp) :: res
283289
end function ${RName}$
@@ -287,10 +293,11 @@ module stdlib_experimental_stats
287293
#:for k1, t1 in RC_KINDS_TYPES
288294
#:for rank in RANKS
289295
#:set RName = rname("moment_mask",rank, t1, k1)
290-
module function ${RName}$(x, order, dim, mask) result(res)
296+
module function ${RName}$(x, order, dim, center, mask) result(res)
291297
${t1}$, intent(in) :: x${ranksuffix(rank)}$
292298
integer, intent(in) :: order
293299
integer, intent(in) :: dim
300+
${t1}$, intent(in), optional :: center${reduced_shape('x', rank, 'dim')}$
294301
logical, intent(in) :: mask${ranksuffix(rank)}$
295302
${t1}$ :: res${reduced_shape('x', rank, 'dim')}$
296303
end function ${RName}$
@@ -300,10 +307,11 @@ module stdlib_experimental_stats
300307
#:for k1, t1 in INT_KINDS_TYPES
301308
#:for rank in RANKS
302309
#:set RName = rname("moment_mask",rank, t1, k1, 'dp')
303-
module function ${RName}$(x, order, dim, mask) result(res)
310+
module function ${RName}$(x, order, dim, center, mask) result(res)
304311
${t1}$, intent(in) :: x${ranksuffix(rank)}$
305312
integer, intent(in) :: order
306313
integer, intent(in) :: dim
314+
real(dp), intent(in), optional :: center${reduced_shape('x', rank, 'dim')}$
307315
logical, intent(in) :: mask${ranksuffix(rank)}$
308316
real(dp) :: res${reduced_shape('x', rank, 'dim')}$
309317
end function ${RName}$

src/stdlib_experimental_stats.md

Lines changed: 30 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
@@ -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 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,35 @@ 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 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`.
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 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.
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 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`.
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]
100-
print *, moment( reshape(x, [ 2, 3 ] ), 2, 1,&
101-
reshape(x, [ 2, 3 ] ) > 3.) !returns [NaN, 0., 0.25]
108+
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]
102117
end program demo_moment
103118
```
104119

0 commit comments

Comments
 (0)