Changeset 59bbd54 in tol


Ignore:
Timestamp:
Jan 22, 2009, 10:15:44 AM (18 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
af86fea3
Parents:
98e52e6
Message:

Allowing prior over sigma

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

File:
1 edited

Legend:

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

    r98e52e6 r59bbd54  
    1313  Text _.autodoc.description =
    1414  "Estimation methods for simplified BSR models with just one node.";
     15
     16  //Tool function to reduce registers length in _.linearParamInfo
     17  Set LPI(
     18    Text Name,              // Unique identidier of the parameter
     19    Real InitValue,         // Given by user initial value or ?
     20    Real Prior.LowerBound,  // Minimum feasible value or -1/0
     21    Real Prior.UpperBound,  // Maximum feasible value or +1/0
     22    Real Prior.Average,     // Normal prior average
     23    Real Prior.Sigma,       // Normal prior standard deviation
     24    Real Prior.SigmaWeight)       // Normal prior weight
     25  {
     26    Bsr.OneNode.LinearParamInfo(
     27      Text Name,             
     28      Real InitValue,         
     29      Real Prior.LowerBound, 
     30      Real Prior.UpperBound, 
     31      Real Prior.Average,
     32      Real Prior.Sigma,
     33      Real Prior.SigmaWeight,
     34      Real Prior.Missing.SigmaFactor=?, //Already not implemented
     35      Real Prior.Missing.LowerBound=?,  //Already not implemented
     36      Real Prior.Missing.UpperBound=?)  //Already not implemented
     37  };
    1538
    1639  Text _.autodoc.member.Estim =
    … …  
    2649  "  Anything _.Y;          //Output data (Matrix or VMatrix)\n"
    2750  "  Anything _.X;          //Input data (Matrix or VMatrix)\n"
    28   "  Real _.sigma;          //Sigma value or ? to simulate it\n"
     51  "  Real _.sigma;          //Sigma prior value, or ? to freely simulate it\n"
    2952  "//Optional members \n"
    3053  "  Set _.orderRelation;   //Set of Bsr.OrderRelation.Info\n"
    3154  "  Set _.arima;           //Set of ARIMAStruct\n"
    32   "  Set _.sigmaPrior;      //Bsr.Sigma.Prior.InverseGamma\n"
     55  "  Real _.sigmaPriorWeight;  //Weight for Bsr.Sigma.Prior.InverseGamma\n"
    3356  "  Set _.timeInfo;        //Set of BSR.NoiseTimeInfo\n"
    3457  "]];\n";
    … …  
    5881    VMatrix prior.Y = Rand(0,1,0,0);
    5982    Matrix prior.X.triplet = Rand(0,3,0,0);
    60 
     83    Set prior.index = Copy(Empty);
    6184    //Used to collect domain information if exists
    6285    Real domain.num = 0;
    … …  
    7396      Real prior.Average = If(ObjectExist("Real","pi->Prior.Average"),
    7497                              pi->Prior.Average, ?);
     98      Real estimSigma = IsUnknown(pi->Prior.Sigma) |
     99                       !IsUnknown(pi->Prior.SigmaWeight);
    75100      Real If(!IsUnknown(prior.Average),
    76101      {
     102        Set Append(prior.index,[[j]]);
    77103        //Collecting prior information
    78104        Real prior.num := prior.num+1;
     105        Real factor = If(estimSigma,1,1/pi->Prior.Sigma);
    79106        VMatrix prior.Y := prior.Y <<
    80           Mat2VMat(Row(pi->Prior.Average/pi->Prior.Sigma));
     107          Mat2VMat(Row(pi->Prior.Average*factor));
    81108        Matrix prior.X.triplet :=
    82           prior.X.triplet << Row(prior.num,j,1/pi->Prior.Sigma);
     109          prior.X.triplet << Row(prior.num,j,factor);
    83110        0
    84111      });
    … …  
    111138    //Checks unique name and prepares for hashed searching by name
    112139    Real SetIndexByName(linearBlock);
    113 
    114140  //WriteLn(_MID+" TRACE 5");
    115141    //Domain constraints
    116142    VMatrix domain.A = Triplet(domain.A.triplet,domain.num,n);
    117 
    118143    //Building order relations if exist
    119144    Set orderRelation = If(ObjectExist("Set","data::_.orderRelation"),
    … …  
    127152        Set rel = orderRelation[k];
    128153        Real lower = FindIndexByName(linearBlock, rel->Lower);
     154        If(!lower,WriteLn("Cannot create order relation. "
     155                          "Variable "+rel->Lower+" was not found","E"));
    129156        Real upper = FindIndexByName(linearBlock, rel->Upper);
     157        If(!upper,WriteLn("Cannot create order relation. "
     158                          "Variable "+rel->Upper+" was not found","E"));
    130159        Row(k,lower,+1) <<
    131160        Row(k,upper,-1)
    … …  
    139168    //Observational information
    140169    Real sigmaIsUnknown = IsUnknown(data::_.sigma);
    141     Set noiseObs = [[
     170    Set noiseObs = {[[
    142171      BSR.NoiseDistrib(
    143172        Text Name = data::_.segmentName+"::Noise";
    … …  
    156185          data::_.timeInfo, Copy(Empty))
    157186      )
    158     ]];
     187    ]]};
     188
    159189  //WriteLn(_MID+" TRACE 7");
    160190    //Prior information
    161191    VMatrix prior.X = Triplet(prior.X.triplet,prior.num,n);
    162     Set noisePrior = If(!prior.num,Copy(Empty),
    163     {
    164       [[
    165       BSR.NoiseDistrib(
     192    Real sigmaIndex = If(sigmaIsUnknown,1,0);
     193    Set noisePrior = For(1,Card(prior.index),Set(Real j)
     194    {
     195      Set pi = (data::_.linearParamInfo)[prior.index[j] ];
     196      Real estimSigma = IsUnknown(pi->Prior.Sigma) |
     197                       !IsUnknown(pi->Prior.SigmaWeight);
     198      Real sigmaIndex := sigmaIndex + estimSigma;
     199
     200      Set SigmaPrior = If(IsUnknown(pi->Prior.SigmaWeight),Copy(Empty),
     201        Bsr.Sigma.Prior.InverseGamma(pi->Prior.Sigma,pi->Prior.SigmaWeight));
     202      Real cov.value = If(estimSigma, 1, pi->Prior.Sigma);
     203      Real L.value   = If(estimSigma, 1, Sqrt(pi->Prior.Sigma));
     204      Real Li.value  = If(estimSigma, 1, 1/Sqrt(pi->Prior.Sigma));
     205      VMatrix z1 = Rand(m+j-1,1,0,0);
     206      VMatrix z2 = Rand(m.full-(j+m),1,0,0);
     207      VMatrix cov.diag = Tra(z1<<Mat2VMat(Col(cov.value))<<z2);
     208      VMatrix L.diag   = Tra(z1<<Mat2VMat(Col(L.value  ))<<z2);
     209      VMatrix Li.diag  = Tra(z1<<Mat2VMat(Col(Li.value ))<<z2);
     210      Set BSR.NoiseDistrib(
    166211        Text Name = data::_.segmentName+".Prior::Noise";
    167212        Real Nu = 0;
    168         Text SigmaName = "";
    169         Real SigmaIndex = -1;
     213        Text SigmaName = If(!estimSigma,"","Pri."+pi->Name+"::Sigma");
     214        Real SigmaIndex = Copy(sigmaIndex);
     215        Set SigmaPrior,
    170216        Set arima = Copy(Empty);
    171217        Set ArimaAuxInfo = Copy(Empty);
    172         VMatrix Cov = Eye(m.full,m.full,0,Rand(m,1,0,0)<<Rand(prior.num,1,1,1));
    173         VMatrix L = Cov;
    174         VMatrix Li = Cov;
    175         Set EquIdx = Range(m+1,m+prior.num,1);
     218        VMatrix Cov = Eye(m.full,m.full,0,cov.diag);
     219        VMatrix L   = Eye(m.full,m.full,0,L.diag);
     220        VMatrix Li  = Eye(m.full,m.full,0,Li.diag);
     221        Set EquIdx = SetOfReal(m+j);
    176222        Set TimeInfo = Copy(Empty)
    177223      )
    178     ]]});
     224   });
     225
    179226  //WriteLn(_MID+" TRACE 8");
    180227    //All allowed segments in this simplified API are obs and prior (optionally)
Note: See TracChangeset for help on using the changeset viewer.