Fixes comments
This commit is contained in:
parent
5354d5be02
commit
6b256bb76e
|
@ -1,11 +1,14 @@
|
|||
// This module defines an abstract BinnedDistribution class, which
|
||||
// should be implemented for each distribution. You need to decide
|
||||
// how to bin the distribution (use _adabin unless there's a nicer
|
||||
// way for your distr) and how to choose the distribution's support.
|
||||
const _math = require("mathjs");
|
||||
const math = _math.create(_math.all);
|
||||
const jStat = require("jstat");
|
||||
|
||||
/**
|
||||
* This module defines an abstract BinnedDistribution class, which
|
||||
* should be implemented for each distribution. You need to decide
|
||||
* how to bin the distribution (use _adabin unless there's a nicer
|
||||
* way for your distr) and how to choose the distribution's support.
|
||||
*/
|
||||
|
||||
math.import({
|
||||
normal: jStat.normal,
|
||||
beta: jStat.beta,
|
||||
|
@ -14,6 +17,9 @@ math.import({
|
|||
});
|
||||
|
||||
class BaseDistributionBinned {
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
constructor(args) {
|
||||
this._set_props();
|
||||
this.max_bin_size = 0.5;
|
||||
|
@ -30,11 +36,18 @@ class BaseDistributionBinned {
|
|||
[this.pdf_vals, this.divider_pts] = this.bin();
|
||||
}
|
||||
|
||||
/**
|
||||
* this is hacky but class properties aren't always supported
|
||||
* @private
|
||||
*/
|
||||
_set_props() {
|
||||
// this is hacky but class properties aren't always supported
|
||||
throw new Error("NotImplementedError");
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {(number[]|[*])[]}
|
||||
* @private
|
||||
*/
|
||||
_adabin() {
|
||||
let point = this.start_point;
|
||||
let vals = [this.pdf_func(point)];
|
||||
|
@ -78,6 +91,10 @@ class BaseDistributionBinned {
|
|||
throw new Error("NotImplementedError");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
* @returns {(any|(function(*=): *))[]}
|
||||
*/
|
||||
get_params_and_pdf_func(args) {
|
||||
let args_str = args.toString() + ")";
|
||||
let substr = this.name + ".pdf(x, " + args_str;
|
||||
|
@ -95,11 +112,17 @@ class BaseDistributionBinned {
|
|||
}
|
||||
|
||||
class NormalDistributionBinned extends BaseDistributionBinned {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_set_props() {
|
||||
this.name = "normal";
|
||||
this.param_names = ["mean", "std"];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {(number|*)[]}
|
||||
*/
|
||||
get_bounds() {
|
||||
return [
|
||||
this.params.mean - 4 * this.params.std,
|
||||
|
@ -107,22 +130,34 @@ class NormalDistributionBinned extends BaseDistributionBinned {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {[[*], [*]]}
|
||||
*/
|
||||
bin() {
|
||||
return this._adabin(this.params.std);
|
||||
}
|
||||
}
|
||||
|
||||
class UniformDistributionBinned extends BaseDistributionBinned {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_set_props() {
|
||||
this.name = "uniform";
|
||||
this.param_names = ["start_point", "end_point"];
|
||||
this.num_bins = 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {*[]}
|
||||
*/
|
||||
get_bounds() {
|
||||
return [this.params.start_point, this.params.end_point];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {(*[])[]}
|
||||
*/
|
||||
bin() {
|
||||
let divider_pts = evenly_spaced_grid(
|
||||
this.params.start_point,
|
||||
|
@ -138,6 +173,9 @@ class UniformDistributionBinned extends BaseDistributionBinned {
|
|||
}
|
||||
|
||||
class LogNormalDistributionBinned extends BaseDistributionBinned {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_set_props() {
|
||||
this.name = "lognormal";
|
||||
this.param_names = ["normal_mean", "normal_std"];
|
||||
|
@ -145,6 +183,12 @@ class LogNormalDistributionBinned extends BaseDistributionBinned {
|
|||
this.n_largest_bound_sample = 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param samples
|
||||
* @param n
|
||||
* @returns {any}
|
||||
* @private
|
||||
*/
|
||||
_nth_largest(samples, n) {
|
||||
var largest_buffer = Array(n).fill(-Infinity);
|
||||
for (const sample of samples) {
|
||||
|
@ -159,6 +203,9 @@ class LogNormalDistributionBinned extends BaseDistributionBinned {
|
|||
return largest_buffer[n - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {(*|any)[]}
|
||||
*/
|
||||
get_bounds() {
|
||||
let samples = Array(this.n_bounds_samples)
|
||||
.fill(0)
|
||||
|
@ -169,11 +216,20 @@ class LogNormalDistributionBinned extends BaseDistributionBinned {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {[[*], [*]]}
|
||||
*/
|
||||
bin() {
|
||||
return this._adabin();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param start
|
||||
* @param stop
|
||||
* @param numel
|
||||
* @returns {*[]}
|
||||
*/
|
||||
function evenly_spaced_grid(start, stop, numel) {
|
||||
return Array(numel)
|
||||
.fill(0)
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
// The main algorithmic work is done by functions in this module.
|
||||
// It also contains the main function, taking the user's string
|
||||
// and returning pdf values and x's.
|
||||
const _math = require("mathjs");
|
||||
const bst = require("binary-search-tree");
|
||||
|
||||
const distrs = require("./distribution.js").distrs;
|
||||
const parse = require("./parse.js");
|
||||
const _math = require("mathjs");
|
||||
const math = _math.create(_math.all);
|
||||
const bst = require("binary-search-tree");
|
||||
|
||||
const NUM_MC_SAMPLES = 300;
|
||||
const OUTPUT_GRID_NUMEL = 300;
|
||||
|
||||
/**
|
||||
* The main algorithmic work is done by functions in this module.
|
||||
* It also contains the main function, taking the user's string
|
||||
* and returning pdf values and x's.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param start
|
||||
* @param stop
|
||||
|
@ -24,22 +27,23 @@ function evenly_spaced_grid(start, stop, numel) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Takes an array of strings like "normal(0, 1)" and
|
||||
* returns the corresponding distribution objects
|
||||
* @param substrings
|
||||
* @returns {*}
|
||||
*/
|
||||
function get_distributions(substrings) {
|
||||
// Takes an array of strings like "normal(0, 1)" and
|
||||
// returns the corresponding distribution objects
|
||||
let names_and_args = substrings.map(parse.get_distr_name_and_args);
|
||||
let pdfs = names_and_args.map(x => new distrs[x[0]](x[1]));
|
||||
return pdfs;
|
||||
}
|
||||
|
||||
// update the binary search tree with bin points of
|
||||
// deterministic_pdf transformed by tansform func
|
||||
// (transfrom func can be a stocahstic func with parameters
|
||||
// sampled from mc_distrs)
|
||||
/**
|
||||
* update the binary search tree with bin points of
|
||||
* deterministic_pdf transformed by tansform func
|
||||
* (transfrom func can be a stocahstic func with parameters
|
||||
* sampled from mc_distrs)
|
||||
*
|
||||
* @param transform_func
|
||||
* @param deterministic_pdf
|
||||
* @param mc_distrs
|
||||
|
@ -116,10 +120,11 @@ function update_transformed_divider_points_bst(
|
|||
return [start_pt, end_pt];
|
||||
}
|
||||
|
||||
// Take the binary search tree with transformed bin points,
|
||||
// and an array of pdf values associated with the bins,
|
||||
// and return a pdf over an evenly spaced grid
|
||||
/**
|
||||
* Take the binary search tree with transformed bin points,
|
||||
* and an array of pdf values associated with the bins,
|
||||
* and return a pdf over an evenly spaced grid
|
||||
*
|
||||
* @param pdf_vals
|
||||
* @param bst_pts_and_idxs
|
||||
* @param output_grid
|
||||
|
@ -177,13 +182,14 @@ function get_final_pdf(pdf_vals, bst_pts_and_idxs, output_grid) {
|
|||
return final_pdf_vals;
|
||||
}
|
||||
|
||||
// Entrypoint. Pass user input strings to this function,
|
||||
// get the corresponding pdf values and input points back.
|
||||
// If the pdf requires monte carlo (it contains a between-distr function)
|
||||
// we first determing which distr to have deterministic
|
||||
// and which to sample from. This is decided based on which
|
||||
// choice gives the least variance.
|
||||
/**
|
||||
* Entrypoint. Pass user input strings to this function,
|
||||
* get the corresponding pdf values and input points back.
|
||||
* If the pdf requires monte carlo (it contains a between-distr function)
|
||||
* we first determing which distr to have deterministic
|
||||
* and which to sample from. This is decided based on which
|
||||
* choice gives the least variance.
|
||||
*
|
||||
* @param user_input_string
|
||||
* @returns {([]|*[])[]}
|
||||
*/
|
||||
|
@ -261,10 +267,11 @@ function pluck_from_array(array, idx) {
|
|||
return [array[idx], array.slice(0, idx).concat(array.slice(idx + 1))];
|
||||
}
|
||||
|
||||
// If distr_string requires MC, try all possible
|
||||
// choices for the deterministic distribution,
|
||||
// and pick the one with the least variance.
|
||||
/**
|
||||
* If distr_string requires MC, try all possible
|
||||
* choices for the deterministic distribution,
|
||||
* and pick the one with the least variance.
|
||||
*
|
||||
* @param distr_string
|
||||
* @returns {(*|*[])[]|*[]}
|
||||
*/
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
// Functions for parsing/processing user input strings are here
|
||||
const _math = require("mathjs");
|
||||
const math = _math.create(_math.all);
|
||||
|
||||
// Functions for parsing/processing user input strings are here
|
||||
|
||||
const DISTR_REGEXS = [
|
||||
/beta\(/g,
|
||||
/(log)?normal\(/g,
|
||||
|
@ -10,6 +11,11 @@ const DISTR_REGEXS = [
|
|||
/uniform\(/g
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param user_input_string
|
||||
* @returns {{mm_args_string: string, outer_string: string}}
|
||||
*/
|
||||
function parse_initial_string(user_input_string) {
|
||||
let outer_output_string = "";
|
||||
let mm_args_string = "";
|
||||
|
@ -42,6 +48,10 @@ function parse_initial_string(user_input_string) {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mm_args_string
|
||||
* @returns {{distrs: [], weights: string}}
|
||||
*/
|
||||
function separate_mm_args(mm_args_string) {
|
||||
if (mm_args_string.endsWith(",")) {
|
||||
mm_args_string = mm_args_string.slice(0, -1);
|
||||
|
@ -68,6 +78,10 @@ function separate_mm_args(mm_args_string) {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param distr_string
|
||||
* @returns {[]}
|
||||
*/
|
||||
function get_distr_substrings(distr_string) {
|
||||
let substrings = [];
|
||||
for (let regex of DISTR_REGEXS) {
|
||||
|
@ -92,6 +106,10 @@ function get_distr_substrings(distr_string) {
|
|||
return substrings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param substr
|
||||
* @returns {(string|*)[]}
|
||||
*/
|
||||
function get_distr_name_and_args(substr) {
|
||||
let distr_name = "";
|
||||
let args_str = "";
|
||||
|
|
Loading…
Reference in New Issue
Block a user