| 392 | |
| 393 | |
| 394 | void __mlogistic_trans_compute (MutableClusteredState& state, |
| 395 | const MappedColumnVector& x, const double& y) |
| 396 | { |
| 397 | int numCategories = state.numCategories -1; |
| 398 | ColumnVector yVec(numCategories); |
| 399 | yVec.fill(0); |
| 400 | |
| 401 | //Pivot around the reference category |
| 402 | if (y > state.refCategory) { |
| 403 | yVec((int)y - 1) = 1; |
| 404 | } else if (y < state.refCategory) { |
| 405 | yVec((int)y) = 1; |
| 406 | } |
| 407 | |
| 408 | // if ((int)y != 0) { |
| 409 | // yVec(((int)y) - 1) = 1; |
| 410 | // } |
| 411 | |
| 412 | /* |
| 413 | Compute the parameter vector (the 'pi' vector in the documentation) |
| 414 | for the data point being processed. |
| 415 | Casting the coefficients into a matrix makes the calculation simple. |
| 416 | */ |
| 417 | Matrix coef = state.coef; |
| 418 | coef.resize(numCategories, state.widthOfX/numCategories); |
| 419 | |
| 420 | //Store the intermediate calculations because we'll reuse them in the LLH |
| 421 | ColumnVector t1 = x; //t1 is vector of size state.widthOfX |
| 422 | t1 = coef*x; |
| 423 | /* |
| 424 | Note: The above 2 lines could have been written as: |
| 425 | ColumnVector t1 = -coef*x; |
| 426 | |
| 427 | but this creates warnings. These warnings are somehow related to the factor |
| 428 | that x is an immutable type. |
| 429 | */ |
| 430 | |
| 431 | ColumnVector t2 = t1.array().exp(); |
| 432 | double t3 = 1 + t2.sum(); |
| 433 | ColumnVector pi = t2/t3; |
| 434 | //The gradient matrix has numCatergories rows and widthOfX columns |
| 435 | Matrix grad = -yVec * x.transpose() + pi * x.transpose(); |
| 436 | //We cast the gradient into a vector to make the math easier. |
| 437 | grad.resize(state.widthOfX, 1); |
| 438 | for (int i = 0; i < state.widthOfX; i++) |
| 439 | { |
| 440 | state.meat_half(0,i) += grad(i); |
| 441 | } |
| 442 | |
| 443 | // Compute the 'a' matrix. |
| 444 | Matrix a(numCategories,numCategories); |
| 445 | Matrix piDiag = pi.asDiagonal(); |
| 446 | a = pi * pi.transpose() - piDiag; |
| 447 | |
| 448 | //Start the Hessian calculations |
| 449 | //Matrix X_transp_AX(numCategories * state.widthOfX, numCategories * state.widthOfX); |
| 450 | Matrix X_transp_AX( (int)state.widthOfX, (int)state.widthOfX); |
| 451 | /* |