Changeset b85e9db in tol
- Timestamp:
- May 28, 2009, 2:53:03 PM (17 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- 7742d71
- Parents:
- 8c67d66
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tol/stdlib/math/stat/models/bayesian/bysMcmc/bsr/gibbs/_nonLinBlock.tol
r8c67d66 rb85e9db 12 12 "given BSR model.\n" 13 13 "Arguments are:\n" 14 " NameBlock filterEval: Evaluates the non linear filter of inputs or "14 " NameBlock usrFilter: Evaluates the non linear filter of inputs or " 15 15 "output. Must have next mandatory methods:\n" 16 16 " Text get.name(Real unused): Identifies the filter\n" … … 29 29 BlockSampler NonLinBlock( 30 30 //Filter evaluator 31 NameBlock filterEval,31 NameBlock usrFilter, 32 32 //Basic BSR that will be modified by filter 33 33 NameBlock bsr, … … 47 47 NameBlock _.bsr = bsr; 48 48 //Filter evaluator 49 NameBlock _.filter Eval = filterEval;49 NameBlock _.filter = usrFilter; 50 50 51 51 //Defining the block in the Markov Chain 52 52 Real _defined = define 53 53 ( 54 _.filter Eval::get.name(0),55 _.filter Eval::get.colNames(0),54 _.filter::get.name(0), 55 _.filter::get.colNames(0), 56 56 bsr::_.mcmcPartialHandler, 57 57 numBlock, … … 60 60 ); 61 61 //Name of affected segment in BSR 62 Text _.segmentName = _.filter Eval::get.segmentName(0);62 Text _.segmentName = _.filter::get.segmentName(0); 63 63 //Number of affected segment in BSR 64 64 Real _.segmentNum = FindIndexByName((_.bsr::_.modelDef)->NoiseDistrib, … … 78 78 }); 79 79 //For input filters is +1, for output ones -1 80 Real _.isInputFilter = If(FunctionExist("Set","_.filter Eval::get.linBlkNames"),81 { 82 If(Card(_.filter Eval::get.linBlkNames(0)),1,0)80 Real _.isInputFilter = If(FunctionExist("Set","_.filter::get.linBlkNames"), 81 { 82 If(Card(_.filter::get.linBlkNames(0)),1,0) 83 83 }, 84 84 { 85 85 0 86 86 }); 87 //Default value for marginal draw88 Matrix _default MarginalDraw = Rand(Card(_.colNames), 1, ?, ?);87 //Default value for custom partial draw 88 Matrix _default.custom.partial.draw = Rand(Card(_.colNames), 1, ?, ?); 89 89 //Optional filter method to be called before to call draw.scalar 90 90 //This method will be called just before to call draw method of corresponding 91 91 //internal non linear block in order to select which parameters will be drawn. 92 //It will return a matrix with marginal sampling in selected indexes and ? in92 //It will return a matrix with custom partial sampling in selected indexes and ? in 93 93 //other ones that will be sampled by non linear block in the standard way 94 Code _.marginalDraw = If(FunctionExist("Matrix","_.filterEval::marginalDraw"), 95 { 96 WriteLn(_MID+"Using specified marginalDraw method"); 97 _.filterEval::marginalDraw 94 Code _.custom.partial.draw = If( 95 FunctionExist("Matrix","_.filter::custom.partial.draw"), 96 { 97 WriteLn(_MID+"Using specified custom.partial.draw method"); 98 _.filter::custom.partial.draw 98 99 }, 99 100 { 100 WriteLn(_MID+"Using default marginalDraw method");101 Matrix marginalDraw(Real unused) { _defaultMarginalDraw }101 WriteLn(_MID+"Using default custom.partial.draw method"); 102 Matrix custom.partial.draw(Real unused) { _default.custom.partial.draw } 102 103 }); 103 104 //Optional filter method that returns log of prior density for one parameter 104 105 //Real get.priorLogDens(Real paramIdx) 105 Code _.get.priorLogDens = If(FunctionExist("Real","_.filter Eval::get.priorLogDens"),106 _.filter Eval::get.priorLogDens,106 Code _.get.priorLogDens = If(FunctionExist("Real","_.filter::get.priorLogDens"), 107 _.filter::get.priorLogDens, 107 108 Real (Real paramIdx) { 0 } ); 108 109 //Names of affected parameters in linear block … … 110 111 Set _.linBlkNames = If(!_.isInputFilter, Copy(Empty), 111 112 { 112 _.filter Eval::get.linBlkNames(0)113 _.filter::get.linBlkNames(0) 113 114 }); 114 115 //Positions of columns of affected parameters in linear block … … 131 132 //////////////////////////////////////////////////////////////////////////// 132 133 { 133 Real ok = _.filter Eval::initialize(0);134 Real setStore(_.filter Eval::get.parameter(0));134 Real ok = _.filter::initialize(0); 135 Real setStore(_.filter::get.parameter(0)); 135 136 ok 136 137 }; 137 138 139 /////////////////////////////////////////////////////////////////////////// 140 NameBlock get.evaluator(Real unused) 141 /////////////////////////////////////////////////////////////////////////// 142 { 143 Set segment = _.segment; 144 Set aux = _.segment->ArimaAuxInfo; 145 Set arima = _.segment->Arima; 146 NameBlock sigBlk = _.bsr::_.sig.blk; 147 VMatrix si = SubRow(SubCol(sigBlk::_.sigmaInv, 148 _.segment->EquIdx), _.segment->EquIdx); 149 150 NameBlock bsr = _.bsr; 151 NameBlock filter = _.filter; 152 Matrix u = VMat2Mat(_.store); 153 Matrix omega= If(!_.isInputFilter, Col(1), 154 SubRow(VMat2Mat(_.bsr::_.mln.blk::_.store), _.filterColIdx)); 155 156 Matrix _.W := VMat2Mat(SubRow(bsr::_.noise,_.segment->EquIdx)); 157 Matrix _.Z := _.W + _.Fi * omega; 158 Matrix Z = _.Z; 159 160 Code _getPriorLogDens = _.get.priorLogDens; 161 162 NameBlock [[ 163 Set _.segment = segment; 164 VMatrix _.fullNoise = bsr::_.noise; 165 NameBlock _.arimaBlk = bsr::_.arm.blk; 166 NameBlock _.sigBlk = bsr::_.sig.blk; 167 Code _filterEval = filter::eval; 168 Code _get.priorLogDens = _getPriorLogDens; 169 Matrix _.u = u; 170 Matrix _.omega= omega; 171 Matrix _.Z = Z; 172 173 VMatrix _.si = si; 174 Polyn _.dif = ARIMAGetDIF(arima); 175 Real _.d = aux::difDegree; 176 Real _.p = aux::p; 177 Real _.q = aux::q; 178 Real _.m = aux::m; 179 Real _.n = _.m - _.d; 180 181 //Parameter index 182 Real _.i = 1; 183 184 Matrix EvalFilteredNoise(Real x) 185 { 186 Real old = MatDat(_.u,_.i,1); 187 Real Do PutMatDat(_.u,_.i,1,x); 188 Matrix Fi = _filterEval(_.u); 189 Real Do PutMatDat(_.u,_.i,1,old); 190 Matrix fn = VMat2Mat(_.si*Mat2VMat(_.Z-Fi*_.omega)); 191 fn 192 }; 193 Real LogDens.WhiteNoise(Real x) 194 { 195 Matrix ei = EvalFilteredNoise(x); 196 Real ld = -0.5*MatDat(MtMSqr(ei),1,1); 197 //WriteLn("TRACE LogDens.WhiteNoise("+_.colNames[_.i]+","<<x+")="<<ld); 198 ld+_get.priorLogDens(_.i) 199 }; 200 Real LogDens.ARMA(Real x) 201 { 202 Matrix Zi = EvalFilteredNoise(x); 203 VMatrix Zi_ = Mat2VMat(Zi); 204 VMatrix ei_ = _.arimaBlk::filterJustBlock(_.segment, Zi_); 205 Matrix ei = VMat2Mat(ei_); 206 Real ld = -0.5*MatDat(MtMSqr(ei),1,1); 207 //WriteLn("TRACE LogDens.ARMA("+_.colNames[_.i]+","<<x+")="<<ld); 208 ld+_get.priorLogDens(_.i) 209 }; 210 Real LogDens.ARIMA(Real x) 211 { 212 Matrix Zi = EvalFilteredNoise(x); 213 //WriteLn("TRACE Zi("<<Rows(Zi)+","<<Columns(Zi)+")"); 214 Matrix Wi = Rand(_.d,1,0,0) << Sub(DifEq(_.dif/1,Zi),_.d+1,1,_.n,1); 215 //WriteLn("TRACE Wi("<<Rows(Wi)+","<<Columns(Wi)+")"); 216 VMatrix Wi_ = Mat2VMat(Wi); 217 VMatrix ei_ = _.arimaBlk::filterJustBlock(_.segment, Wi_); 218 //WriteLn("TRACE ei_("<<VRows(ei_)+","<<VColumns(ei_)+")"); 219 Matrix ei = VMat2Mat(Sub(ei_,_.d+1,1,_.n,1)); 220 //WriteLn("TRACE ei("<<Rows(ei)+","<<Columns(ei)+")"); 221 Real ld = -0.5*MatDat(MtMSqr(ei),1,1); 222 //WriteLn("TRACE LogDens.ARIMA("+_.colNames[_.i]+","<<x+")="<<ld); 223 ld+_get.priorLogDens(_.i) 224 }; 225 Code LogDens = Case( 226 Or(Not(_.arimaBlk::enabled),And(Not(_.p),Not(_.q))), 227 LogDens.WhiteNoise, 228 _.d==0, 229 LogDens.ARMA, 230 1==1, 231 LogDens.ARIMA ) 232 ]]}; 233 138 234 //////////////////////////////////////////////////////////////////////////// 139 235 Text _.autodoc.member.draw.scalar = … … 144 240 { 145 241 //WriteLn("TRACE"+_MID+"draw.scalar 1 "); 146 VMatrix fullNoise = _.bsr::_.noise; 147 NameBlock arimaBlk = _.bsr::_.arm.blk; 148 NameBlock sigBlk = _.bsr::_.sig.blk; 149 Matrix u = VMat2Mat(_.store); 150 //WriteLn("TRACE"+_MID+"draw.scalar 1.1 _.isInputFilter = "<<_.isInputFilter); 151 Matrix omega= If(!_.isInputFilter, Col(1), 152 SubRow(VMat2Mat(_.bsr::_.mln.blk::_.store), _.filterColIdx)); 153 //WriteLn("TRACE"+_MID+"draw.scalar 1.2 omega("<<Rows(omega)+"x"<<Columns(omega)); 154 //WriteLn("TRACE"+_MID+"draw.scalar 1.3 _.Fi("<<Rows(_.Fi)+"x"<<Columns(_.Fi)); 155 Matrix _.W := VMat2Mat(SubRow(fullNoise,_.segment->EquIdx)); 156 //WriteLn("TRACE"+_MID+"draw.scalar 1.4 _.W("<<Rows(_.W)+"x"<<Columns(_.W)); 157 Matrix _.Z := _.W + _.Fi * omega; 158 //WriteLn("TRACE"+_MID+"draw.scalar 1.5 _.Z("<<Rows(_.Z)+"x"<<Columns(_.Z)); 159 Set segment = _.segment; 160 Set aux = _.segment->ArimaAuxInfo; 161 Set arima = _.segment->Arima; 162 Polyn dif = ARIMAGetDIF(arima); 163 // 164 VMatrix si = SubRow(SubCol(sigBlk::_.sigmaInv,_.segment->EquIdx),_.segment->EquIdx); 165 Real d = aux::difDegree; 166 Real p = aux::p; 167 Real q = aux::q; 168 Real m = aux::m; 169 Real n = m - d; 242 170 243 //WriteLn("TRACE"+_MID+"draw.scalar 1.6 d="<<d+" p="<<p+" q="<<q+" m="<<m+" n="<<n); 171 244 //WriteLn("TRACE"+_MID+"draw.scalar 2"); 172 Matrix mrgDraw = _.marginalDraw(0); 245 246 NameBlock ev = get.evaluator(0); 247 Matrix mrgDraw = _.custom.partial.draw(0); 173 248 Set draw.all = For(1, Rows(mrgDraw), Real(Real i) 174 249 { 175 250 Real mrg = MatDat(mrgDraw,i,1); 176 251 If(Not(IsUnknown(mrg)), mrg, { 177 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].1");178 Set bounds = _.filterEval::get.bounds(i,u);179 Code fltEv = _.filterEval::eval;180 Code get.priorLogDens = _.get.priorLogDens;181 Matrix Z = _.Z;252 Real ev::_.i := i; 253 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].3"); 254 Real x = MatDat(ev::_.u,i,1); 255 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].4"); 256 Set bounds = _.filter::get.bounds(i,ev::_.u); 182 257 Real x0 = bounds[1]; 183 258 Real x1 = bounds[2]; 184 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].2");185 //WriteLn("TRACE"+_MID+"draw.scalar.one arimaBlk::enabled=["<<arimaBlk::enabled+"]");186 //WriteLn("TRACE"+_MID+"draw.scalar.one d=["<<d+"]");187 //WriteLn("TRACE"+_MID+"draw.scalar.one p=["<<p+"]");188 //WriteLn("TRACE"+_MID+"draw.scalar.one q=["<<q+"]");189 //WriteLn("TRACE"+_MID+"draw.scalar.one bounds=["<<x0+","<<x1+"]");190 //WriteLn("TRACE"+_MID+"draw.scalar.one omega="<<omega);191 Matrix EvalFilteredNoise(Real x)192 {193 Real old = MatDat(u,i,1);194 Real Do PutMatDat(u,i,1,x);195 Matrix Fi = fltEv(u);196 Real Do PutMatDat(u,i,1,old);197 Matrix fn = VMat2Mat(si*Mat2VMat(Z-Fi*omega));198 fn199 };200 Real LogDens.WhiteNoise(Real x)201 {202 Matrix ei = EvalFilteredNoise(x);203 Real ld = -0.5*MatDat(MtMSqr(ei),1,1);204 //WriteLn("TRACE LogDens.WhiteNoise("+_.colNames[i]+","<<x+")="<<ld);205 ld+get.priorLogDens(i)206 };207 Real LogDens.ARMA(Real x)208 {209 Matrix Zi = EvalFilteredNoise(x);210 VMatrix Zi_ = Mat2VMat(Zi);211 VMatrix ei_ = arimaBlk::filterJustBlock(segment, Zi_);212 Matrix ei = VMat2Mat(ei_);213 Real ld = -0.5*MatDat(MtMSqr(ei),1,1);214 //WriteLn("TRACE LogDens.ARMA("+_.colNames[i]+","<<x+")="<<ld);215 ld+get.priorLogDens(i)216 };217 Real LogDens.ARIMA(Real x)218 {219 Matrix Zi = EvalFilteredNoise(x);220 //WriteLn("TRACE Zi("<<Rows(Zi)+","<<Columns(Zi)+")");221 Matrix Wi = Rand(d,1,0,0) << Sub(DifEq(dif/1,Zi),d+1,1,n,1);222 //WriteLn("TRACE Wi("<<Rows(Wi)+","<<Columns(Wi)+")");223 VMatrix Wi_ = Mat2VMat(Wi);224 VMatrix ei_ = arimaBlk::filterJustBlock(segment, Wi_);225 //WriteLn("TRACE ei_("<<VRows(ei_)+","<<VColumns(ei_)+")");226 Matrix ei = VMat2Mat(Sub(ei_,d+1,1,n,1));227 //WriteLn("TRACE ei("<<Rows(ei)+","<<Columns(ei)+")");228 Real ld = -0.5*MatDat(MtMSqr(ei),1,1);229 //WriteLn("TRACE LogDens.ARIMA("+_.colNames[i]+","<<x+")="<<ld);230 ld+get.priorLogDens(i)231 };232 Code LogDens = Case(233 Or(Not(arimaBlk::enabled),And(Not(p),Not(q))),234 LogDens.WhiteNoise,235 d==0,236 LogDens.ARMA,237 1==1,238 LogDens.ARIMA );239 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].3");240 Real x = MatDat(u,i,1);241 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].4");242 259 Real If(Or(x<x0,x>x1), x := Rand(x0,x1) ); 243 260 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].5"); 244 Real x:= _scalarSampler( LogDens,x0,x1,x);261 Real x:= _scalarSampler(ev::LogDens,x0,x1,x); 245 262 //WriteLn("TRACE draw.scalar x= "<<x); 246 263 //WriteLn("TRACE"+_MID+"draw.scalar.one 2.["+IntText(i)+"].6"); 247 Real PutMatDat( u,i,1,x);264 Real PutMatDat(ev::_.u,i,1,x); 248 265 //WriteLn("TRACE"+_MID+"draw.scalar.one x="<<x); 249 266 x … … 297 314 { 298 315 //WriteLn("TRACE "+_MID+"::setStore 3"); 299 Matrix _.Fi := _.filter Eval::eval(VMat2Mat(_.store));316 Matrix _.Fi := _.filter::eval(VMat2Mat(_.store)); 300 317 //WriteLn("TRACE "+_MID+"::setStore 4"); 301 318 Real ok = If(!_.isInputFilter, 0,
Note: See TracChangeset
for help on using the changeset viewer.
