GithubHelp home page GithubHelp logo

msharp / elixir-statistics Goto Github PK

View Code? Open in Web Editor NEW
138.0 6.0 31.0 215 KB

Statistical functions and distributions for Elixir

Home Page: http://hexdocs.pm/statistics/

License: Other

Elixir 100.00%

elixir-statistics's Introduction

Statistics

Build Status hex.pm version

Statistics functions and distributions for Elixir.

Usage

Add Statistics as a dependency in your mix.exs file to install from hex.pm.

def deps do
  [
    { :statistics, "~> 0.6"}
  ]
end

After you are done, run mix deps.get in your shell to fetch and compile Statistics.

To try it out, start an interactive Elixir shell with iex -S mix.

Get the median value from a list

iex> Statistics.median([1,2,3])
2

Calculate the variance of a list of values.

iex> Statistics.variance([1,2,3,4])
1.25

Or draw a random number from a Gaussian distribution with a mean of 1 and standard deviation of 2.

iex> Statistics.Distributions.Normal.rand(1, 2)
2.5998185179627384

Documentation

Elixir has great documentation tools using ex_doc.

The docs are hosted on hexdocs.pm/statistics.

Performance

This is not a library to use if you need fast computation.

Everything is implemented in Elixir. Many of the implementations use slow approximations, numerical function integration, or trial-and-error methods.

There is much room for improvement. To make this library really fast (and precise), we would probably need to interface with existing C libraries.

Contributing

I will accept pull requests.

If you want to contribute, please create a topic branch with tests and submit a pull request.

License

Apache 2

elixir-statistics's People

Contributors

cyrusofeden avatar irio avatar jackdoe avatar katafrakt avatar kirang89 avatar malcolmstill avatar msharp avatar mwmiller avatar opsb avatar siljesc avatar wkhere avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

elixir-statistics's Issues

Infinite loop in Statistics.Distributions.Binomial.rand(1, x)

To replicate:

  1. Set up project with deps {:statistics, "~> 0.6.2"}
  2. Enter elixir interface (iex)
  3. Type Statistics.Distributions.Binomial.rand(1, 0.5)
  4. Watch as the function keeps running.
  5. Use Ctrl-C (Windows) to terminate iex to recover

The root cause is likely within the pmf function, as Statistics.Distributions.Binomial.pmf(1, x).(y) being 0 no matter the values of x and y. This causes the rand function to go into an infinite loop.

Mode calculation (samples without modes + and multi-mode)

Hi there!

Thanks for providing a numbers and statistics library!

I think I might have found a little bug/edge case in the mode calculations:

iex(7)> Statistics.mode([1, 2, 3])        
1 # should be nil to the best of my knowledge as nothing occurs more than once
iex(8)> Statistics.mode([1, 2, 2, 3, 3])  
2 # should be [2, 3] as 2 values occur multiple times

Mode at Wikipedia

Given the list of data [1, 1, 2, 4, 4] the mode is not unique โ€“ the dataset may be said to be bimodal, while a set with more than two modes may be described as multimodal.

I can't find a good source of no mode if no values occurs most often right now.

For reference here's how statistex determines the mode (which arguably might be a bit verbose ๐Ÿคทโ€โ™€๏ธ )

Hypergeometric function

Need a robust implementation of the 2F1 hypergeometric function.

Currently, the t distribution CDF is calculated with a numerical approximation of the integral of the PDF. This is extremely slow, in particular when calculating the PPF, which executes CDF many times.

Shorter Functions

Here are shorter versions of some of the Match functions:

def median(list) do
Enum.sort(list) |> Enum.at(round(Float.floor(length(list)/2)))
end

def hist(list) do
list |> Enum.reduce(%{}, fn(tag, acc) -> Map.update(acc, tag, 1, &(&1 + 1)) end)
end

def mode(list) do
h = hist(list)
max = Map.values(h) |> Enum.max()
h |> Enum.find(fn {key,val} -> val == max end) |> elem(0)
end

@SPEC factorial(non_neg_integer) :: non_neg_integer
def factorial(n) when n < 0 do
raise ArithmeticError, message: "Argument n must be a positive number"
end
def factorial(n) when n == 0 or n == 1 do
1
end
def factorial(n) do
n*factorial(n-1)
end

Permutation and combination functions fail when k > N

The functions Math.permutation/2 and Math.combination/2 with arguments N, k should return 0 when k > N or N < 0 or k < 0. Currently it results in a call to the factorial function with a negative integer which errors (example below)

** (ArithmeticError) Argument n must be a positive number
    (statistics 0.6.3) lib/math/math.ex:194: Statistics.Math.factorial/1
    (statistics 0.6.3) lib/math/math.ex:246: Statistics.Math.permutation/2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.