Changeset 8806009 in tol


Ignore:
Timestamp:
Aug 10, 2009, 1:41:14 PM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
d346108
Parents:
41d0e3c
Message:

Fixes #718

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1208 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/bmath/bstat/logit.cpp

    r41d0e3c r8806009  
    6363  BTimer tm(BText("Logit model (")+N+"x"+n+")");
    6464
    65   BMatrix<BDat> dif, f(N,1), H_(n,n);
     65  BMatrix<BDat> dif, f(N,1), H_(n,n), Xb(N,1);
    6666  e.Alloc(N,1);
    6767  p.Alloc(N,1);
    … …  
    7474  clock_t tm_1, tm_0 = BTimer::Clocks();
    7575  BReal tm_dif;
     76  BDat Lp, L1p;
    7677
    7778  for(iter=0; iter<BDat::MaxIter(); iter++)
    … …  
    8182    if(iter==0)
    8283    {
    83       p.SetAllValuesTo(0);
     84      Xb.SetAllValuesTo(0);
    8485    }
    8586    else
    … …  
    8788    //Std(BText("\nTRACE Logit B=\n")+B.Name());
    8889    //Std(BText("\nTRACE Logit X=\n")+X.Name());
    89       p = X*B;
    90     //Std(BText("\nTRACE Logit p=\n")+p.Name());
     90      Xb = X*B;
     91    //Std(BText("\nTRACE Logit Xb=\n")+Xb.Name());
    9192    }
    9293    oldLlh = lnLikelyhood;
    … …  
    9495    for(i=0; i<N; i++)
    9596    {
    96       p(i,0) = 1.0/(1+Exp(-p(i,0)));
    97       f(i,0) = p(i,0)*(1-p(i,0));
     97      if(Xb(i,0)>=0)
     98      {
     99        p(i,0) = 1.0/(Exp(-Xb(i,0))+1.0);
     100      }
     101      else
     102      {
     103        p(i,0) = Exp(Xb(i,0))/(Exp(Xb(i,0))+1.0);
     104      }
     105      f(i,0) = p(i,0)*(1.0-p(i,0));
    98106      e(i,0) = y(i,0)-p(i,0);
    99107      for(j=0; j<n; j++)
    … …  
    105113        }
    106114      }
    107             if(Abs(e(i,0))<=BDat::Tolerance()) { lnL(i,0) = 0.0;             }
    108       else if(Abs(e(i,0))==1    )
    109       {
    110         #if defined(_MSC_VER) || defined(UNIX)
    111         //lnL(i,0) = -INFINITY;
    112         lnL(i,0) = BDat::NegInf();
    113         #else
    114          lnL(i,0) = -1.0/0.0;
    115         #endif
    116       }
    117       else
    118       {
    119               lnL(i,0) = y(i,0)*Log(p(i,0))+(1-y(i,0))*Log(1-p(i,0));
    120       }
    121       // Std(BText("\ny=")+y(i,0)+"\tp="+p(i,0)+"\te="+e(i,0)+"\tlnL="+lnL(i,0));
     115            if(Abs(e(i,0))<=BDat::Tolerance()) { lnL(i,0) = 0.0; }
     116      else
     117      {
     118        if(Xb(i,0)>=0)
     119        {
     120                Lp  = Log(p(i,0));
     121          L1p = -Xb(i,0)-Log(1+Exp(-Xb(i,0)));
     122        }
     123        else
     124        {
     125                Lp  =  Xb(i,0)-Log(1+Exp( Xb(i,0)));
     126          L1p = Log(1-p(i,0));
     127        }
     128        lnL(i,0) = y(i,0)*Lp + (1-y(i,0))*L1p;
     129      }
     130      if(!lnL(i,0).IsFinite())
     131      {
     132        Std(BText("\n  Non finite log-likelihood i=")+i+"\ty="+y(i,0)+"\tXb="+Xb(i,0)+"\tp="+p(i,0)+"\tlog(p)="+Lp+"\tlog(1-p)="+L1p+"\tlnL="+lnL(i,0));
     133      }
    122134      lnLikelyhood += lnL(i,0);
    123135    }
    … …  
    141153
    142154    if(advance.IsUnknown()) { break; }
    143     B -= dif;
    144155    if(norm      < DEpsilon()       ) { break; }
    145156    if(maxAbsDif < BDat::Tolerance()) { break; }
    146157    if(advance   < DEpsilon()       ) { break; }
    147158    if(Abs(oldLlh-lnLikelyhood) < DEpsilon()) { break; }
     159    if(iter==BDat::MaxIter()-1) { break; }
     160    B -= dif;
    148161  }
    149162  return BTRUE;
    … …  
    194207  BReal tm_dif;
    195208  lnLikelyhood = BDat::NegInf();
     209  BDat _p, _1p;
    196210  for(iter=0; iter<BDat::MaxIter(); iter++)
    197211  {
    … …  
    203217    for(i=0; i<N; i++)
    204218    {
    205       p(i,0) = BNormalDist::Dist01(XB(i,0));
     219      _1p = BNormalDist::Dist01(-XB(i,0));
     220       _p = BNormalDist::Dist01( XB(i,0));
     221      p(i,0) = _p;
    206222      f(i,0) = Exp(-0.5*(XB(i,0)*XB(i,0)))/Sqrt(2.0*BDat::Pi());//BNormalDist::Dens01(XB(i,0));
    207       l(i,0) = (y(i,0)==0)?-f(i,0)/(1-p(i,0)):f(i,0)/p(i,0);
    208       e(i,0) = y(i,0)-p(i,0);
     223      l(i,0) = (y(i,0)==0)?-f(i,0)/(_1p):f(i,0)/_p;
     224      e(i,0) = y(i,0)-_p;
    209225      for(j=0; j<n; j++)
    210226      {
    … …  
    216232      }
    217233            if(Abs(e(i,0))<=BDat::Tolerance()) { lnL(i,0) = 0.0;             }
    218       else if(Abs(e(i,0))==1                )
    219       {
    220         lnL(i,0) = BDat::NegInf();
    221       }
    222234      else
    223235      {
    224               lnL(i,0) = y(i,0)*Log(p(i,0))+(1-y(i,0))*Log(1-p(i,0));
     236              lnL(i,0) = y(i,0)*Log(_p)+(1-y(i,0))*Log(_1p);
    225237      }
    226238//    Std(BText("\ny=")+y(i,0)+"\tXB="+XB(i,0)+
    227 //              "\tp="+p(i,0)+"\tf="+f(i,0)+"\tl="+l(i,0)+
     239//              "\tp="+_p+"\tf="+f(i,0)+"\tl="+l(i,0)+
    228240//              "\te="+e(i,0)+"\tlnL="+lnL(i,0));
    229241      lnLikelyhood += lnL(i,0);
    … …  
    247259
    248260    if(advance.IsUnknown()) { break; }
    249     B -= dif;
    250261    if(norm          < chop               ) { break; }
    251262    if(maxAbsDif < BDat::Tolerance()) { break; }
    252263    if(advance   < chop               ) { break; }
    253264    if(Abs(oldLlh-lnLikelyhood) < DEpsilon()) { break; }   
     265    if(iter==BDat::MaxIter()-1) { break; }
     266    B -= dif;
    254267  }
    255268  return BTRUE;
Note: See TracChangeset for help on using the changeset viewer.