Fixes CdfLibrary.js

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

View File

@ -1,174 +1,170 @@
const { const {
Cdf, Cdf,
Pdf, Pdf,
ContinuousDistribution, ContinuousDistribution,
ContinuousDistributionCombination, ContinuousDistributionCombination,
scoringFunctions, scoringFunctions,
} = require("@foretold/cdf/lib"); } = require("@foretold/cdf/lib");
const _ = require("lodash"); const _ = require("lodash");
/** /**
* *
* @param xs * @param xs
* @param ys * @param ys
* @returns {{ys: *, xs: *}} * @returns {{ys: *, xs: *}}
*/ */
function cdfToPdf({ xs, ys }) { function cdfToPdf({ xs, ys }) {
let cdf = new Cdf(xs, ys); let cdf = new Cdf(xs, ys);
let pdf = cdf.toPdf(); let pdf = cdf.toPdf();
return { xs: pdf.xs, ys: pdf.ys }; return { xs: pdf.xs, ys: pdf.ys };
} }
/** /**
* *
* @param xs * @param xs
* @param ys * @param ys
* @returns {{ys: *, xs: *}} * @returns {{ys: *, xs: *}}
*/ */
function pdfToCdf({ xs, ys }) { function pdfToCdf({ xs, ys }) {
let cdf = new Pdf(xs, ys); let cdf = new Pdf(xs, ys);
let pdf = cdf.toCdf(); let pdf = cdf.toCdf();
return { xs: pdf.xs, ys: pdf.ys }; return { xs: pdf.xs, ys: pdf.ys };
} }
/** /**
* *
* @param sampleCount * @param sampleCount
* @param vars * @param vars
* @returns {{ys: *, xs: *}} * @returns {{ys: *, xs: *}}
*/ */
function mean(sampleCount, vars) { function mean(sampleCount, vars) {
let cdfs = vars.map(r => new Cdf(r.xs, r.ys)); let cdfs = vars.map(r => new Cdf(r.xs, r.ys));
let comb = new ContinuousDistributionCombination(cdfs); let comb = new ContinuousDistributionCombination(cdfs);
let newCdf = comb.combineYsWithMean(sampleCount); let newCdf = comb.combineYsWithMean(sampleCount);
return { xs: newCdf.xs, ys: newCdf.ys }; return { xs: newCdf.xs, ys: newCdf.ys };
} }
/** /**
* *
* @param sampleCount * @param sampleCount
* @param predictionCdf * @param predictionCdf
* @param resolutionCdf * @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 toCdf = (r) => (new Cdf(r.xs, r.ys));
let prediction = toCdf(predictionCdf); let prediction = toCdf(predictionCdf);
if (_.isFinite(resolutionUniformAdditionWeight)){ if (_.isFinite(resolutionUniformAdditionWeight)) {
prediction = prediction.combineWithUniformOfCdf( prediction = prediction.combineWithUniformOfCdf(
{ {
cdf: toCdf(resolutionCdf), cdf: toCdf(resolutionCdf),
uniformWeight: resolutionUniformAdditionWeight, uniformWeight: resolutionUniformAdditionWeight,
sampleCount sampleCount
}
);
}
return scoringFunctions.distributionInputDistributionOutputMarketless({
predictionCdf: prediction,
resultCdf: toCdf(resolutionCdf),
sampleCount,
});
}
/**
*
* @param sampleCount
* @param 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 }) {
let cdf = new Cdf(xs, ys);
return cdf.findY(x);
}
/**
*
* @param x
* @param xs
* @param ys
* @returns {number[]}
*/
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 }) {
let cdf = new Cdf(xs, ys);
return cdf.findX(y);
}
/**
*
* @param xs
* @param ys
* @returns {number[]}
*/
function integral({ xs, ys }) {
if (_.includes(ys, NaN)){
return NaN;
}
else if (_.includes(ys, Infinity) && _.includes(ys, -Infinity)){
return NaN;
}
else if (_.includes(ys, Infinity)){
return Infinity;
}
else if (_.includes(ys, -Infinity)){
return -Infinity;
}
let integral = 0;
for (let i = 1; i < ys.length; i++) {
let thisY = ys[i];
let lastY = ys[i - 1];
let thisX = xs[i];
let lastX = xs[i - 1];
if (
_.isFinite(thisY) && _.isFinite(lastY) &&
_.isFinite(thisX) && _.isFinite(lastX)
) {
let sectionInterval = ((thisY + lastY) / 2) * (thisX - lastX);
integral = integral + sectionInterval;
} }
);
}
return integral;
} }
module.exports = { return scoringFunctions.distributionInputDistributionOutputMarketless({
cdfToPdf, predictionCdf: prediction,
pdfToCdf, resultCdf: toCdf(resolutionCdf),
findY, sampleCount,
findX, });
convertToNewLength, }
mean,
scoreNonMarketCdfCdf,
differentialEntropy,
integral,
};
/**
*
* @param sampleCount
* @param 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 }) {
let cdf = new Cdf(xs, ys);
return cdf.findY(x);
}
/**
*
* @param x
* @param xs
* @param ys
* @returns {number[]}
*/
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 }) {
let cdf = new Cdf(xs, ys);
return cdf.findX(y);
}
/**
*
* @param xs
* @param ys
* @returns {number[]}
*/
function integral({ xs, ys }) {
if (_.includes(ys, NaN)) {
return NaN;
} else if (_.includes(ys, Infinity) && _.includes(ys, -Infinity)) {
return NaN;
} else if (_.includes(ys, Infinity)) {
return Infinity;
} else if (_.includes(ys, -Infinity)) {
return -Infinity;
}
let integral = 0;
for (let i = 1; i < ys.length; i++) {
let thisY = ys[i];
let lastY = ys[i - 1];
let thisX = xs[i];
let lastX = xs[i - 1];
if (
_.isFinite(thisY) && _.isFinite(lastY) &&
_.isFinite(thisX) && _.isFinite(lastX)
) {
let sectionInterval = ((thisY + lastY) / 2) * (thisX - lastX);
integral = integral + sectionInterval;
}
}
return integral;
}
module.exports = {
cdfToPdf,
pdfToCdf,
findY,
findX,
convertToNewLength,
mean,
scoreNonMarketCdfCdf,
differentialEntropy,
integral,
};