Overview
The method presented here for fitting a Poisson distribution to a dataset utilizes Newton's method
Implementation
The implementation uses the Poisson glm library to compute the log Likelihood of the given dataset. The logLikelihood function in the library takes 3 parameters
- the dataset - an array of objects
- y function - a function that takes an item from the inputted dataset and returns the y value (count) registerd in that item
- mu function - function that returns the value of mu for the given set of {% \beta %} values.
{% \mu_i = exp[\vec{x}_i^T \vec{\beta}] %}
/*
Define the function to optimized. Here, the function takes four paremeters, the values for
the beta vector, and then runs the logLikelihood function in the poisson glm library.
*/
let f = function(b0, b1, b2, b3){
let logL = pl.logLikelihood(data, item=>item.num_awards, item=>{
let eta = b0+b1*item.progAcademic + b2*item.progVocational + b3*item.math;
let mu = Math.exp(eta);
return mu;
});
return logL;
}
//create an iterator from the newtons method library
let test = op.iterator(f, [0,0,0,0]);
//run several interations
for(let i=0;i<100;i++){
test.iterate();
}
Try it!