Fixes CdfLibrary.js

This commit is contained in:
Roman Galochkin 2020-02-25 13:13:17 +03:00
parent 4fc7723e32
commit 9349930ad8

View File

@ -4,57 +4,57 @@ const {
ContinuousDistribution,
ContinuousDistributionCombination,
scoringFunctions,
} = require("@foretold/cdf/lib");
const _ = require("lodash");
} = require("@foretold/cdf/lib");
const _ = require("lodash");
/**
/**
*
* @param xs
* @param ys
* @returns {{ys: *, xs: *}}
*/
function cdfToPdf({ xs, ys }) {
function cdfToPdf({ xs, ys }) {
let cdf = new Cdf(xs, ys);
let pdf = cdf.toPdf();
return { xs: pdf.xs, ys: pdf.ys };
}
}
/**
/**
*
* @param xs
* @param ys
* @returns {{ys: *, xs: *}}
*/
function pdfToCdf({ xs, ys }) {
function pdfToCdf({ xs, ys }) {
let cdf = new Pdf(xs, ys);
let pdf = cdf.toCdf();
return { xs: pdf.xs, ys: pdf.ys };
}
}
/**
/**
*
* @param sampleCount
* @param vars
* @returns {{ys: *, xs: *}}
*/
function mean(sampleCount, vars) {
function mean(sampleCount, vars) {
let cdfs = vars.map(r => new Cdf(r.xs, r.ys));
let comb = new ContinuousDistributionCombination(cdfs);
let newCdf = comb.combineYsWithMean(sampleCount);
return { xs: newCdf.xs, ys: newCdf.ys };
}
}
/**
/**
*
* @param sampleCount
* @param predictionCdf
* @param resolutionCdf
*/
function scoreNonMarketCdfCdf(sampleCount, predictionCdf, resolutionCdf, resolutionUniformAdditionWeight=0) {
function scoreNonMarketCdfCdf(sampleCount, predictionCdf, resolutionCdf, resolutionUniformAdditionWeight = 0) {
let toCdf = (r) => (new Cdf(r.xs, r.ys));
let prediction = toCdf(predictionCdf);
if (_.isFinite(resolutionUniformAdditionWeight)){
if (_.isFinite(resolutionUniformAdditionWeight)) {
prediction = prediction.combineWithUniformOfCdf(
{
cdf: toCdf(resolutionCdf),
@ -69,75 +69,72 @@ const {
resultCdf: toCdf(resolutionCdf),
sampleCount,
});
}
}
/**
/**
*
* @param sampleCount
* @param cdf
*/
function differentialEntropy(sampleCount, cdf) {
function differentialEntropy(sampleCount, cdf) {
let toCdf = (r) => (new Cdf(r.xs, r.ys));
return scoringFunctions.differentialEntropy({
cdf: toCdf(cdf),
sampleCount: sampleCount
});
}
}
/**
/**
*
* @param x
* @param xs
* @param ys
* @returns {number}
*/
function findY(x, { xs, ys }) {
function findY(x, { xs, ys }) {
let cdf = new Cdf(xs, ys);
return cdf.findY(x);
}
}
/**
/**
*
* @param x
* @param xs
* @param ys
* @returns {number[]}
*/
function convertToNewLength(n, { xs, ys }) {
function convertToNewLength(n, { xs, ys }) {
let dist = new ContinuousDistribution(xs, ys);
return dist.convertToNewLength(n);
}
}
/**
/**
*
* @param y
* @param xs
* @param ys
* @returns {number}
*/
function findX(y, { xs, ys }) {
function findX(y, { xs, ys }) {
let cdf = new Cdf(xs, ys);
return cdf.findX(y);
}
}
/**
/**
*
* @param xs
* @param ys
* @returns {number[]}
*/
function integral({ xs, ys }) {
if (_.includes(ys, NaN)){
function integral({ xs, ys }) {
if (_.includes(ys, NaN)) {
return NaN;
}
else if (_.includes(ys, Infinity) && _.includes(ys, -Infinity)){
} else if (_.includes(ys, Infinity) && _.includes(ys, -Infinity)) {
return NaN;
}
else if (_.includes(ys, Infinity)){
} else if (_.includes(ys, Infinity)) {
return Infinity;
}
else if (_.includes(ys, -Infinity)){
} else if (_.includes(ys, -Infinity)) {
return -Infinity;
}
@ -158,9 +155,9 @@ const {
}
return integral;
}
}
module.exports = {
module.exports = {
cdfToPdf,
pdfToCdf,
findY,
@ -170,5 +167,4 @@ const {
scoreNonMarketCdfCdf,
differentialEntropy,
integral,
};
};