Skip to content

Commit fdf4e54

Browse files
committed
covariance: update spec
1 parent c3c277f commit fdf4e54

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/stdlib_experimental_stats.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,66 @@
11
# Descriptive statistics
22

3+
* [`cov` - covariance of array elements](#cov---covariance-of-array-elements)
34
* [`mean` - mean of array elements](#mean---mean-of-array-elements)
45
* [`moment` - central moments of array elements](#moment---central-moments-of-array-elements)
56
* [`var` - variance of array elements](#var---variance-of-array-elements)
67

8+
9+
## `cov` - covariance of array elements
10+
11+
### Description
12+
13+
Returns the covariance of the elements of `array` along dimension `dim` if the corresponding element in `mask` is `true` (if `mask` is provided).
14+
15+
Per default, the covariance is defined as:
16+
17+
```
18+
cov(array) = 1/(n-1) sum_i (array(i) - mean(array) * (array(i) - mean(array)) )
19+
```
20+
21+
where n is the number of elements.
22+
23+
The scaling can be changed with the logical argument `corrected`. If `corrected` is `.false.`, then the sum is scaled with `n`, otherwise with `n-1`.
24+
25+
26+
### Syntax
27+
28+
`result = cov(array, dim [, mask [, corrected]])`
29+
30+
### Arguments
31+
32+
`array`: Shall be a 1-rank or a 2-rank array of type `integer`, `real`, or `complex`.
33+
34+
`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`.
35+
36+
`mask` (optional): Shall be of type `logical` and either by a scalar or an array of the same shape as `array`.
37+
38+
`corrected` (optional): Shall be a scalar of type `logical`. If `corrected` is `.true.` (default value), the sum is scaled with `n-1`. If `corrected` is `.false.`, then the sum is scaled with `n`.
39+
40+
### Return value
41+
42+
If `array` is of rank 1 and of type `real` or `complex`, the result is of the same type `real` corresponding to the type of `array`.
43+
If `array` is of rank 2 and of type `real` or `complex`, the result is of the same type as `array`.
44+
If `array` is of type `integer`, the result is of type `real(dp)`.
45+
46+
If `array` is of rank 1, a scalar with the covariance (that is the variance) of all elements in `array` is returned.
47+
If `array` is of rank 2, a 2-rank array is returned.
48+
49+
If `mask` is specified, the result is the covariance of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
50+
### Example
51+
52+
```fortran
53+
program demo_cov
54+
use stdlib_experimental_stats, only: cov
55+
implicit none
56+
real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ]
57+
real :: y(1:2, 1:3) = reshape([ 1., 2., 3., 4., 5., 6. ], [ 2, 3])
58+
print *, cov(x, 1) !returns 3.5
59+
print *, cov(x, 1, corrected = .false.) !returns 2.9167
60+
print *, cov(y, 1) !returns a square matrix of size 3 with all elements equal to 0.5
61+
end program demo_cov
62+
```
63+
764
## `mean` - mean of array elements
865

966
### Description

0 commit comments

Comments
 (0)