* @brief Perform the logistic-regression transition step */
| 226 | * @brief Perform the logistic-regression transition step |
| 227 | */ |
| 228 | AnyType |
| 229 | logregr_simple_step_transition::run(AnyType &args) { |
| 230 | LogRegrSPTransitionState<MutableArrayHandle<double> > state = args[0]; |
| 231 | if (args[1].isNull() || args[2].isNull()) { return args[0]; } |
| 232 | double y = args[1].getAs<bool>() ? 1. : -1.; |
| 233 | MappedColumnVector x; |
| 234 | try { |
| 235 | // an exception is raised in the backend if args[2] contains nulls |
| 236 | MappedColumnVector xx = args[2].getAs<MappedColumnVector>(); |
| 237 | // x is a const reference, we can only rebind to change its pointer |
| 238 | x.rebind(xx.memoryHandle(), xx.size()); |
| 239 | } catch (const ArrayWithNullException &e) { |
| 240 | return args[0]; |
| 241 | } |
| 242 | |
| 243 | // The following check was added with MADLIB-138. |
| 244 | if (!dbal::eigen_integration::isfinite(x)) { |
| 245 | warning("Design matrix is not finite."); |
| 246 | state.status = TERMINATED; |
| 247 | return state; |
| 248 | } |
| 249 | |
| 250 | if (state.numRows == 0) { |
| 251 | if (x.size() > std::numeric_limits<uint16_t>::max()){ |
| 252 | warning("Number of independent variables cannot be larger than 65535."); |
| 253 | state.status = TERMINATED; |
| 254 | return state; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | state.initialize(*this, static_cast<uint16_t>(x.size())); |
| 259 | if (!args[3].isNull()) { |
| 260 | LogRegrSPTransitionState<ArrayHandle<double> > previousState = args[3]; |
| 261 | |
| 262 | state = previousState; |
| 263 | state.reset(); |
| 264 | } |
| 265 | } |
| 266 | // Now do the transition step |
| 267 | state.numRows++; |
| 268 | double xc = dot(x, state.coef); |
| 269 | state.gradNew.noalias() += sigma(-y * xc) * y * trans(x); |
| 270 | |
| 271 | // Note: sigma(-x) = 1 - sigma(x). |
| 272 | double a = sigma(xc) * sigma(-xc); |
| 273 | state.X_transp_AX += x * trans(x) * a; |
| 274 | |
| 275 | state.logLikelihood -= std::log( 1. + std::exp(-y * xc) ); |
| 276 | |
| 277 | return state; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
nothing calls this directly
no test coverage detected