Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/surveydistribution.cc
[mareframe] / trunk / gadget / surveydistribution.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/surveydistribution.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "surveydistribution.h"
2 :     #include "readfunc.h"
3 :     #include "readword.h"
4 :     #include "readaggregation.h"
5 :     #include "errorhandler.h"
6 :     #include "mathfunc.h"
7 :     #include "areatime.h"
8 :     #include "stock.h"
9 :     #include "suitfuncptrvector.h"
10 :     #include "gadget.h"
11 :     #include "global.h"
12 :    
13 :     SurveyDistribution::SurveyDistribution(CommentStream& infile, const AreaClass* const Area,
14 :     const TimeClass* const TimeInfo, Keeper* const keeper, double weight, const char* name)
15 :     : Likelihood(SURVEYDISTRIBUTIONLIKELIHOOD, weight, name), alptr(0) {
16 :    
17 :     int i, j;
18 :     char text[MaxStrLength];
19 :     strncpy(text, "", MaxStrLength);
20 :     int numarea = 0, numage = 0, numlen = 0;
21 :    
22 :     char datafilename[MaxStrLength];
23 :     char aggfilename[MaxStrLength];
24 :     strncpy(datafilename, "", MaxStrLength);
25 :     strncpy(aggfilename, "", MaxStrLength);
26 :     ifstream datafile;
27 :     CommentStream subdata(datafile);
28 :     readWordAndValue(infile, "datafile", datafilename);
29 :    
30 :     timeindex = 0;
31 :     fittype = new char[MaxStrLength];
32 :     strncpy(fittype, "", MaxStrLength);
33 :     liketype = new char[MaxStrLength];
34 :     strncpy(liketype, "", MaxStrLength);
35 :    
36 :     //read in area aggregation from file
37 :     readWordAndValue(infile, "areaaggfile", aggfilename);
38 :     datafile.open(aggfilename, ios::in);
39 :     handle.checkIfFailure(datafile, aggfilename);
40 :     handle.Open(aggfilename);
41 :     numarea = readAggregation(subdata, areas, areaindex);
42 :     handle.Close();
43 :     datafile.close();
44 :     datafile.clear();
45 :    
46 :     //Must change from outer areas to inner areas.
47 :     for (i = 0; i < areas.Nrow(); i++)
48 :     for (j = 0; j < areas.Ncol(i); j++)
49 :     areas[i][j] = Area->getInnerArea(areas[i][j]);
50 :    
51 :     //read in length aggregation from file
52 :     readWordAndValue(infile, "lenaggfile", aggfilename);
53 :     datafile.open(aggfilename, ios::in);
54 :     handle.checkIfFailure(datafile, aggfilename);
55 :     handle.Open(aggfilename);
56 :     numlen = readLengthAggregation(subdata, lengths, lenindex);
57 :     handle.Close();
58 :     datafile.close();
59 :     datafile.clear();
60 :    
61 :     LgrpDiv = new LengthGroupDivision(lengths);
62 :     if (LgrpDiv->Error())
63 :     handle.logMessage(LOGFAIL, "Error in surveydistribution - failed to create length group");
64 :    
65 :     //read in age aggregation from file
66 :     readWordAndValue(infile, "ageaggfile", aggfilename);
67 :     datafile.open(aggfilename, ios::in);
68 :     handle.checkIfFailure(datafile, aggfilename);
69 :     handle.Open(aggfilename);
70 :     numage = readAggregation(subdata, ages, ageindex);
71 :     handle.Close();
72 :     datafile.close();
73 :     datafile.clear();
74 :    
75 :     //read in the stocknames
76 :     i = 0;
77 :     infile >> text >> ws;
78 :     if (strcasecmp(text, "stocknames") != 0)
79 :     handle.logFileUnexpected(LOGFAIL, "stocknames", text);
80 :     infile >> text;
81 :     while (!infile.eof() && (strcasecmp(text, "fittype") != 0)) {
82 :     infile >> ws;
83 :     stocknames.resize(new char[strlen(text) + 1]);
84 :     strcpy(stocknames[i++], text);
85 :     infile >> text;
86 :     }
87 :     if (stocknames.Size() == 0)
88 :     handle.logFileMessage(LOGFAIL, "\nError in surveydistribution - failed to read stocks");
89 :     handle.logMessage(LOGMESSAGE, "Read stock data - number of stocks", stocknames.Size());
90 :    
91 :     infile >> fittype >> ws;
92 :     fitnumber = 0;
93 :     if (strcasecmp(fittype, "linearfit") == 0) {
94 :     fitnumber = 1;
95 :     } else if (strcasecmp(fittype, "powerfit") == 0) {
96 :     fitnumber = 2;
97 :     } else
98 :     handle.logFileMessage(LOGFAIL, "\nError in surveydistribution - unrecognised fittype", fittype);
99 :    
100 :     parameters.resize(2, keeper);
101 :     infile >> text >> ws;
102 :     if (strcasecmp(text, "parameters") != 0)
103 :     handle.logFileUnexpected(LOGFAIL, "parameters", text);
104 :     parameters.read(infile, TimeInfo, keeper);
105 :    
106 :     suit.resize(LgrpDiv->numLengthGroups(), 0.0);
107 :     infile >> text >> ws;
108 :     if ((strcasecmp(text, "function") == 0)) {
109 :     //read suitability function
110 :     infile >> text >> ws;
111 :     SuitFuncPtrVector tempsuitfunc;
112 :     tempsuitfunc.readSuitFunction(infile, text, TimeInfo, keeper);
113 :     suitfunction = tempsuitfunc[0];
114 :     if (suitfunction->usesPredLength())
115 :     suitfunction->setPredLength(0.0);
116 :    
117 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++) {
118 :     if (suitfunction->usesPreyLength())
119 :     suitfunction->setPreyLength(LgrpDiv->meanLength(i));
120 :    
121 :     suit[i] = suitfunction->calculate();
122 :     }
123 :    
124 :     } else if (strcasecmp(text, "suitfile") == 0) {
125 :     //read values from file
126 :     for (i = 0; i < LgrpDiv->numLengthGroups(); i++)
127 :     infile >> suit[i];
128 :    
129 :     } else {
130 :     handle.logFileMessage(LOGFAIL, "\nError in surveydistribution - unrecognised suitability", text);
131 :     }
132 :    
133 :     //JMB - changed to make the reading of epsilon optional
134 :     infile >> ws;
135 :     char c = infile.peek();
136 :     if ((c == 'e') || (c == 'E'))
137 :     readWordAndVariable(infile, "epsilon", epsilon);
138 :     else
139 :     epsilon = 1.0;
140 :    
141 :     if (epsilon < verysmall) {
142 :     handle.logFileMessage(LOGWARN, "epsilon should be a positive integer - set to default value 1");
143 :     epsilon = 1.0;
144 :     }
145 :    
146 :     readWordAndValue(infile, "likelihoodtype", liketype);
147 :     likenumber = 0;
148 :     if (strcasecmp(liketype, "pearson") == 0)
149 :     likenumber = 1;
150 :     else if (strcasecmp(liketype, "multinomial") == 0)
151 :     likenumber = 2;
152 :     else if (strcasecmp(liketype, "gamma") == 0)
153 :     likenumber = 3;
154 :     else if (strcasecmp(liketype, "log") == 0)
155 :     likenumber = 4;
156 :     else
157 :     handle.logFileMessage(LOGFAIL, "\nError in surveydistribution - unrecognised likelihoodtype", liketype);
158 :    
159 :     //read the survey distribution data from the datafile
160 :     datafile.open(datafilename, ios::in);
161 :     handle.checkIfFailure(datafile, datafilename);
162 :     handle.Open(datafilename);
163 :     readDistributionData(subdata, TimeInfo, numarea, numage, numlen);
164 :     handle.Close();
165 :     datafile.close();
166 :     datafile.clear();
167 :    
168 :     //prepare for next likelihood component
169 :     infile >> ws;
170 :     if (!infile.eof()) {
171 :     infile >> text >> ws;
172 :     if (strcasecmp(text, "[component]") != 0)
173 :     handle.logFileUnexpected(LOGFAIL, "[component]", text);
174 :     }
175 :     }
176 :    
177 :     void SurveyDistribution::readDistributionData(CommentStream& infile,
178 :     const TimeClass* TimeInfo, int numarea, int numage, int numlen) {
179 :    
180 :     double tmpnumber;
181 :     char tmparea[MaxStrLength], tmpage[MaxStrLength], tmplen[MaxStrLength];
182 :     strncpy(tmparea, "", MaxStrLength);
183 :     strncpy(tmpage, "", MaxStrLength);
184 :     strncpy(tmplen, "", MaxStrLength);
185 :     int keepdata, timeid, areaid, ageid, lenid;
186 :     int i, year, step, count, reject;
187 :    
188 :     //Check the number of columns in the inputfile
189 :     if (countColumns(infile) != 6)
190 :     handle.logFileMessage(LOGFAIL, "wrong number of columns in inputfile - should be 6");
191 :    
192 :     year = step = count = reject = 0;
193 :     while (!infile.eof()) {
194 :     keepdata = 1;
195 :     infile >> year >> step >> tmparea >> tmpage >> tmplen >> tmpnumber >> ws;
196 :    
197 :     //crude check to see if something has gone wrong and avoid infinite loops
198 :     if (strlen(tmparea) == 0)
199 :     handle.logFileMessage(LOGFAIL, "failed to read data from file");
200 :    
201 :     //if tmparea is in areaindex keep data, else dont keep the data
202 :     areaid = -1;
203 :     for (i = 0; i < areaindex.Size(); i++)
204 :     if (strcasecmp(areaindex[i], tmparea) == 0)
205 :     areaid = i;
206 :    
207 :     if (areaid == -1)
208 :     keepdata = 0;
209 :    
210 :     //if tmpage is in ageindex keep data, else dont keep the data
211 :     ageid = -1;
212 :     for (i = 0; i < ageindex.Size(); i++)
213 :     if (strcasecmp(ageindex[i], tmpage) == 0)
214 :     ageid = i;
215 :    
216 :     if (ageid == -1)
217 :     keepdata = 0;
218 :    
219 :     //if tmplen is in lenindex keep data, else dont keep the data
220 :     lenid = -1;
221 :     for (i = 0; i < lenindex.Size(); i++)
222 :     if (strcasecmp(lenindex[i], tmplen) == 0)
223 :     lenid = i;
224 :    
225 :     if (lenid == -1)
226 :     keepdata = 0;
227 :    
228 :     //check if the year and step are in the simulation
229 :     timeid = -1;
230 :     if ((TimeInfo->isWithinPeriod(year, step)) && (keepdata == 1)) {
231 :     for (i = 0; i < Years.Size(); i++)
232 :     if ((Years[i] == year) && (Steps[i] == step))
233 :     timeid = i;
234 :    
235 :     if (timeid == -1) {
236 :     Years.resize(1, year);
237 :     Steps.resize(1, step);
238 :     timeid = (Years.Size() - 1);
239 :    
240 :     obsDistribution.resize();
241 :     modelDistribution.resize();
242 :     likelihoodValues.AddRows(1, numarea, 0.0);
243 :     for (i = 0; i < numarea; i++) {
244 :     obsDistribution[timeid].resize(new DoubleMatrix(numage, numlen, 0.0));
245 :     modelDistribution[timeid].resize(new DoubleMatrix(numage, numlen, 0.0));
246 :     }
247 :     }
248 :    
249 :     } else
250 :     keepdata = 0;
251 :    
252 :    
253 :     if (keepdata == 1) {
254 :     //survey distribution data is required, so store it
255 :     count++;
256 :     (*obsDistribution[timeid][areaid])[ageid][lenid] = tmpnumber;
257 :     } else
258 :     reject++; //count number of rejected data points read from file
259 :     }
260 :    
261 :     AAT.addActions(Years, Steps, TimeInfo);
262 :     if (count == 0)
263 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - found no data in the data file for", this->getName());
264 :    
265 :     if (Steps.Size() > 0) {
266 :     //JMB - to be comparable, this should only take place on the same step in each year
267 :     step = Steps[0];
268 :     timeid = 0;
269 :     for (i = 1; i < Steps.Size(); i++)
270 :     if (Steps[i] != step)
271 :     timeid++;
272 :    
273 :     if (timeid != 0)
274 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - differing timesteps for", this->getName());
275 :     }
276 :    
277 :     if (reject != 0)
278 :     handle.logMessage(LOGMESSAGE, "Discarded invalid surveydistribution data - number of invalid entries", reject);
279 :     handle.logMessage(LOGMESSAGE, "Read surveydistribution data file - number of entries", count);
280 :     }
281 :    
282 :     SurveyDistribution::~SurveyDistribution() {
283 :     int i, j;
284 :     for (i = 0; i < stocknames.Size(); i++)
285 :     delete[] stocknames[i];
286 :     for (i = 0; i < areaindex.Size(); i++)
287 :     delete[] areaindex[i];
288 :     for (i = 0; i < ageindex.Size(); i++)
289 :     delete[] ageindex[i];
290 :     for (i = 0; i < lenindex.Size(); i++)
291 :     delete[] lenindex[i];
292 :    
293 :     if (suitfunction != NULL) {
294 :     delete suitfunction;
295 :     suitfunction = NULL;
296 :     }
297 :     for (i = 0; i < obsDistribution.Nrow(); i++) {
298 :     for (j = 0; j < obsDistribution.Ncol(i); j++) {
299 :     delete obsDistribution[i][j];
300 :     delete modelDistribution[i][j];
301 :     }
302 :     }
303 :    
304 :     if (aggregator != 0)
305 :     delete aggregator;
306 :     delete LgrpDiv;
307 :     delete[] fittype;
308 :     delete[] liketype;
309 :     }
310 :    
311 :     void SurveyDistribution::Reset(const Keeper* const keeper) {
312 :     Likelihood::Reset(keeper);
313 :     if (isZero(weight))
314 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - zero weight for", this->getName());
315 :     int i, j;
316 :     for (i = 0; i < modelDistribution.Nrow(); i++)
317 :     for (j = 0; j < modelDistribution.Ncol(i); j++)
318 :     (*modelDistribution[i][j]).setToZero();
319 :     if (handle.getLogLevel() >= LOGMESSAGE)
320 :     handle.logMessage(LOGMESSAGE, "Reset surveydistribution component", this->getName());
321 :     }
322 :    
323 :     void SurveyDistribution::Print(ofstream& outfile) const {
324 :     int i;
325 :     outfile << "\nSurvey Distribution " << this->getName() << " - likelihood value " << likelihood
326 :     << "\n\tFunction " << liketype << "\n\tStock names:";
327 :     for (i = 0; i < stocknames.Size(); i++)
328 :     outfile << sep << stocknames[i];
329 :     outfile << endl;
330 :     aggregator->Print(outfile);
331 :     outfile.flush();
332 :     }
333 :    
334 :     void SurveyDistribution::printLikelihood(ofstream& outfile, const TimeClass* const TimeInfo) {
335 :    
336 :     if (!AAT.atCurrentTime(TimeInfo))
337 :     return;
338 :    
339 :     int i, area, age, len;
340 :     timeindex = -1;
341 :     for (i = 0; i < Years.Size(); i++)
342 :     if ((Years[i] == TimeInfo->getYear()) && (Steps[i] == TimeInfo->getStep()))
343 :     timeindex = i;
344 :     if (timeindex == -1)
345 :     handle.logMessage(LOGFAIL, "Error in surveydistribution - invalid timestep");
346 :    
347 :     for (area = 0; area < modelDistribution.Ncol(timeindex); area++) {
348 :     for (age = 0; age < modelDistribution[timeindex][area]->Nrow(); age++) {
349 :     for (len = 0; len < modelDistribution[timeindex][area]->Ncol(age); len++) {
350 :     outfile << setw(lowwidth) << Years[timeindex] << sep << setw(lowwidth)
351 :     << Steps[timeindex] << sep << setw(printwidth) << areaindex[area] << sep
352 :     << setw(printwidth) << ageindex[age] << sep << setw(printwidth)
353 :     << lenindex[len] << sep << setprecision(largeprecision) << setw(largewidth);
354 :    
355 :     //JMB crude filter to remove the 'silly' values from the output
356 :     if ((*modelDistribution[timeindex][area])[age][len] < rathersmall)
357 :     outfile << 0 << endl;
358 :     else
359 :     outfile << (*modelDistribution[timeindex][area])[age][len] << endl;
360 :     }
361 :     }
362 :     }
363 :     }
364 :    
365 :     void SurveyDistribution::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
366 :    
367 :     int i, j, k, found, minage, maxage;
368 :     StockPtrVector stocks;
369 :    
370 :     for (i = 0; i < stocknames.Size(); i++) {
371 :     found = 0;
372 :     for (j = 0; j < Stocks.Size(); j++) {
373 :     if (strcasecmp(stocknames[i], Stocks[j]->getName()) == 0) {
374 :     found++;
375 :     stocks.resize(Stocks[j]);
376 :     }
377 :     }
378 :     if (found == 0)
379 :     handle.logMessage(LOGFAIL, "Error in surveydistribution - failed to match stock", stocknames[i]);
380 :     }
381 :    
382 :     for (i = 0; i < stocks.Size(); i++)
383 :     for (j = 0; j < stocks.Size(); j++)
384 :     if ((strcasecmp(stocks[i]->getName(), stocks[j]->getName()) == 0) && (i != j))
385 :     handle.logMessage(LOGFAIL, "Error in surveydistribution - repeated stock", stocks[i]->getName());
386 :    
387 :     //check areas, ages and lengths
388 :     if (handle.getLogLevel() >= LOGWARN) {
389 :     for (j = 0; j < areas.Nrow(); j++) {
390 :     found = 0;
391 :     for (i = 0; i < stocks.Size(); i++)
392 :     for (k = 0; k < areas.Ncol(j); k++)
393 :     if (stocks[i]->isInArea(areas[j][k]))
394 :     found++;
395 :     if (found == 0)
396 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - stock not defined on all areas");
397 :     }
398 :    
399 :     minage = 9999;
400 :     maxage = 0;
401 :     for (i = 0; i < ages.Nrow(); i++) {
402 :     for (j = 0; j < ages.Ncol(i); j++) {
403 :     minage = min(ages[i][j], minage);
404 :     maxage = max(ages[i][j], maxage);
405 :     }
406 :     }
407 :    
408 :     found = 0;
409 :     for (i = 0; i < stocks.Size(); i++)
410 :     if (minage >= stocks[i]->minAge())
411 :     found++;
412 :     if (found == 0)
413 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - minimum age less than stock age");
414 :    
415 :     found = 0;
416 :     for (i = 0; i < stocks.Size(); i++)
417 :     if (maxage <= stocks[i]->maxAge())
418 :     found++;
419 :     if (found == 0)
420 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - maximum age greater than stock age");
421 :    
422 :     found = 0;
423 :     for (i = 0; i < stocks.Size(); i++)
424 :     if (LgrpDiv->maxLength(0) > stocks[i]->getLengthGroupDiv()->minLength())
425 :     found++;
426 :     if (found == 0)
427 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - minimum length group less than stock length");
428 :    
429 :     found = 0;
430 :     for (i = 0; i < stocks.Size(); i++)
431 :     if (LgrpDiv->minLength(LgrpDiv->numLengthGroups()) < stocks[i]->getLengthGroupDiv()->maxLength())
432 :     found++;
433 :     if (found == 0)
434 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - maximum length group greater than stock length");
435 :     }
436 :    
437 :     aggregator = new StockAggregator(stocks, LgrpDiv, areas, ages);
438 :     }
439 :    
440 :     void SurveyDistribution::calcIndex(const TimeClass* const TimeInfo) {
441 :     //written by kgf 13/10 98
442 :    
443 :     int area, age, len;
444 :     if (suitfunction != NULL) {
445 :     suitfunction->updateConstants(TimeInfo);
446 :     if ((timeindex == 0) || (suitfunction->didChange(TimeInfo))) {
447 :     if (suitfunction->usesPredLength())
448 :     suitfunction->setPredLength(0.0);
449 :    
450 :     for (len = 0; len < LgrpDiv->numLengthGroups(); len++) {
451 :     if (suitfunction->usesPreyLength())
452 :     suitfunction->setPreyLength(LgrpDiv->meanLength(len));
453 :    
454 :     suit[len] = suitfunction->calculate();
455 :     }
456 :     }
457 :     }
458 :    
459 :     parameters.Update(TimeInfo);
460 :     switch (fitnumber) {
461 :     case 1:
462 :     for (area = 0; area < areas.Nrow(); area++)
463 :     for (age = (*alptr)[area].minAge(); age <= (*alptr)[area].maxAge(); age++)
464 :     for (len = (*alptr)[area].minLength(age); len < (*alptr)[area].maxLength(age); len++)
465 :     (*modelDistribution[timeindex][area])[age][len] = parameters[0] * suit[len] * (((*alptr)[area][age][len]).N + parameters[1]);
466 :     break;
467 :     case 2:
468 :     for (area = 0; area < areas.Nrow(); area++)
469 :     for (age = (*alptr)[area].minAge(); age <= (*alptr)[area].maxAge(); age++)
470 :     for (len = (*alptr)[area].minLength(age); len < (*alptr)[area].maxLength(age); len++)
471 :     (*modelDistribution[timeindex][area])[age][len] = parameters[0] * suit[len] * pow(((*alptr)[area][age][len]).N, parameters[1]);
472 :     break;
473 :     default:
474 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - unrecognised fittype", fittype);
475 :     break;
476 :     }
477 :     }
478 :    
479 :     void SurveyDistribution::addLikelihood(const TimeClass* const TimeInfo) {
480 :    
481 :     if ((!(AAT.atCurrentTime(TimeInfo))) || (isZero(weight)))
482 :     return;
483 :    
484 :     int i;
485 :     timeindex = -1;
486 :     for (i = 0; i < Years.Size(); i++)
487 :     if ((Years[i] == TimeInfo->getYear()) && (Steps[i] == TimeInfo->getStep()))
488 :     timeindex = i;
489 :     if (timeindex == -1)
490 :     handle.logMessage(LOGFAIL, "Error in surveydistribution - invalid timestep");
491 :    
492 :     double l = 0.0;
493 :     aggregator->Sum();
494 :     if (handle.getLogLevel() >= LOGMESSAGE)
495 :     handle.logMessage(LOGMESSAGE, "Calculating likelihood score for surveydistribution component", this->getName());
496 :    
497 :     alptr = &aggregator->getSum();
498 :     this->calcIndex(TimeInfo);
499 :     switch (likenumber) {
500 :     case 1:
501 :     l = calcLikPearson();
502 :     break;
503 :     case 2:
504 :     l = calcLikMultinomial();
505 :     break;
506 :     case 3:
507 :     l = calcLikGamma();
508 :     break;
509 :     case 4:
510 :     l = calcLikLog();
511 :     break;
512 :     default:
513 :     handle.logMessage(LOGWARN, "Warning in surveydistribution - unrecognised likelihoodtype", liketype);
514 :     break;
515 :     }
516 :    
517 :     if (handle.getLogLevel() >= LOGMESSAGE)
518 :     handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
519 :     likelihood += l;
520 :     }
521 :    
522 :     double SurveyDistribution::calcLikMultinomial() {
523 :     //written by kgf 30/10 98
524 :     //Multinomial formula from H J Skaug
525 :    
526 :     double temp, total, obstotal, modtotal;
527 :     int area, age, len;
528 :    
529 :     total = 0.0;
530 :     for (area = 0; area < areas.Nrow(); area++) {
531 :     temp = 0.0;
532 :     obstotal = 0.0;
533 :     modtotal = 0.0;
534 :     for (age = 0; age < (*obsDistribution[timeindex][area]).Nrow(); age++) {
535 :     for (len = 0; len < (*obsDistribution[timeindex][area]).Ncol(age); len++) {
536 :     temp -= (*obsDistribution[timeindex][area])[age][len] *
537 :     log(((*modelDistribution[timeindex][area])[age][len]) + epsilon);
538 :     obstotal += (*obsDistribution[timeindex][area])[age][len];
539 :     modtotal += ((*modelDistribution[timeindex][area])[age][len] + epsilon);
540 :     }
541 :     }
542 :    
543 :     if ((modtotal < verysmall) && (!(isZero(obstotal)))) {
544 :     likelihoodValues[timeindex][area] = 0.0;
545 :     } else {
546 :     temp /= obstotal;
547 :     temp += log(modtotal);
548 :     likelihoodValues[timeindex][area] = temp;
549 :     }
550 :     total += likelihoodValues[timeindex][area];
551 :     }
552 :     return total;
553 :     }
554 :    
555 :     double SurveyDistribution::calcLikPearson() {
556 :     //written by kgf 13/10 98
557 :    
558 :     double temp, total, diff;
559 :     int area, age, len;
560 :    
561 :     total = 0.0;
562 :     for (area = 0; area < areas.Nrow(); area++) {
563 :     temp = 0.0;
564 :     for (age = 0; age < (*obsDistribution[timeindex][area]).Nrow(); age++) {
565 :     for (len = 0; len < (*obsDistribution[timeindex][area]).Ncol(age); len++) {
566 :     diff = ((*modelDistribution[timeindex][area])[age][len] - (*obsDistribution[timeindex][area])[age][len]);
567 :     diff *= diff;
568 :     diff /= ((*modelDistribution[timeindex][area])[age][len] + epsilon);
569 :     temp += diff;
570 :     }
571 :     }
572 :     likelihoodValues[timeindex][area] = temp;
573 :     total += likelihoodValues[timeindex][area];
574 :     }
575 :     return total;
576 :     }
577 :    
578 :     double SurveyDistribution::calcLikGamma() {
579 :     //written by kgf 24/5 00
580 :     //The gamma likelihood function as described by
581 :     //Hans J Skaug 15/3 00, at present without internal weighting.
582 :    
583 :     double total, temp;
584 :     int area, age, len;
585 :    
586 :     total = 0.0;
587 :     for (area = 0; area < areas.Nrow(); area++) {
588 :     temp = 0.0;
589 :     for (age = 0; age < (*obsDistribution[timeindex][area]).Nrow(); age++)
590 :     for (len = 0; len < (*obsDistribution[timeindex][area]).Ncol(age); len++)
591 :     temp += (((*obsDistribution[timeindex][area])[age][len] /
592 :     ((*modelDistribution[timeindex][area])[age][len] + epsilon)) +
593 :     log((*modelDistribution[timeindex][area])[age][len] + epsilon));
594 :    
595 :     likelihoodValues[timeindex][area] = temp;
596 :     total += likelihoodValues[timeindex][area];
597 :     }
598 :     return total;
599 :     }
600 :    
601 :     double SurveyDistribution::calcLikLog() {
602 :     //corrected by kgf 27/11 98 to sum first and then take the logarithm
603 :     double total, obstotal, modtotal;
604 :     int area, age, len;
605 :    
606 :     total = 0.0;
607 :     for (area = 0; area < areas.Nrow(); area++) {
608 :     obstotal = 0.0;
609 :     modtotal = 0.0;
610 :     for (age = 0; age < (*obsDistribution[timeindex][area]).Nrow(); age++) {
611 :     for (len = 0; len < (*obsDistribution[timeindex][area]).Ncol(age); len++) {
612 :     modtotal += (*modelDistribution[timeindex][area])[age][len];
613 :     obstotal += (*obsDistribution[timeindex][area])[age][len];
614 :     }
615 :     }
616 :    
617 :     if (!(isZero(modtotal)))
618 :     likelihoodValues[timeindex][area] = (log(obstotal / modtotal) * log(obstotal / modtotal));
619 :     else
620 :     likelihoodValues[timeindex][area] = verybig;
621 :    
622 :     total += likelihoodValues[timeindex][area];
623 :     }
624 :     return total;
625 :     }
626 :    
627 :     void SurveyDistribution::printSummary(ofstream& outfile) {
628 :     int year, area;
629 :    
630 :     for (year = 0; year < likelihoodValues.Nrow(); year++) {
631 :     for (area = 0; area < likelihoodValues.Ncol(year); area++) {
632 :     outfile << setw(lowwidth) << Years[year] << sep << setw(lowwidth)
633 :     << Steps[year] << sep << setw(printwidth) << areaindex[area] << sep
634 :     << setw(largewidth) << this->getName() << sep << setprecision(smallprecision)
635 :     << setw(smallwidth) << weight << sep << setprecision(largeprecision)
636 :     << setw(largewidth) << likelihoodValues[year][area] << endl;
637 :     }
638 :     }
639 :     outfile.flush();
640 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge