Skip to content

Commit 20a76b8

Browse files
committed
moment_dev: addition of spec
1 parent 7c33e00 commit 20a76b8

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

src/stdlib_experimental_stats.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Implemented
44

55
* `mean`
6+
* `moment`
67
* `var`
78

89
## `mean` - mean of array elements
@@ -28,7 +29,7 @@ Returns the mean of all the elements of `array`, or of the elements of `array` a
2829
### Return value
2930

3031
If `array` is of type `real` or `complex`, the result is of the same type as `array`.
31-
If `array` is of type `integer`, the result is of type `double precision`.
32+
If `array` is of type `integer`, the result is of type `real(dp)`.
3233

3334
If `dim` is absent, a scalar with the mean 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.
3435

@@ -49,6 +50,60 @@ program demo_mean
4950
end program demo_mean
5051
```
5152

53+
## `moment` - central moment of array elements
54+
55+
### Description
56+
57+
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+
The _k_-th order central moment is defined as :
60+
61+
```
62+
moment(array) = 1/n sum_i (array(i) - mean(array))^k
63+
```
64+
65+
where n is the number of elements.
66+
67+
### Syntax
68+
69+
`result = moment(array, order [, mask])`
70+
71+
`result = moment(array, order, dim [, mask])`
72+
73+
### Arguments
74+
75+
`array`: Shall be an array of type `integer`, `real`, or `complex`.
76+
77+
`order`: Shall be an scalar of type `integer`.
78+
79+
`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`.
80+
81+
`mask` (optional): Shall be of type `logical` and either by a scalar or an array of the same shape as `array`.
82+
83+
### Return value
84+
85+
If `array` is of type `real` or `complex`, the result is of the same type as `array`.
86+
If `array` is of type `integer`, the result is of type `real(dp)`.
87+
88+
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.
89+
90+
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`.
91+
92+
### Example
93+
94+
```fortran
95+
program demo_moment
96+
use stdlib_experimental_stats, only: moment
97+
implicit none
98+
real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ]
99+
print *, moment(x, 2) !returns 2.9167
100+
print *, moment( reshape(x, [ 2, 3 ] ), 2) !returns 2.9167
101+
print *, moment( reshape(x, [ 2, 3 ] ), 2, 1) !returns [0.25, 0.25, 0.25]
102+
print *, moment( reshape(x, [ 2, 3 ] ), 2, 1,&
103+
reshape(x, [ 2, 3 ] ) > 3.) !returns [NaN, 0.0, 0.25]
104+
end program demo_moment
105+
```
106+
52107
## `var` - variance of array elements
53108

54109
### Description
@@ -58,7 +113,7 @@ Returns the variance of all the elements of `array`, or of the elements of `arra
58113
Per default, the variance is defined as the best unbiased estimator and is computed as:
59114

60115
```
61-
var(x) = 1/(n-1) sum_i (array(i) - mean(array))^2
116+
var(array) = 1/(n-1) sum_i (array(i) - mean(array))^2
62117
```
63118

64119
where n is the number of elements.
@@ -108,7 +163,7 @@ program demo_var
108163
reshape(x, [ 2, 3 ] ) > 3.) !returns [NaN, NaN, 0.5]
109164
print *, var( reshape(x, [ 2, 3 ] ), 1,&
110165
reshape(x, [ 2, 3 ] ) > 3.,&
111-
corrected=.false.) !returns [NaN, 0., 0.5]
166+
corrected=.false.) !returns [NaN, 0., 0.25]
112167
end program demo_var
113168
```
114169

0 commit comments

Comments
 (0)