Skip to content

add gaussian pdf and cdf functions #4

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/gaussian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { erf } from 'mathjs';

/**
* This file contains functions to produce
* Gaussian distribution and density functions
*/


/**
*
* @param sd - standard deviation
* @param m - mean
* @return {function(*): *} Gaussian probability density function
*/
export const createGaussianPDF = ({sd,m}) => {
return (x) => {
const exp = (-0.5)*(x-m/sd)*(x-m/sd);
return (1/(sd*Math.sqrt(2*Math.PI)))*Math.exp(exp);
}
}

/**
*
* @param sd - standard deviation
* @param m - mean
* @return {function(*): *} Gaussian probability density function
*/
export const createGaussianCDF = ({sd,m}) => {
return (x) => {
return (1 - erf((m - x ) / (Math.sqrt(2) * sd))) / 2;
}
}
37 changes: 37 additions & 0 deletions lib/time-series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This function generates a random time series for
* a given time interval with given precession,standard deviation, etc....
*/
import { generateRandomNumbers } from './random-sd';
import { getUnixTime,addMilliseconds,parseISO,getTime } from 'date-fns';

export const generateTimeSeries = ({
from,
to,
precession, // in milliseconds,
numberOfSamples,
sd
}) => {

const milliSecondsFrom = getTime(parseISO(from));
const milliSecondsTo = getTime(parseISO(to));
const duration = milliSecondsTo - milliSecondsFrom;
const __numberOfSamples = numberOfSamples
|| duration/precession
|| 1000;

const timeArray = new Array(Math.floor(__numberOfSamples))
.fill(1)
.map((i,index) =>{
return addMilliseconds(parseISO(from),index*precession);
});

const randomNumbers = generateRandomNumbers({
sd,
length: timeArray.length
});

return timeArray.map((time,index)=>{
return [time,randomNumbers[index]];
});
};