diff --git a/index.js b/index.js index ba4b4089..3f3f16d3 100644 --- a/index.js +++ b/index.js @@ -44,29 +44,50 @@ const find_beta_from_ci = ({ci_lower, ci_upper}) => { let result = (f_h - f)/h return result } + + // backtracking line search + // + // Once we know the direction, how far to go along it? + const get_optimal_step_size_a = ({a,b, d, is_a}) => { + let dir = d_a > 0 ? 1 : -1 + + let step_size_min = 0 + let loss_s_min = is_a ? loss(a + step_size_min * dir, b) : loss(a, b + step_size_min * dir) + + let step_size_max = 0.1 + let loss_s_max = is_a ? loss(a + step_size_max * dir, b) : loss(a, b + step_size_max * dir) + + + for(let i=0; i<20; i++){ + if(loss_s_min < loss_s_max){ + step_size_max = (step_size_max + step_size_min) / 2 + loss_s_max = is_a ? loss(a + step_size_max * dir, b) : loss(a, b + step_size_max * dir) + }else{ + step_size_min = (step_size_max + step_size_min) / 2 + loss_s_min = is_a ? loss(a + step_size_min * dir, b) : loss(a, b + step_size_min * dir) + } + } + return (step_size_min + step_size_max)/2 + } // gradient descent step const gradient_descent = (a_init,b_init) => { - let epsilon = 2**(-14) // 1/16384 - let n_a = 2 - let n_b = 2 let a = a_init let b = b_init let max_steps = 2000 for(let i = 0; i