Skip to content

Commit

Permalink
Miscellaneous refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
safwank committed Aug 28, 2016
1 parent e06cfd7 commit c80101c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
56 changes: 36 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

# Numerix

A collection of (potentially) useful mathematical functions. At the moment it has a number of distance and correlation functions. The plan is to implement other special functions, statistics, probability distributions, maybe even machine learning algorithms.
A collection of useful mathematical in Elixir, with a slant towards statistics, linear algebra and machine learning.

## Installation

Add `numerix` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:numerix, "~> 0.0.6"}]
[{:numerix, "~> 0.1.0"}]
end
```

Expand All @@ -32,6 +32,26 @@ Check out the [API reference](https://hexdocs.pm/numerix/api-reference.html) for

## Features

### Statistics

* Mean
* Weighted mean
* Median
* Mode
* Range
* Variance
* Population variance
* Standard deviation
* Population standard deviation
* Moment
* Kurtosis
* Skewness
* Covariance
* Weighted covariance
* Population covariance
* Quantile
* Percentile

### Correlation functions

* Pearson
Expand All @@ -54,22 +74,18 @@ Check out the [API reference](https://hexdocs.pm/numerix/api-reference.html) for
* Logit
* Logistic

### Statistics
### Window functions

* Mean
* Weighted mean
* Median
* Mode
* Range
* Variance
* Population variance
* Standard deviation
* Population standard deviation
* Moment
* Kurtosis
* Skewness
* Covariance
* Weighted covariance
* Population covariance
* Quantile
* Percentile
* Gaussian

### Linear algebra

* Dot product
* L1-norm
* L2-norm
* p-norm
* Vector subtraction and multiplication

### Kernel functions

* RBF
19 changes: 7 additions & 12 deletions lib/correlation.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule Numerix.Correlation do
alias Numerix.{Common, Statistics}

@moduledoc """
Statistical correlation functions between two vectors.
"""

import Numerix.LinearAlgebra
alias Numerix.{Common, Statistics}

@doc """
Calculates the Pearson correlation coefficient between two vectors.
"""
Expand All @@ -14,21 +15,15 @@ defmodule Numerix.Correlation do
def pearson(vector1, vector2) do
sum1 = vector1 |> Enum.sum
sum2 = vector2 |> Enum.sum

sum_of_squares1 = vector1 |> square |> Enum.sum
sum_of_squares2 = vector2 |> square |> Enum.sum

sum_of_products =
vector1
|> Stream.zip(vector2)
|> Stream.map(fn {x, y} -> x * y end)
|> Enum.sum
sum_of_products = vector1 |> dot_product(vector2)

size = length(vector1)
num = sum_of_products - (sum1 * sum2 / size)
density = :math.sqrt(
(sum_of_squares1 - :math.pow(sum1, 2) / size)
* (sum_of_squares2 - :math.pow(sum2, 2) / size))
density = (sum_of_squares1 - :math.pow(sum1, 2) / size)
|> Kernel.*(sum_of_squares2 - :math.pow(sum2, 2) / size)
|> :math.sqrt

case density do
0.0 -> 0.0
Expand Down
2 changes: 1 addition & 1 deletion lib/statistics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ defmodule Numerix.Statistics do
defp sum_powered_deviations(xs, n) do
x_mean = mean(xs)
xs
|> Enum.map(fn x -> :math.pow(x - x_mean, n) end)
|> Stream.map(fn x -> :math.pow(x - x_mean, n) end)
|> Enum.sum
end

Expand Down

0 comments on commit c80101c

Please sign in to comment.