Ecto.OLAP v0.2.1 Ecto.OLAP.Statistics View Source

Set of macros providing additional aggregates for statistics counting.

Example data

All examples assumes we have table stats_agg with content:

yeardivorce_ratemarg_cons
20005.08.2
20014.77.0
20024.66.5
20034.45.3
20044.35.2
20054.14.0
20064.24.6
20074.24.5
20084.24.2
20094.23.7

Example data thanks to Tyler Vigen’s Spurious Correlations

Link to this section Summary

Functions

Compute correlation coefficient for given y and x expressions

Average of the independent variable x

Average of the dependent variable y

Count rows where both expressions are nonnull

y-intercept of the least-squares-fit linear equation determined by the (x, y) pairs

Square of the correlation coefficient

Slope of the least-squares-fit linear equation determined by the (x, y) pairs

“Sum of squares” of independent variable

“Sum of products” of independent times dependent variable

“Sum of squares” of dependent variable

Link to this section Functions

Compute correlation coefficient for given y and x expressions.

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: corr(e.divorce_rate, e.marg_cons)
0.9806576205544681
Link to this macro regr_avgx(y, x) View Source (macro)

Average of the independent variable x.

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_avgx(e.divorce_rate, e.marg_cons)
5.320000000000001
Link to this macro regr_avgy(y, x) View Source (macro)

Average of the dependent variable y.

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_avgy(e.divorce_rate, e.marg_cons)
4.390000000000001
Link to this macro regr_count(y, x) View Source (macro)

Count rows where both expressions are nonnull.

Link to this macro regr_intercept(y, x) View Source (macro)

y-intercept of the least-squares-fit linear equation determined by the (x, y) pairs.

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_intercept(e.divorce_rate, e.marg_cons)
3.363198179561455

Square of the correlation coefficient.

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_r2(e.divorce_rate, e.marg_cons)
0.9616893687515513
Link to this macro regr_slope(y, x) View Source (macro)

Slope of the least-squares-fit linear equation determined by the (x, y) pairs.

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_slope(e.divorce_rate, e.marg_cons)
0.1930078609846896
Link to this macro regr_sxx(y, x) View Source (macro)

“Sum of squares” of independent variable.

sum(x^2) - sum(x)^2
-------------------
         N

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_sxx(e.divorce_rate, e.marg_cons)
19.33599999999983
Link to this macro regr_sxy(y, x) View Source (macro)

“Sum of products” of independent times dependent variable.

sum(x * y) - sum(x) * sum(y)
----------------------------
             N

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_sxy(e.divorce_rate, e.marg_cons)
3.7319999999999256
Link to this macro regr_syy(y, x) View Source (macro)

“Sum of squares” of dependent variable.

sum(y^2) - sum(y)^2
-------------------
         N

Example

iex> import Ecto.Query
iex> import Ecto.OLAP.Statistics
iex>
iex> alias Ecto.Integration.TestRepo
iex>
iex> TestRepo.one from e in "stats_agg",
...>   select: regr_syy(e.divorce_rate, e.marg_cons)
0.7489999999999327