src/forecast.js
// then exceed the range of the data, so we went with the simpler, better behaved kernels in kde().
then exceed the range of the data, so we went with the simpler, better behaved kernels in kde().
then exceed the range of the data so we went with the simpler better behaved kernels in kde () .
function kdeWiden(XW, opts={}) {
  opts = Object.assign({hp:1, hf: null, wf:8, n: 80, outputKernels: false}, opts)
  let {hp, hf, wf, n, outputKernels} = opts
  const norm = gaussian(0, 1)
  const minV = d3.min(XW, d => d[0])
  const maxV = d3.max(XW, d => d[0])
  if (!hf) {
    hf = Math.max(maxV - minV, 0.00001)
  }
  const h = hf*hp
  const rangeValues = []
  for (const [x, w] of XW) {
    const value = h * norm.ppf(0.999) / (w * wf)
    rangeValues.push(x + value)
    rangeValues.push(x - value)
  }
  const domain = d3.extent(rangeValues)
  const step = (domain[1] - domain[0]) / n
  const sc = 1 / (norm.pdf((0) / h))
  const vs = []
  const kernelVs = []
  for (let a = domain[0]; a < domain[1]; a += step) {
    let totalV = 0
    for (const [i, [x, w]] of XW.entries()) {
      const v = sc * w * norm.pdf((a - x)*w*wf / h) / XW.length
      totalV += v 
      if (outputKernels) {
        kernelVs.push({v: a, p: v , k: i})
      }
    }
    vs.push({v: a, p: totalV })
  }
  const out = {ps: vs}
  if (outputKernels) {
    out.kernelPs = kernelVs
  }
  return out
}
function kdeWiden ( XW , opts = { } ) { opts = Object . assign ( { hp : 1 , hf : null , wf : 8 , n : 80 , outputKernels : false } , opts ) let { hp , hf , wf , n , outputKernels } = opts const norm = gaussian ( 0 , 1 ) const minV = d3 . min ( XW , d => d [ 0 ] ) const maxV = d3 . max ( XW , d => d [ 0 ] ) if ( ! hf ) { hf = Math . max ( maxV - minV , 0.00001 ) } const h = hf * hp const rangeValues = [ ] for ( const [ x , w ] of XW ) { const value = h * norm . ppf ( 0.999 ) / ( w * wf ) rangeValues . push ( x + value ) rangeValues . push ( x - value ) } const domain = d3 . extent ( rangeValues ) const step = ( domain [ 1 ] - domain [ 0 ] ) / n const sc = 1 / ( norm . pdf ( ( 0 ) / h ) ) const vs = [ ] const kernelVs = [ ] for ( let a = domain [ 0 ] ; a < domain [ 1 ] ; a += step ) { let totalV = 0 for ( const [ i , [ x , w ] ] of XW . entries ( ) ) { const v = sc * w * norm . pdf ( ( a - x ) * w * wf / h ) / XW . length totalV += v if ( outputKernels ) { kernelVs . push ( { v : a , p : v , k : i } ) } } vs . push ( { v : a , p : totalV } ) } const out = { ps : vs } if ( outputKernels ) { out . kernelPs = kernelVs } return out }
then exceed the range of the data, so we went with the simpler, better behaved kernels in kde().
then exceed the range of the data so we went with the simpler better behaved kernels in kde () .