Skip to content

Commit

Permalink
Merge pull request #20 from cicirello/feature-stdev
Browse files Browse the repository at this point in the history
Added methods for sample standard deviation
  • Loading branch information
cicirello authored Feb 11, 2022
2 parents 96172ed + 7c99df6 commit 751fdab
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - 2021-12-22
## [Unreleased] - 2022-02-11

### Added

Expand All @@ -17,11 +17,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### CI/CD
* Added automated commenting of test coverage percentages to CI/CD workflow.

### Other


## [1.2.0] - 2022-02-11

### Added
* Statistics.stdev(int[]) and Statistics.stdev(double[]) methods for sample standard deviation.

### CI/CD
* Added automated commenting of test coverage percentages to CI/CD workflow.


## [1.1.0] - 2021-9-22

### Added
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/cicirello/math/stats/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Statistics() {}
public static double mean(int[] data) {
int sum = 0;
for (int e : data) sum = sum + e;
return 1.0 * sum / data.length;
return ((double)sum) / data.length;
}

/**
Expand Down Expand Up @@ -126,6 +126,24 @@ public static double varianceSample(double[] data) {
return (sumSquares - sum*sum/data.length)/(data.length-1.0);
}

/**
* Computes the sample standard deviation.
* @param data The dataset.
* @return the sample standard deviation.
*/
public static double stdev(int[] data) {
return Math.sqrt(varianceSample(data));
}

/**
* Computes the sample standard deviation.
* @param data The dataset.
* @return the sample standard deviation.
*/
public static double stdev(double[] data) {
return Math.sqrt(varianceSample(data));
}

/**
* Computes covariance for a pair of random variables.
* @param X Array of samples of first variable.
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/cicirello/math/stats/StatisticsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,44 @@ public void testSampleVarianceOfDoubles() {
}
}

@Test
public void testStdevOfInts() {
for (int n = 1; n <= 10; n++) {
int[] data = new int[n];
for (int i = 0; i < n; i++) {
data[i] = 3;
}
assertEquals("All elements the same", 0.0, Statistics.stdev(data), EPSILON);
}
// Note: This assumes that the variance method passes its tests.
for (int n = 2; n <= 10; n++) {
int[] data = new int[n];
for (int i = 0; i < n; i++) {
data[i] = i+1;
}
assertEquals("ints from 1 to n", Math.sqrt(Statistics.variance(data)*n/(n-1)), Statistics.stdev(data), EPSILON);
}
}

@Test
public void testStdevOfDoubles() {
for (int n = 1; n <= 10; n++) {
double[] data = new double[n];
for (int i = 0; i < n; i++) {
data[i] = 3;
}
assertEquals("All elements the same", 0.0, Statistics.stdev(data), EPSILON);
}
// Note: This assumes that the variance method passes its tests.
for (int n = 2; n <= 10; n++) {
double[] data = new double[n];
for (int i = 0; i < n; i++) {
data[i] = i+1;
}
assertEquals("ints from 1 to n", Math.sqrt(Statistics.variance(data)*n/(n-1)), Statistics.stdev(data), EPSILON);
}
}

@Test
public void testCovarianceOfInts() {
for (int n = 1; n <= 10; n++) {
Expand Down

0 comments on commit 751fdab

Please sign in to comment.