Skip to content
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

Statistical functions #1990

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Added support for fetching tables from Google Spreadsheets.
([#1976](https://github.com/enso-org/enso/pull/1976)).
- Added support for certain statistical functions of table columns
([#1990](https://github.com/enso-org/enso/pull/1990)).

# Enso 0.2.28 (2021-09-02)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,41 @@ type Column
(Vector.Vector v).reduce (+) / v.length
this.java_column.aggregate 'mean' vec_mean True

## Computes the variance of the sample represented by this column.

Arguments:
- degrees_of_freedom_correction: a correction to account for the
missing degrees of freedom in the sample. The default value of `1`
computes a sample variance. Setting it to `0` will compute population
variance instead.
variance degrees_of_freedom_correction=1 =
mean = this.mean
shifted = this - mean
sq = shifted * shifted
sq.sum / (this.length - degrees_of_freedom_correction)

## Computes the standard deviation of the sample represented by this column.

Arguments:
- degrees_of_freedom_correction: a correction to account for the
missing degrees of freedom in the sample. The default value of `1`
computes a sample standard deviation. Setting it to `0` will compute
population standard deviation instead.
standard_deviation degrees_of_freedom_correction=1 =
this.variance degrees_of_freedom_correction . sqrt

## Computes the coefficient of determination of a given prediction column.

Arguments:
- predictions: the column predicting the values of this column.
r_squared predictions =
prediction_diff = this - predictions
ss_res = prediction_diff*prediction_diff . sum
ss_tot_lin = this - this.mean
ss_tot = ss_tot_lin*ss_tot_lin . sum
1 - ss_res / ss_tot


## UNSTABLE

Sorts the column according to the specified rules.
Expand Down
20 changes: 20 additions & 0 deletions test/Table_Tests/src/Column_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,23 @@ spec = Test.group "Columns" <|
col = Examples.decimal_column.set_index Examples.integer_column
col.index.to_vector . should_equal Examples.integer_column.to_vector

Test.specify "should allow computing variance and standard deviation" <|
const = Column.from_vector 'const' [1, 1, 1, 1, 1]
const.variance . should_equal 0
const.standard_deviation.should_equal 0

rand = Column.from_vector 'random' [10.0, 4.2, 6.8, 6.2, 7.2]
rand.variance . should_equal 4.372
rand.variance degrees_of_freedom_correction=0 . should_equal 3.4976
rand.standard_deviation . should_equal 2.090932806 epsilon=(10 ^ -6)
rand.standard_deviation degrees_of_freedom_correction=0 . should_equal 1.870187156 epsilon=(10 ^ -6)

Test.specify "should allow computing the R² score of a prediction" <|
sample = Column.from_vector 'sample' [1,2,3,4,5]
mean_pred = Column.from_vector 'mean' [3,3,3,3,3]
perfect_pred = Column.from_vector 'perfect' [1,2,3,4,5]
bad_pred = Column.from_vector 'bad' [5,4,3,2,1]

sample.r_squared mean_pred . should_equal 0
sample.r_squared perfect_pred . should_equal 1
sample.r_squared bad_pred . should_equal -3