Example #1
0
AnyValue LogisticRegressionIRLS::transition(AbstractDBInterface &db,
    AnyValue args) {
    AnyValue::iterator arg(args);
    
    // Initialize Arguments from SQL call
    State state = *arg++;
    double y = *arg++ ? 1. : -1.;
    DoubleRow_const x = *arg++;

    // See MADLIB-138. At least on certain platforms and with certain versions,
    // LAPACK will run into an infinite loop if pinv() is called for non-finite
    // matrices. We extend the check also to the dependent variables.
    if (!boost::math::isfinite(y))
        throw std::invalid_argument("Dependent variables are not finite.");
    else if (!x.is_finite())
        throw std::invalid_argument("Design matrix is not finite.");

    if (state.numRows == 0) {
        state.initialize(db.allocator(AbstractAllocator::kAggregate), x.n_elem);
        if (!arg->isNull()) {
            const State previousState = *arg;
            
            state = previousState;
            state.reset();
        }
    }
    
    // Now do the transition step
    state.numRows++;

    // xc = x_i c
    double xc = as_scalar( x * state.coef );
        
    // a_i = sigma(x_i c) sigma(-x_i c)
    double a = sigma(xc) * sigma(-xc);
    
    // Note: sigma(-x) = 1 - sigma(x).
    //
    //             sigma(-y_i x_i c) y_i
    // z = x_i c + ---------------------
    //                     a_i
    double z = xc + sigma(-y * xc) * y / a;

    state.X_transp_Az += trans(x) * a * z;
    state.X_transp_AX += trans(x) * a * x;
        
    // We use state.sumy to store the log likelihood.
    //          n
    //         --
    // l(c) = -\  ln(1 + exp(-y_i * c^T x_i))
    //         /_
    //         i=1
    state.logLikelihood -= std::log( 1. + std::exp(-y * xc) );
    return state;
}