Skip to content

Commit

Permalink
FIgure out stddev
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Apr 7, 2022
1 parent c303784 commit 8cc25b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
16 changes: 10 additions & 6 deletions distribution/lib/Standard/Test/0.0.0-dev/src/Main.enso
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,16 @@ Error.should_equal _ frames_to_skip=0 = here.fail_match_on_unexpected_error this
example_should_equal =
1.00000001 . should_equal 1.00000002 epsilon=0.0001
Decimal.should_equal : Decimal -> Decimal -> Integer -> Assertion
Decimal.should_equal that (epsilon = 0) (frames_to_skip=0) = case this.equals that epsilon of
True -> Success
False ->
loc = Meta.get_source_location 2+frames_to_skip
msg = this.to_text + " did not equal " + that.to_text + " (at " + loc + ")."
Panic.throw (Failure msg)
Decimal.should_equal that (epsilon = 0) (frames_to_skip=0) =
matches = case that of
Number -> this.equals that epsilon
_ -> False
case matches of
True -> Success
False ->
loc = Meta.get_source_location 2+frames_to_skip
msg = this.to_text + " did not equal " + that.to_text + " (at " + loc + ")."
Panic.throw (Failure msg)

## Asserts that the given `Boolean` is `True`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.enso.table.aggregations;

import java.util.List;
import org.enso.table.data.column.storage.Storage;
import org.enso.table.data.table.Column;
import org.enso.table.data.table.problems.InvalidAggregation;

import java.util.List;

/***
* Aggregate Column computing the standard deviation of a group.
*/
Expand All @@ -25,7 +24,7 @@ public Calculation(double value) {
private final Storage storage;
private final boolean population;

public StandardDeviation(String name, Column column,boolean population) {
public StandardDeviation(String name, Column column, boolean population) {
super(name, Storage.Type.DOUBLE);
this.storage = column.getStorage();
this.population = population;
Expand All @@ -34,12 +33,13 @@ public StandardDeviation(String name, Column column,boolean population) {
@Override
public Object aggregate(List<Integer> indexes) {
Calculation current = null;
for (int row: indexes) {
for (int row : indexes) {
Object value = storage.getItemBoxed(row);
if (value != null) {
Double dValue = CastToDouble(value);
if (dValue == null) {
this.addProblem(new InvalidAggregation(this.getName(), row, "Cannot convert to a number."));
this.addProblem(
new InvalidAggregation(this.getName(), row, "Cannot convert to a number."));
return null;
}

Expand All @@ -48,12 +48,14 @@ public Object aggregate(List<Integer> indexes) {
} else {
current.count++;
current.total += dValue;
current.total_sqr += dValue*dValue;
current.total_sqr += dValue * dValue;
}
}
}
return current == null ? null :
(population ? 1 : Math.sqrt(current.count / (current.count - 1.0))) *
Math.sqrt(current.total_sqr / current.count - Math.pow(current.total / current.count, 2));

if (current == null) return null;
if (!population && current.count <= 1) return null;
return (population ? 1 : Math.sqrt(current.count / (current.count - 1.0)))
* Math.sqrt(current.total_sqr / current.count - Math.pow(current.total / current.count, 2));
}
}
3 changes: 1 addition & 2 deletions test/Table_Tests/src/Aggregate_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ aggregate_spec prefix table empty_table table_builder materialize is_database te
m1 = materialize r1
m1.columns.length . should_equal 2
m1.columns.first.at 0 . should_equal Nothing
m1.columns.first.at 0 . should_equal Nothing
m1.columns.second.at 0 . should_equal 0

Test.group prefix+"Table.aggregate should correctly select result types" pending=pending <|
Test.specify " widening to decimals on Average" <|
Expand Down Expand Up @@ -930,7 +930,6 @@ aggregate_spec prefix table empty_table table_builder materialize is_database te
table = table_builder [["X", [1, 2, 3, 4, Nothing]]]
r1 = table.aggregate [Standard_Deviation "X" (population=True), Standard_Deviation "X" (population=False)]
r1.row_count.should_equal 1
r1.print
m1 = materialize r1
m1.columns.length . should_equal 2
m1.columns.first.at 0 . should_equal 1.1180339887499 epsilon=0.000001
Expand Down

0 comments on commit 8cc25b9

Please sign in to comment.