| 119438 | return dist.mean(mean).stdev(stdev); |
| 119439 | } |
| 119440 | function kde(support, bandwidth) { |
| 119441 | const kernel = gaussian(); |
| 119442 | let n = 0; |
| 119443 | const dist = { |
| 119444 | data (_) { |
| 119445 | if (arguments.length) { |
| 119446 | support = _; |
| 119447 | n = _ ? _.length : 0; |
| 119448 | return dist.bandwidth(bandwidth); |
| 119449 | } else return support; |
| 119450 | }, |
| 119451 | bandwidth (_) { |
| 119452 | if (!arguments.length) return bandwidth; |
| 119453 | bandwidth = _; |
| 119454 | if (!bandwidth && support) bandwidth = estimateBandwidth(support); |
| 119455 | return dist; |
| 119456 | }, |
| 119457 | sample () { |
| 119458 | return support[~~(random() * n)] + bandwidth * kernel.sample(); |
| 119459 | }, |
| 119460 | pdf (x) { |
| 119461 | let y = 0, i = 0; |
| 119462 | for(; i < n; ++i)y += kernel.pdf((x - support[i]) / bandwidth); |
| 119463 | return y / bandwidth / n; |
| 119464 | }, |
| 119465 | cdf (x) { |
| 119466 | let y = 0, i = 0; |
| 119467 | for(; i < n; ++i)y += kernel.cdf((x - support[i]) / bandwidth); |
| 119468 | return y / n; |
| 119469 | }, |
| 119470 | icdf () { |
| 119471 | throw Error("KDE icdf not supported."); |
| 119472 | } |
| 119473 | }; |
| 119474 | return dist.data(support); |
| 119475 | } |
| 119476 | function sampleLogNormal(mean, stdev) { |
| 119477 | mean = mean || 0; |
| 119478 | stdev = stdev == null ? 1 : stdev; |