Changeset 59bbd54 in tol
- Timestamp:
- Jan 22, 2009, 10:15:44 AM (18 years ago)
- Branches:
- 1941-docker-image-to-test, devel, master
- Children:
- af86fea3
- Parents:
- 98e52e6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tol/stdlib/math/stat/models/bayesian/bysMcmc/bsr/_oneNode.tol
r98e52e6 r59bbd54 13 13 Text _.autodoc.description = 14 14 "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 }; 15 38 16 39 Text _.autodoc.member.Estim = … … 26 49 " Anything _.Y; //Output data (Matrix or VMatrix)\n" 27 50 " Anything _.X; //Input data (Matrix or VMatrix)\n" 28 " Real _.sigma; //Sigma value or ? tosimulate it\n"51 " Real _.sigma; //Sigma prior value, or ? to freely simulate it\n" 29 52 "//Optional members \n" 30 53 " Set _.orderRelation; //Set of Bsr.OrderRelation.Info\n" 31 54 " Set _.arima; //Set of ARIMAStruct\n" 32 " Set _.sigmaPrior; //Bsr.Sigma.Prior.InverseGamma\n"55 " Real _.sigmaPriorWeight; //Weight for Bsr.Sigma.Prior.InverseGamma\n" 33 56 " Set _.timeInfo; //Set of BSR.NoiseTimeInfo\n" 34 57 "]];\n"; … … 58 81 VMatrix prior.Y = Rand(0,1,0,0); 59 82 Matrix prior.X.triplet = Rand(0,3,0,0); 60 83 Set prior.index = Copy(Empty); 61 84 //Used to collect domain information if exists 62 85 Real domain.num = 0; … … 73 96 Real prior.Average = If(ObjectExist("Real","pi->Prior.Average"), 74 97 pi->Prior.Average, ?); 98 Real estimSigma = IsUnknown(pi->Prior.Sigma) | 99 !IsUnknown(pi->Prior.SigmaWeight); 75 100 Real If(!IsUnknown(prior.Average), 76 101 { 102 Set Append(prior.index,[[j]]); 77 103 //Collecting prior information 78 104 Real prior.num := prior.num+1; 105 Real factor = If(estimSigma,1,1/pi->Prior.Sigma); 79 106 VMatrix prior.Y := prior.Y << 80 Mat2VMat(Row(pi->Prior.Average /pi->Prior.Sigma));107 Mat2VMat(Row(pi->Prior.Average*factor)); 81 108 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); 83 110 0 84 111 }); … … 111 138 //Checks unique name and prepares for hashed searching by name 112 139 Real SetIndexByName(linearBlock); 113 114 140 //WriteLn(_MID+" TRACE 5"); 115 141 //Domain constraints 116 142 VMatrix domain.A = Triplet(domain.A.triplet,domain.num,n); 117 118 143 //Building order relations if exist 119 144 Set orderRelation = If(ObjectExist("Set","data::_.orderRelation"), … … 127 152 Set rel = orderRelation[k]; 128 153 Real lower = FindIndexByName(linearBlock, rel->Lower); 154 If(!lower,WriteLn("Cannot create order relation. " 155 "Variable "+rel->Lower+" was not found","E")); 129 156 Real upper = FindIndexByName(linearBlock, rel->Upper); 157 If(!upper,WriteLn("Cannot create order relation. " 158 "Variable "+rel->Upper+" was not found","E")); 130 159 Row(k,lower,+1) << 131 160 Row(k,upper,-1) … … 139 168 //Observational information 140 169 Real sigmaIsUnknown = IsUnknown(data::_.sigma); 141 Set noiseObs = [[170 Set noiseObs = {[[ 142 171 BSR.NoiseDistrib( 143 172 Text Name = data::_.segmentName+"::Noise"; … … 156 185 data::_.timeInfo, Copy(Empty)) 157 186 ) 158 ]]; 187 ]]}; 188 159 189 //WriteLn(_MID+" TRACE 7"); 160 190 //Prior information 161 191 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( 166 211 Text Name = data::_.segmentName+".Prior::Noise"; 167 212 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, 170 216 Set arima = Copy(Empty); 171 217 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); 176 222 Set TimeInfo = Copy(Empty) 177 223 ) 178 ]]}); 224 }); 225 179 226 //WriteLn(_MID+" TRACE 8"); 180 227 //All allowed segments in this simplified API are obs and prior (optionally)
Note: See TracChangeset
for help on using the changeset viewer.
