probability
A set of tools that probably help out with probability
npm install probability
Want to see pretty graphs? Log in now!
17 | downloads in the last month |
Last Published By | |
---|---|
Version | 0.0.4 last updated a year ago |
License | MIT |
Keywords | expectation, probability, convolution, fft, correlation |
Repository | git://github.com/dropdownmenu/node-probability.git (git) |
Dependencies | underscore |
node-probability
A set of tools that probably help out with probability.
overview
This is a general tool box for discrete probability theory. It is capable of handling complex numbers or just plain sequences of numbers. Numbers are represented in one of two ways:
// the normal way to have a sequence of numbers
x = [0, 1, 2, 3, ...];
// a simple way to use complex numbers
y = {
// real part
real: [0, 1, 2, ...],
// imaginary part
img: [3, 4, 5, ...]
}
Functions will always return numbers in the second format since most operations will involve some sort of complex math. There are several utility functions included to help restore these objects to traditional arrays.
methods
var prob = require('probability');
FFT
fft
Takes the fft of a given signal. Will zero-pad the signal to the next power of two.
// fft of a real valued signal
X = prob.fft([0, 1, 2, 3]);
// fft of a complex valued signal with real and imaginary parts
// NOTE: arrays must be equal length
Y = prob.fft([0, 1, 2, 3], [4, 5, 6, 7]);
// fft of a complexvalued signal
Z = prob.fft({
real: [0, 1, 2, 3],
img: [4, 5, 6, 7]
});
`
ifft
Takes the ifft of a given signal. Has the same input format as prob.fft
, but referenced as prob.ifft
.
Moments
expectation / E
Calculates the expectation of a given input
// two ways to call the method
m = prob.expectation(x);
m = prob.E(x);
// you can also set the index of the 0th element of x
// i.e. this will calculate the expectation as if x[0] corresponds to time t = -10
m = prob.expectation(x, -10);
variance / VAR
Calculates the variance of an input in a similar way to expectation
// two ways to call the method
sigma = prob.variance(x);
sigma = prob.VAR(x);
// you can also set the index of the 0th element of x
sigma = prob.VAR(x, -10);
moment
Calculates the nth moment of x
// calculate the 3th moment
skew = prob.moment(x, 3);
// you can also set the index of the 0th element of x
skew = prob.expectation(x, 3, -10);
lotus
Calculate E[g(x)] for a provided function g.
prob.lotus(x, function(val, i, x) {
// val is current value for processing
// i is the index of the value
// x is the original array (val = x[i])
// ... do some math here ...
// you must return a single number
return newVal;
});
Convolution
convolution / conv
Performs a convolution of two signals
y = prob.convolution(x, h);
y = prob.conv(x, h);
correlation / R
Calculates the correlation of two signals. If only one input is given, then the autocorrelation is returned.
Rxy = prob.correlation(x, y);
Rx = prob.R(x);
spectralDensity / S
Calculates the spectral density of one of two signals.
Sxy = prob.spectralDensity(x, y);
Sx = prob.S(x);
Complex
complex.add
Adds two complex vectors
z = prob.complex.add(x, y);
complex.mul
Multiplies two complex vectors
z = prob.complex.mul(x, y);
complex.magnitude
Returns |x|
y = prob.complex.magnitude(x);
todo
Need to create basic tests that verify that these methods match up with something like Matlab.
history
0.0.1: Initial release with no tests
license
Copyright (c) 2012 Tim McGowan (dropdownmenu)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.