From 9b9aaf8f8ab907d2167f87f5fcb5bb504abb8755 Mon Sep 17 00:00:00 2001 From: solo474 Date: Tue, 27 Oct 2020 15:05:01 +0000 Subject: [PATCH 1/2] add samples input option as alternative to precession Signed-off-by: solo474 --- lib/time-series.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/time-series.js diff --git a/lib/time-series.js b/lib/time-series.js new file mode 100644 index 0000000..e225722 --- /dev/null +++ b/lib/time-series.js @@ -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]]; + }); +}; From 99688de8974f23be473db107b6cbda09da3002ef Mon Sep 17 00:00:00 2001 From: solo474 Date: Tue, 27 Oct 2020 15:45:36 +0000 Subject: [PATCH 2/2] add gaussian pdf and cdf function creators --- lib/gaussian.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/gaussian.js diff --git a/lib/gaussian.js b/lib/gaussian.js new file mode 100644 index 0000000..adfdff9 --- /dev/null +++ b/lib/gaussian.js @@ -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; + } +}