-
Notifications
You must be signed in to change notification settings - Fork 188
Addition of variance function in stdlib_experimental_stats #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
e966e7b
044abc5
d77b6e9
baabfc8
9b19154
da90a89
2a0182a
01e897c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
## Implemented | ||
|
||
* `mean` | ||
* `var` | ||
|
||
## `mean` - mean of array elements | ||
|
||
|
@@ -47,3 +48,55 @@ program demo_mean | |
reshape(x, [ 2, 3 ] ) > 3.) !returns [ NaN, 4.0, 5.5 ] | ||
end program demo_mean | ||
``` | ||
|
||
## `var` - variance of array elements | ||
|
||
### 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`. | ||
|
||
The variance is defined as the best unbiased estimator and is computed as: | ||
|
||
``` | ||
var(x) = 1/(n-1) sum_i (array(i) - mean(array))^2 | ||
``` | ||
|
||
### Syntax | ||
|
||
`result = var(array [, mask])` | ||
|
||
`result = var(array, dim [, mask])` | ||
|
||
### Arguments | ||
|
||
`array`: Shall be an array of type `integer`, `real`, or `complex`. | ||
|
||
`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`. | ||
|
||
`mask` (optional): Shall be of type `logical` and either by 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 `double precision`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will raise this issue elsewhere, but I do not agree with this API for the return type when the input is integer data. I only bring it up here because it is not quite correct to say that the return type is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nshaffer Thank you for your review. I used Would it be better to write "..... the result is of type |
||
|
||
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 `ar | ||
ray` 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`. | ||
|
||
### Example | ||
|
||
```fortran | ||
program demo_var | ||
use stdlib_experimental_stats, only: var | ||
implicit none | ||
real :: x(1:6) = [ 1., 2., 3., 4., 5., 6. ] | ||
print *, var(x) !returns 3.5 | ||
print *, var( reshape(x, [ 2, 3 ] )) !returns 3.5 | ||
print *, var( reshape(x, [ 2, 3 ] ), 1) !returns [0.5, 0.5, 0.5] | ||
print *, var( reshape(x, [ 2, 3 ] ), 1,& | ||
reshape(x, [ 2, 3 ] ) > 3.) !returns [0., NaN, 0.5] | ||
end program demo_var | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably rename this macro to have a more descriptive name. If it is only used to select subarrays by reducing the dimension, we could have:
and use it as
It could also be used, if we need to reduce more than one rank, e.g.
Also the description should be clarified a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented as suggested. The proposed macro is more general and better fit to its aim.
Could you have another review, please?