-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMM.C
747 lines (631 loc) · 19.7 KB
/
IMM.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/****************************************************************
IMM.C
Copyright (C)2013 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include "IMM.H"
#include <iostream>
#include <fstream>
#include <math.h>
#include "BOOM/ProteinTrans.H"
#include "BOOM/Constants.H"
#include "BOOM/Alphabet.H"
#include "BOOM/Stacktrace.H"
#include "BOOM/Chi2FitTest.H"
#include "NthOrderStringIterator.H"
#include "MarkovChainCompiler.H"
extern Alphabet alphabet;
inline int hashTableSize(int order)
{
// these are all primes
switch(order)
{
case 0: return 11; break;
case 1: return 29; break;
case 2: return 127; break;
case 3: return 619; break;
default:
case 4: return 3121; break;
}
}
IMM::IMM(const IMM &other)
: N(other.N), phase(other.phase), alphabetSize(other.alphabetSize),
revComp(NULL), models(new BOOM::Vector<BOOM::StringMap<double>*>)
{
for(int i=0 ; i<=N ; ++i)
models->push_back(new BOOM::StringMap<double>(*(*other.models)[i]));
setContentType(other.getContentType());
setStrand(other.getStrand());
if(getContentType()==INTERGENIC)
revComp=this;
else if(other.revComp && getStrand()==FORWARD_STRAND)
revComp=new IMM(*other.revComp);
}
IMM::IMM(BOOM::Vector<TrainingSequence*> &v,int order,
int minSampleSize,int phase,ContentType contentType,
Strand strand)
: N(order),
alphabetSize(alphabet.getNumElements()),
phase(phase),
revComp(NULL),
models(new BOOM::Vector<BOOM::StringMap<double>*>)
{
setContentType(contentType);
if(strand==EITHER_STRAND) strand=::getStrand(contentType);
setStrand(strand);
buildModels(v,minSampleSize);
if(strand==FORWARD_STRAND)
{
BOOM::Vector<TrainingSequence*> rcSeqs;
revCompSeqs(v,rcSeqs);
revComp=new IMM(rcSeqs,order,minSampleSize,phase,
::reverseComplement(contentType),
REVERSE_STRAND);
revComp->revComp=this;
}
}
IMM::IMM(istream &is,Strand strand)
: revComp(NULL), models(new BOOM::Vector<BOOM::StringMap<double>*>)
{
setStrand(strand);
load(is);
}
IMM::IMM(const BOOM::String &filename)
: revComp(NULL), models(new BOOM::Vector<BOOM::StringMap<double>*>)
{
ifstream is(filename.c_str());
if(!is.good()) throw BOOM::String("Error opening file ")+filename
+" in IMM::IMM()";
BOOM::String modelType;
is >> modelType;
if(modelType!="IMM")
throw BOOM::String("Attempt to load an object of type ")+modelType+
" into an IMM";
load(is);
}
IMM::~IMM()
{
int n=models->size();
for(int i=0 ; i<n ; ++i)
delete (*models)[i];
delete models;
/* ### THIS IS BAD! THE REVCOMP WILL BE DELETED MULTIPLE TIMES...
(UNLESS FORWARD_STRAND_ONLY IS DEFINED)
if(getStrand()==FORWARD_STRAND && revComp!=this)
delete revComp;
*/
}
double IMM::scoreSubsequence(const Sequence &seq,const BOOM::String &str,
int begin,int length,int)
{
// This is slow -- please use FastIMM if you care about speed...
Symbol dummySymbol;
char dummyChar;
double score=0;
int end=begin+length;
for(int pos=begin ; pos<end ; ++pos)
score+=scoreSingleBase(seq,str,pos,dummySymbol,dummyChar);
return score;
}
double IMM::scoreSingleBase(const Sequence &seq,const BOOM::String &str,
int index,Symbol s,char c)
{
const char *p=str.c_str();
switch(getStrand())
{
case PLUS_STRAND:
{
int maxOrder=(index>N ? N : index);
for(int order=maxOrder ; order>=0 ; --order)
{
BOOM::StringMap<double> &model=*(*models)[order];
if(model.isDefined(p,index-order,order+1))
return model.lookup(p,index-order,order+1);
}
return 0.0; // usually caused by N's; marginalizing gives 0.0
/*throw BOOM::String("IMM::scoreSingleBase('+',")+
index+",strlen="+strlen(p)+",str="+
str.substring(index,maxOrder)+")";*/
}
case MINUS_STRAND:
{
/*
On the minus strand we have to take our contexts from the
right (but only because we trained the model that way)
*/
int seqLen=str.length();
int maxOrder=seqLen-index-1;
if(maxOrder>N) maxOrder=N;
for(int order=maxOrder ; order>=0 ; --order)
{
BOOM::StringMap<double> &model=*(*models)[order];
if(model.isDefined(p,index,order+1))
return model.lookup(p,index,order+1);
}
return 0.0;
/*throw BOOM::Stacktrace(
BOOM::String("IMM::scoreSingleBase('-',")+
index+",strlen="+strlen(p)+",str="+
str.substring(index,maxOrder)+")");*/
}
default: throw BOOM::String(__FILE__)+__LINE__;
}
}
void IMM::scoreSingleBase(const Sequence &seq,const BOOM::String &str,
int index,Symbol s,char c,
double &scorePhase0,double &scorePhase1,
double &scorePhase2)
{
// I'm not a 3-periodic markov chain -- I'm just a plain, ordinary,
// homogeneous markov chain:
scorePhase0=scorePhase1=scorePhase2=scoreSingleBase(seq,str,index,s,c);
}
ContentSensor *IMM::reverseComplement()
{
return revComp;
}
bool IMM::save(const BOOM::String &filename)
{
ofstream os(filename.c_str());
if(!os.good()) throw BOOM::String("Error creating file ")+filename+
"in IMM::save()";
return save(os);
}
bool IMM::save(ostream &os)
{
os.precision(8);
os << "IMM" << endl;
os << getContentType() << endl;
os << N << "\t" << phase << "\t" << endl;
int numModels=models->size();
os << numModels << endl;
for(int i=0 ; i<numModels ; ++i)
{
BOOM::StringMap<double> &model=*(*models)[i];
os << model.size() << endl;
BOOM::StringMap<double>::iterator cur=model.begin(), end=model.end();
for(; cur!=end ; ++cur)
os << (*cur).first << endl << (*cur).second << endl;
}
if(getStrand()==FORWARD_STRAND) revComp->save(os);
return true;
}
void IMM::load(istream &is)
{
int numModels, numElements;
BOOM::String str, pStr;
ContentType contentType;
is >> contentType >> N >> phase >> numModels;
setContentType(contentType);
setStrand(::getStrand(contentType)); // ### 9/17/2015
for(int i=0 ; i<numModels ; ++i) {
models->push_back(new BOOM::StringMap<double>(hashTableSize(N)));
BOOM::StringMap<double> &model=*(*models)[i];
is >> numElements;
for(int j=0 ; j<numElements ; ++j) {
is >> str >> pStr;
double p=pStr.asDouble();
/*if(p>10.0) p=10.0;
else if(p<-1.0) p=-1.0;*/
model.lookup(str.c_str(),str.length())=p;//pStr.asDouble();
}}
if(getStrand()==FORWARD_STRAND) {
BOOM::String modelType;
is >> modelType;
modelType.removeWhitespace();
if(modelType.isEmpty()) revComp=NULL;
else {
revComp=new IMM(is,REVERSE_STRAND);
revComp->revComp=this; }
}
}
void IMM::buildModels(BOOM::Vector<TrainingSequence*> &v,
int minSampleSize)
{
/*
This is the training procedure for IMMs
*/
// Instantiate a separate hash table for each order
counts=new BOOM::Vector<BOOM::StringMap<int>*>;
for(int i=0 ; i<=N ; ++i)
{
int size=hashTableSize(N);
models->push_back(new BOOM::StringMap<double>(size));
counts->push_back(new BOOM::StringMap<int>(size));
}
// Install pseudocounts
for(int order=0 ; order<=N ; ++order)
{
BOOM::StringMap<int> &count=*(*counts)[order];
NthOrderStringIterator iterator(order+1,alphabet);
while(!iterator.done())
{
BOOM::String ngram=iterator.getNextString();
count.lookup(ngram.c_str(),ngram.length())=1;
}
}
// Extract all windows up to size N and count the different
// combinations that actually occur
BOOM::Vector<TrainingSequence*>::iterator cur=v.begin(), end=v.end();
for(; cur!=end ; ++cur)
{
TrainingSequence &seq=**cur;
BOOM::String &str=*seq.toString(alphabet);
for(int order=0 ; order<=N ; ++order)
if(getStrand()==FORWARD_STRAND)
updateCounts_fw(str,order,seq.getPhase(),seq.getBoostCount());
else
updateCounts_rev(str,order,seq.getPhase(),seq.getBoostCount());
delete &str;
}
// Now convert the counts into conditional probabilities
if(getStrand()==FORWARD_STRAND)
{
computeProbabilities_fw(minSampleSize);
interpolate_fw();
}
else
{
computeProbabilities_rev(minSampleSize);
interpolate_rev();
}
delete counts;
}
void IMM::computeProbabilities_fw(int minSampleSize)
{
for(int order=0 ; order<=N ; ++order)
{
BOOM::StringMap<double> &model=*(*models)[order];
BOOM::StringMap<int> &count=*(*counts)[order];
NthOrderStringIterator iterator(order,alphabet);
while(!iterator.done())
{
// Within this state (history), consider all emissions
BOOM::String history=iterator.getNextString();
int sampleSize=0;
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String ngram=history+alphabet.lookup(s);
if(count.isDefined(ngram.c_str(),ngram.length()))
sampleSize+=int(count.lookup(ngram.c_str(),ngram.length()));
}
// If sample size is insufficient, remove from the model
// (a lower-order model will always be available)
if(sampleSize<minSampleSize)
undefine_fw(history,count);
// Otherwise, normalize counts by sample size to produce a
// probability
else
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String ngram=history+alphabet.lookup(s);
if(count.isDefined(ngram.c_str(),ngram.length()))
{
model.lookup(ngram.c_str(),ngram.length())=
log(count.lookup(ngram.c_str(),ngram.length())/
(double)sampleSize);
}
else // ### this might be dangerous...
model.lookup(ngram.c_str(),ngram.length())=
NEGATIVE_INFINITY;
}
}
}
}
void IMM::computeProbabilities_rev(int minSampleSize)
{
for(int order=0 ; order<=N ; ++order)
{
BOOM::StringMap<double> &model=*(*models)[order];
BOOM::StringMap<int> &count=*(*counts)[order];
NthOrderStringIterator iterator(order,alphabet);
while(!iterator.done())
{
// Within this state (future), consider all emissions
BOOM::String future=iterator.getNextString();
int sampleSize=0;
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String ngram=alphabet.lookup(s)+future;
if(count.isDefined(ngram.c_str(),ngram.length()))
sampleSize+=int(count.lookup(ngram.c_str(),ngram.length()));
}
// If sample size is insufficient, remove from the model
// (a lower-order model will always be available)
if(sampleSize<minSampleSize)
undefine_rev(future,count);
// Otherwise, normalize counts by sample size to produce a
// probability
else
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String ngram=alphabet.lookup(s)+future;
if(count.isDefined(ngram.c_str(),ngram.length()))
{
model.lookup(ngram.c_str(),ngram.length())=
log(count.lookup(ngram.c_str(),ngram.length())
/(double)sampleSize);
}
else // ### this might be dangerous...
model.lookup(ngram.c_str(),ngram.length())=
NEGATIVE_INFINITY;
}
}
}
}
void IMM::undefine_fw(BOOM::String &history,
BOOM::StringMap<int> &model)
{
/*
This method removes from the given model all n-grams consisting
of the given history followed by a single base.
*/
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String ngram=history+alphabet.lookup(s);
if(model.isDefined(ngram.c_str(),ngram.length()))
model.remove(ngram.c_str(),ngram.length());
}
}
void IMM::undefine_rev(BOOM::String &future,
BOOM::StringMap<int> &model)
{
/*
This method removes from the given model all n-grams consisting
of the given future preceded by a single base.
*/
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String ngram=alphabet.lookup(s)+future;
if(model.isDefined(ngram.c_str(),ngram.length()))
model.remove(ngram.c_str(),ngram.length());
}
}
void IMM::updateCounts_fw(BOOM::String &str,int order,int seqPhase,
int boostCount)
{
/*
This method simply counts all the n-grams of length order+1.
*/
BOOM::StringMap<int> &count=*(*counts)[order];
int windowSize=order+1;
int len=str.length();
int firstPos, periodicity;
if(phase==NO_PHASE)
{
firstPos=0;
periodicity=1;
}
else
{
if(seqPhase==-1)
throw BOOM::String("seq phase=-1 in IMM::updateCounts(), "
"called as part of ThreePeriodicIMM:\n")+str;
firstPos=(phase-seqPhase+3)%3;
periodicity=3;
}
for(int pos=firstPos ; pos<len ; pos+=periodicity)
{
int begin=pos-order;
if(begin<0) continue;
if(count.isDefined(str.c_str(),begin,windowSize))
count.lookup(str.c_str(),begin,windowSize)+=boostCount;
else
count.lookup(str.c_str(),begin,windowSize)=boostCount;
}
}
void IMM::updateCounts_rev(BOOM::String &str,int order,int seqPhase,
int boostCount)
{
/*
This method simply counts all the n-grams of length order+1.
*/
BOOM::StringMap<int> &count=*(*counts)[order];
int windowSize=order+1;
int len=str.length();
int firstPos, periodicity;
if(phase==NO_PHASE)
{
firstPos=0;
periodicity=1;
}
else
{
if(seqPhase==-1)
throw BOOM::String("seq phase=-1 in IMM::updateCounts(), "
"called as part of ThreePeriodicIMM (-):\n")+str;
firstPos=(seqPhase-phase+3)%3;
periodicity=3;
}
for(int pos=firstPos ; pos+order<len ; pos+=periodicity)
{
if(count.isDefined(str.c_str(),pos,windowSize))
count.lookup(str.c_str(),pos,windowSize)+=boostCount;
else
count.lookup(str.c_str(),pos,windowSize)=boostCount;
}
}
void IMM::revCompSeqs(BOOM::Vector<TrainingSequence*> &forwardSeqs,
BOOM::Vector<TrainingSequence*> &revSeqs)
{
BOOM::Vector<TrainingSequence*>::iterator cur=forwardSeqs.begin(),
end=forwardSeqs.end();
for(; cur!=end ; ++cur)
revSeqs.push_back(
static_cast<TrainingSequence*>((*cur)->reverseComplement(alphabet)));
}
void IMM::useLogOdds_anonymous(ContentSensor &nullModel)
{
Strand strand=getStrand();
for(int order=0 ; order<=N ; ++order)
{
BOOM::StringMap<double> *model=(*models)[order];
BOOM::StringMap<double>::iterator cur=model->begin(),
end=model->end();
for(; cur!=end ; ++cur)
{
const char *p=(*cur).first;
int len=strlen(p);
Sequence seq(p,alphabet);
int targetPos=(strand==FORWARD_STRAND ? len-1 : 0);
double nullScore=nullModel.scoreSingleBase(seq,p,targetPos,
seq[targetPos],
p[targetPos]);
cout << "="<<nullScore<<" vs "<<(*cur).second <<p<<endl;
(*cur).second-=nullScore;
}
}
if(strand==FORWARD_STRAND && revComp && revComp!=this)
revComp->useLogOdds_anonymous(*nullModel.reverseComplement());
}
void IMM::useLogOdds(ContentSensor &nullModel)
{
IMM &other=dynamic_cast<IMM&>(nullModel);
for(int order=0 ; order<=N ; ++order)
{
BOOM::StringMap<double> *model=(*models)[order];
BOOM::StringMap<double> *otherModel=(*other.models)[order];
BOOM::StringMap<double>::iterator cur=model->begin(),
end=model->end();
for(; cur!=end ; ++cur)
{
const char *p=(*cur).first;
int len=strlen(p);
(*cur).second-=otherModel->lookup(p,0,len);
}
}
if(getStrand()==FORWARD_STRAND && revComp && revComp!=this)
revComp->useLogOdds(*nullModel.reverseComplement());
}
void IMM::interpolate_fw()
{
BOOM::Vector<BOOM::StringMap<double>*> interpolated;
interpolated.push_back((*models)[0]);
for(int order=1 ; order<=N ; ++order)
{
BOOM::StringMap<double> *newModel=
new BOOM::StringMap<double>(hashTableSize(order));
interpolated.push_back(newModel);
BOOM::StringMap<double> *prevIMM=interpolated[order-1];
BOOM::StringMap<double> *model=(*models)[order];
BOOM::StringMap<int> *count=(*counts)[order];
BOOM::StringMap<int> *smallerCount=(*counts)[order-1];
NthOrderStringIterator iterator(order,alphabet);
while(!iterator.done())
{
BOOM::String longHistory=iterator.getNextString();
BOOM::String shortHistory=longHistory.substr(1,order-1);
BOOM::Vector<int> longCounts, shortCounts; // distributions
int longTotal=0;
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
char c=alphabet.lookup(s);
BOOM::String ngram=longHistory+c;
const char *p=ngram.c_str(); int len=ngram.length();
if(!count->isDefined(p,len)) continue;
int longCount=count->lookup(p,len);
longCounts.push_back(longCount);
longTotal+=longCount;
ngram=shortHistory+c;
int shortCount=
smallerCount->lookup(ngram.c_str(),ngram.length());
/*
// ### the following is true to the original paper...
int shortCount=
10000*prevIMM->lookup(ngram.c_str(),ngram.length());
*/
shortCounts.push_back(shortCount);
}
double p=chiTest(longCounts,shortCounts);
double confidence=1-p;// confidence that the distributions differ
if(confidence<0.5) confidence=0.0;
double samplePortion=longTotal/double(400);
double lambda=
(samplePortion>=1 ? 1.0 : confidence*samplePortion);
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
char c=alphabet.lookup(s);
BOOM::String ngram=longHistory+c;
const char *p=ngram.c_str(); int len=ngram.length();
if(!count->isDefined(p,len)) continue;
newModel->lookup(p,len)=
lambda * model->lookup(p,len) +
(1-lambda) * prevIMM->lookup(p+1,len-1);
}
}
}
for(int i=1 ; i<=N ; ++i) delete (*models)[i];
*models=interpolated;
}
double IMM::chiTest(BOOM::Vector<int> &counts1,BOOM::Vector<int> &counts2)
{
int sum1=0, sum2=0, n1=counts1.size(), n2=counts2.size();
for(int i=0 ; i<n1 ; ++i) sum1+=counts1[i];
for(int i=0 ; i<n2 ; ++i) sum2+=counts2[i];
BOOM::Vector<int> expectedCounts;
double denom=sum2;
for(int i=0 ; i<n2 ; ++i)
expectedCounts.push_back(int(counts2[i]/denom*sum1+0.5));
BOOM::Chi2FitTest test(counts1,expectedCounts,chiSquaredTable);
return test.getP();
}
void IMM::interpolate_rev()
{
BOOM::Vector<BOOM::StringMap<double>*> interpolated;
interpolated.push_back((*models)[0]);
for(int order=1 ; order<=N ; ++order)
{
BOOM::StringMap<double> *newModel=
new BOOM::StringMap<double>(hashTableSize(order));
interpolated.push_back(newModel);
BOOM::StringMap<double> *prevIMM=interpolated[order-1];
BOOM::StringMap<double> *model=(*models)[order];
BOOM::StringMap<int> *count=(*counts)[order];
BOOM::StringMap<int> *smallerCount=(*counts)[order-1];
NthOrderStringIterator iterator(order,alphabet);
while(!iterator.done())
{
BOOM::String longFuture=iterator.getNextString();
BOOM::String shortFuture=longFuture.substr(0,order-1);
BOOM::Vector<int> longCounts, shortCounts; // distributions
int longTotal=0;
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
char c=alphabet.lookup(s);
BOOM::String ngram=c+longFuture;
const char *p=ngram.c_str(); int len=ngram.length();
if(!count->isDefined(p,len)) continue;
int longCount=count->lookup(p,len);
longCounts.push_back(longCount);
longTotal+=longCount;
ngram=c+shortFuture;
int shortCount=
smallerCount->lookup(ngram.c_str(),ngram.length());
shortCounts.push_back(shortCount);
}
double p=chiTest(longCounts,shortCounts);
double confidence=1-p;// confidence that the distributions differ
if(confidence<0.5) confidence=0.0;
double samplePortion=longTotal/double(400);
double lambda=
(samplePortion>=1 ? 1.0 : confidence*samplePortion);
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
char c=alphabet.lookup(s);
BOOM::String ngram=c+longFuture;
const char *p=ngram.c_str(); int len=ngram.length();
if(!count->isDefined(p,len)) continue;
newModel->lookup(p,len)=
lambda * model->lookup(p,len) +
(1-lambda) * prevIMM->lookup(p,len-1);
}
}
}
for(int i=1 ; i<=N ; ++i) delete (*models)[i];
*models=interpolated;
}
ContentSensor *IMM::compile()
{
return MarkovChainCompiler::compile(*this);
}