Changeset 07da02f in tol


Ignore:
Timestamp:
Dec 10, 2008, 3:20:52 PM (18 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
81d4b20
Parents:
de4a617
Message:

Using configuration parameters of trying order for MCMC initial values method

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/stdlib/math/stat/models/bayesian/bysMcmc/bsr/gibbs/_basicMaster.tol

    rde4a617 r07da02f  
    132132    {
    133133    //WriteLn("TRACESearching valid initial values");
    134       Real givenMatch = VMatMax(_.modelDef->A*givenInitVal-_.modelDef->a)<=0;
    135       If(givenMatch,
    136       {
    137         WriteLn(_MID+"Given init values matches constrain inequations.");
    138         givenInitVal
    139       },
    140       {
    141         WriteLn(_MID+"Given init values doesn't match constrain inequations."
    142                 "Trying zero solution ...", "W");
    143         Real zeroMatch = VMatMax(-_.modelDef->a)<=0;
    144         If(zeroMatch,
     134      Set try = SetOfNameBlock
     135      (
     136        [[Text name="givenByUser", Real order=config_::bsr.iniVal.try.givenByUser]],
     137        [[Text name="zero",        Real order=config_::bsr.iniVal.try.zero]],
     138        [[Text name="cnstrMinNorm",Real order=config_::bsr.iniVal.try.cnstrMinNorm]],
     139        [[Text name="cnstrMLE",    Real order=config_::bsr.iniVal.try.cnstrMLE]]
     140      );
     141      Set try.sel = Select(try,Real(NameBlock method)
     142      { method::order });
     143      Set try.sort = Sort(try.sel,Real(NameBlock a, NameBlock b)
     144      { Compare(a::order,b::order) });
     145      Real method.num = 1;
     146      Real method.ok = False;
     147      VMatrix beta = Rand(_.N,1,?,?);
     148      While(And(!method.ok,method.num<=Card(try.sort)),
     149      {
     150        Text method.name = (try.sort[method.num])::name;
     151        Do WriteLn(_MID+"Trying feasible initial values for MCM by method "+
     152          method.name+" ...", "");
     153        VMatrix beta := Case(
     154        method.name=="givenByUser",
    145155        {
    146           WriteLn(_MID+"Zero solution matches constrain inequations.");
     156          givenInitVal
     157        },
     158        method.name=="zero",
     159        {
    147160          Rand(_.N,1,0,0)
    148161        },
     162        method.name=="givenByUser",
    149163        {
    150           WriteLn(_MID+"Zero init values doesn't match constrain inequations.\n"
    151                     "Trying constrained minimum norm solution ...", "W");
     164          beta := givenInitVal
     165        },
     166        method.name=="cnstrMinNorm",
     167        {
    152168          Matrix  Dt    = VMat2Mat(Tra(_.modelDef->A));
    153169          Matrix  d     = VMat2Mat(_.modelDef->a);
    154           Matrix  I     = Diag(_.N,1);
    155           Matrix  Z     = Rand(_.N,1,0,0);
    156           Matrix  cnstr = Rquadprog::solveQP(I, Z, -Dt, -d,
     170          Matrix  XtX   = Diag(_.N,1);
     171          Matrix  XtY   = Rand(_.N,1,0,0);
     172          Matrix  cnstr = Rquadprog::solveQP(XtX, XtY, -Dt, -d,
    157173                                            {[[Real verbose=TRUE]]})[1];
    158           VMatrix cnstr_ = Mat2VMat(cnstr);
    159           Real cnstrMatch=VMatMax(_.modelDef->A*cnstr_-_.modelDef->a)<=0;
    160           If(cnstrMatch,
    161           {
    162             WriteLn(_MID+"Constrained minimum norm solution "
    163                          "matches constrain inequations.");
    164             cnstr_
    165           },
    166           {
    167             WriteLn(_MID+"Constrained minimum norm solution doesn't match "
    168                          "constrain inequations.", "E");
    169             givenInitVal
    170           })
    171         })
    172       })
     174          Mat2VMat(cnstr)
     175        },
     176        method.name=="cnstrMLE",
     177        {
     178          Matrix  Dt    = VMat2Mat(Tra(_.modelDef->A));
     179          Matrix  d     = VMat2Mat(_.modelDef->a);
     180          Matrix  XtX   = VMat2Mat(MtMSqr(_.XplusU));
     181          Matrix  XtY   = VMat2Mat(Tra(Tra(_.YplusV)*_.XplusU));
     182          Matrix  cnstr = Rquadprog::solveQP(XtX, XtY, -Dt, -d,
     183                                            {[[Real verbose=TRUE]]})[1];
     184          Mat2VMat(cnstr)
     185        });
     186        Real method.ok :=  VMatMax(_.modelDef->A*beta-_.modelDef->a)<=0;
     187        If(method.ok,
     188          WriteLn(_MID+"Method "+method.name+" matches constraints."),
     189          WriteLn(_MID+"Method "+method.name+" DOESN'T match constraints."));
     190        Real method.num := method.num+1
     191      });
     192      beta
    173193    })
    174194  };
    … …  
    181201    Matrix aux=SetCol(EvalSet(_.modelDef->LinearBlock,Real(BSR.LinearBlock b)
    182202    { b->InitialValue }));
     203    VMatrix _.YplusV := _.outMis.blk::filter(_.Y);
     204  //WriteLn("TRACE"+_MID+" _.lin.blk.init 6");
     205    VMatrix _.XplusU := _.inpMis.blk::filter(_.modelDef->X);
    183206  //WriteLn("TRACE"+_MID+" _.lin.blk.init 2");
    184207    VMatrix beta0 = findMatchingBeta(Mat2VMat(aux));
    … …  
    189212    Real DoExternalFilter(0);
    190213    VMatrix sig_0 = _.sig.blk::draw(0, _.Y - _.modelDef->X*_.beta);
    191   //WriteLn("TRACE"+_MID+" _.lin.blk.init 5");
    192     VMatrix _.YplusV := _.outMis.blk::filter(_.Y);
    193   //WriteLn("TRACE"+_MID+" _.lin.blk.init 6");
    194     VMatrix _.XplusU := _.inpMis.blk::filter(_.modelDef->X);
    195214  //WriteLn("TRACE"+_MID+" _.lin.blk.init 7");
    196215    VMatrix _.filter := _.XplusU*_.beta;
Note: See TracChangeset for help on using the changeset viewer.