1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
---
title: stats
---
# Descriptive statistics
[TOC]
## `corr` - Pearson correlation of array elements
### Status
Experimental
### Description
Returns the Pearson correlation of the elements of `array` along dimension `dim` if the corresponding element in `mask` is `true`.
The Pearson correlation between two rows (or columns), say `x` and `y`, of `array` is defined as:
```
corr(x, y) = cov(x, y) / sqrt( var(x) * var(y))
```
### Syntax
`result = ` [[stdlib_stats(module):corr(interface)]] `(array, dim [, mask])`
### Class
Generic subroutine
### Arguments
`array`: Shall be a rank-1 or a rank-2 array of type `integer`, `real`, or `complex`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
`mask` (optional): Shall be of type `logical` and either a scalar or an array of the same shape as `array`. It is an `intent(in)` argument.
### Return value
If `array` is of rank 1 and of type `real` or `complex`, the result is of type `real` and has the same kind as `array`.
If `array` is of rank 2 and of type `real` or `complex`, the result is of the same type and kind as `array`.
If `array` is of type `integer`, the result is of type `real(dp)`.
If `array` is of rank 1 and of size larger than 1, a scalar equal to 1 is returned. Otherwise, IEEE `NaN` is returned.
If `array` is of rank 2, a rank-2 array with the corresponding correlations is returned.
If `mask` is specified, the result is the Pearson correlation of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
### Example
```fortran
{!example/stats/example_corr.f90!}
```
## `cov` - covariance of array elements
### Status
Experimental
### Description
Returns the covariance of the elements of `array` along dimension `dim` if the corresponding element in `mask` is `true`.
Per default, the covariance is defined as:
```
cov(array) = 1/(n-1) sum_i (array(i) - mean(array) * (array(i) - mean(array)))
```
where `n` is the number of elements.
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`.
### Syntax
`result = ` [[stdlib_stats(module):cov(interface)]] `(array, dim [, mask [, corrected]])`
### Class
Generic subroutine
### Arguments
`array`: Shall be a rank-1 or a rank-2 array of type `integer`, `real`, or `complex`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
`mask` (optional): Shall be of type `logical` and either a scalar or an array of the same shape as `array`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
### Return value
If `array` is of rank 1 and of type `real` or `complex`, the result is of type `real` corresponding to the type of `array`.
If `array` is of rank 2 and of type `real` or `complex`, the result is of the same type as `array`.
If `array` is of type `integer`, the result is of type `real(dp)`.
If `array` is of rank 1, a scalar with the covariance (that is the variance) of all elements in `array` is returned.
If `array` is of rank 2, a rank-2 array is returned.
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`.
### Example
```fortran
{!example/stats/example_cov.f90!}
```
## `mean` - mean of array elements
### Status
Experimental
### Description
Returns the mean 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`.
### Syntax
`result = ` [[stdlib_stats(module):mean(interface)]] `(array [, mask])`
`result = ` [[stdlib_stats(module):mean(interface)]] `(array, dim [, mask])`
### Class
Generic subroutine
### Arguments
`array`: Shall be an array of type `integer`, `real`, or `complex`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
`mask` (optional): Shall be of type `logical` and either a scalar or an array of the same shape as `array`. It is an `intent(in)` argument.
### Return value
If `array` is of type `real` or `complex`, the result is of the same type as `array`.
If `array` is of type `integer`, the result is of type `real(dp)`.
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.
If `mask` is specified, the result is the mean of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
### Example
```fortran
{!example/stats/example_mean.f90!}
```
## `median` - median of array elements
### Status
Experimental
### Description
Returns the median 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`.
The median of the elements of `array` is defined as the "middle"
element, after that the elements are sorted in an increasing order, e.g. `array_sorted =
sort(array)`. If `n = size(array)` is an even number, the median is:
```
median(array) = array_sorted( floor( (n + 1) / 2.))
```
and if `n` is an odd number, the median is:
```
median(array) = mean( array_sorted( floor( (n + 1) / 2.):floor( (n + 1) / 2.) + 1 ) )
```
The current implementation relies on a selection algorithm applied on a copy of
the whole array, using the subroutine [[stdlib_selection(module):select(interface)]]
provided by the [[stdlib_selection(module)]] module.
### Syntax
`result = ` [[stdlib_stats(module):median(interface)]] `(array [, mask])`
`result = ` [[stdlib_stats(module):median(interface)]] `(array, dim [, mask])`
### Class
Generic subroutine
### Arguments
`array`: Shall be an array of type `integer` or `real`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
`mask` (optional): Shall be of type `logical` and either a scalar or an array of the same shape as `array`. It is an `intent(in)` argument.
### Return value
If `array` is of type `real`, the result is of type `real` with the same kind as `array`.
If `array` is of type `real` and contains IEEE `NaN`, the result is IEEE `NaN`.
If `array` is of type `integer`, the result is of type `real(dp)`.
If `dim` is absent, a scalar with the median 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.
If `mask` is specified, the result is the median of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
### Example
```fortran
{!example/stats/example_median.f90!}
```
## `moment` - central moments of array elements
### Status
Experimental
### Description
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`.
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`.
The _k_-th order central moment is defined as :
```
moment(array) = 1/n sum_i (array(i) - mean(array))^k
```
where `n` is the number of elements.
The _k_-th order moment about `center` is defined as :
```
moment(array) = 1/n sum_i (array(i) - center)^k
```
### Syntax
`result = ` [[stdlib_stats(module):moment(interface)]] `(array, order [, center [, mask]])`
`result = ` [[stdlib_stats(module):moment(interface)]] `(array, order, dim [, center [, mask]])`
### Class
Generic subroutine
### Arguments
`array`: Shall be an array of type `integer`, `real`, or `complex`.
`order`: Shall be an scalar of type `integer`.
`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`.
`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`.
`mask` (optional): Shall be of type `logical` and either a scalar or an array of the same shape as `array`.
### Return value
If `array` is of type `real` or `complex`, the result is of the same type as `array`.
If `array` is of type `integer`, the result is of type `real(dp)`.
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.
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`.
### Example
```fortran
{!example/stats/example_moment.f90!}
```
## `var` - variance of array elements
### Status
Experimental
### Description
Returns the variance 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`.
Per default, the variance is defined as the best unbiased estimator and is computed as:
```
var(array) = 1/(n-1) sum_i (array(i) - mean(array))^2
```
where `n` is the number of elements.
The use of the term `n-1` for scaling is called Bessel 's correction. 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`.
### Syntax
`result = ` [[stdlib_stats(module):var(interface)]] `(array [, mask [, corrected]])`
`result = ` [[stdlib_stats(module):var(interface)]] `(array, dim [, mask [, corrected]])`
### Class
Generic subroutine
### Arguments
`array`: Shall be an array of type `integer`, `real`, or `complex`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
`mask` (optional): Shall be of type `logical` and either a scalar or an array of the same shape as `array`. It is an `intent(in)` argument.
`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`. It is an `intent(in)` argument.
### Return value
If `array` is of type `real` or `complex`, the result is of type `real` corresponding to the type of `array`.
If `array` is of type `integer`, the result is of type `real(dp)`.
If `dim` is absent, a scalar with the variance 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.
If `mask` is specified, the result is the variance of all elements of `array` corresponding to `true` elements of `mask`. If every element of `mask` is `false`, the result is IEEE `NaN`.
If the variance is computed with only one single element, then the result is IEEE `NaN` if `corrected` is `.true.` and is `0.` if `corrected` is `.false.`.
### Example
```fortran
{!example/stats/example_var.f90!}
```
|