Fixes CdfLibrary.js
This commit is contained in:
parent
4fc7723e32
commit
9349930ad8
|
@ -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 ys
|
|
||||||
* @returns {{ys: *, xs: *}}
|
|
||||||
*/
|
|
||||||
function cdfToPdf({ xs, ys }) {
|
|
||||||
let cdf = new Cdf(xs, ys);
|
|
||||||
let pdf = cdf.toPdf();
|
|
||||||
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 cdfToPdf({ xs, ys }) {
|
||||||
let cdf = new Pdf(xs, ys);
|
let cdf = new Cdf(xs, ys);
|
||||||
let pdf = cdf.toCdf();
|
let pdf = cdf.toPdf();
|
||||||
return { xs: pdf.xs, ys: pdf.ys };
|
return { xs: pdf.xs, ys: pdf.ys };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param sampleCount
|
|
||||||
* @param vars
|
|
||||||
* @returns {{ys: *, xs: *}}
|
|
||||||
*/
|
|
||||||
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) {
|
|
||||||
let toCdf = (r) => (new Cdf(r.xs, r.ys));
|
|
||||||
let prediction = toCdf(predictionCdf);
|
|
||||||
if (_.isFinite(resolutionUniformAdditionWeight)){
|
|
||||||
prediction = prediction.combineWithUniformOfCdf(
|
|
||||||
{
|
|
||||||
cdf: toCdf(resolutionCdf),
|
|
||||||
uniformWeight: resolutionUniformAdditionWeight,
|
|
||||||
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 xs
|
* @param ys
|
||||||
* @param ys
|
* @returns {{ys: *, xs: *}}
|
||||||
* @returns {number[]}
|
*/
|
||||||
*/
|
function pdfToCdf({ xs, ys }) {
|
||||||
function convertToNewLength(n, { xs, ys }) {
|
let cdf = new Pdf(xs, ys);
|
||||||
let dist = new ContinuousDistribution(xs, ys);
|
let pdf = cdf.toCdf();
|
||||||
return dist.convertToNewLength(n);
|
return { xs: pdf.xs, ys: pdf.ys };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param y
|
* @param sampleCount
|
||||||
* @param xs
|
* @param vars
|
||||||
* @param ys
|
* @returns {{ys: *, xs: *}}
|
||||||
* @returns {number}
|
*/
|
||||||
*/
|
function mean(sampleCount, vars) {
|
||||||
function findX(y, { xs, ys }) {
|
let cdfs = vars.map(r => new Cdf(r.xs, r.ys));
|
||||||
let cdf = new Cdf(xs, ys);
|
let comb = new ContinuousDistributionCombination(cdfs);
|
||||||
return cdf.findX(y);
|
let newCdf = comb.combineYsWithMean(sampleCount);
|
||||||
}
|
|
||||||
|
return { xs: newCdf.xs, ys: newCdf.ys };
|
||||||
/**
|
}
|
||||||
*
|
|
||||||
* @param xs
|
/**
|
||||||
* @param ys
|
*
|
||||||
* @returns {number[]}
|
* @param sampleCount
|
||||||
*/
|
* @param predictionCdf
|
||||||
function integral({ xs, ys }) {
|
* @param resolutionCdf
|
||||||
if (_.includes(ys, NaN)){
|
*/
|
||||||
return NaN;
|
function scoreNonMarketCdfCdf(sampleCount, predictionCdf, resolutionCdf, resolutionUniformAdditionWeight = 0) {
|
||||||
}
|
let toCdf = (r) => (new Cdf(r.xs, r.ys));
|
||||||
else if (_.includes(ys, Infinity) && _.includes(ys, -Infinity)){
|
let prediction = toCdf(predictionCdf);
|
||||||
return NaN;
|
if (_.isFinite(resolutionUniformAdditionWeight)) {
|
||||||
}
|
prediction = prediction.combineWithUniformOfCdf(
|
||||||
else if (_.includes(ys, Infinity)){
|
{
|
||||||
return Infinity;
|
cdf: toCdf(resolutionCdf),
|
||||||
}
|
uniformWeight: resolutionUniformAdditionWeight,
|
||||||
else if (_.includes(ys, -Infinity)){
|
sampleCount
|
||||||
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,
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user