18
18
import beast .core .NSLogger ;
19
19
import beast .core .Operator ;
20
20
import beast .core .State ;
21
+ import beast .core .StateNode ;
21
22
import beast .core .StateNodeInitialiser ;
23
+ import beast .core .parameter .Parameter ;
22
24
import beast .core .util .CompoundDistribution ;
23
25
import beast .core .util .Evaluator ;
24
26
import beast .core .util .Log ;
27
+ import beast .evolution .tree .Tree ;
25
28
import beast .util .NSLogAnalyser ;
26
29
import beast .util .Randomizer ;
27
30
@@ -79,6 +82,10 @@ public class NS extends MCMC {
79
82
List <Double > likelihoods = new ArrayList <>();
80
83
boolean finished = false ;
81
84
85
+ int paramCount = 0 ;
86
+ double paramCountFactor = 1.0 ;
87
+ boolean autoLoopLength = false ;
88
+
82
89
public NS () {}
83
90
84
91
public NS (int chainLength , int preBurnin , int particleCount , int subChainLength , State state , List <Operator > operators , CompoundDistribution distribution , Double epsilon ) {
@@ -155,6 +162,19 @@ public void initAndValidate() {
155
162
H = 0 ;
156
163
157
164
NSloggers = new ArrayList <>();
165
+
166
+
167
+ paramCount = 0 ;
168
+ for (StateNode node : state .stateNodeInput .get ()) {
169
+ if (node instanceof Parameter <?>) {
170
+ Parameter <?> param = (Parameter <?>) node ;
171
+ paramCount += param .getDimension ();
172
+ } else if (node instanceof Tree ) {
173
+ Tree tree = (Tree ) node ;
174
+ paramCount += tree .getNodeCount () * 2 ;
175
+ }
176
+ }
177
+ Log .warning ("Counting " + paramCount + " parameters" );
158
178
}
159
179
160
180
@ Override
@@ -512,8 +532,22 @@ protected void updateParticle(int sampleNr) {
512
532
oldLogPrior += d .getArrayValue ();
513
533
}
514
534
515
- for (int j = 0 ; j < subChainLength ; j ++) {
516
- composeProposal (j + subChainLength * sampleNr );
535
+
536
+ if (autoLoopLength ) {
537
+ int acceptCount = 0 ;
538
+ int j = 0 ;
539
+ while (acceptCount < paramCount * paramCountFactor ) {
540
+ boolean accept = composeProposal (j + subChainLength * sampleNr );
541
+ if (accept ) {
542
+ acceptCount ++;
543
+ }
544
+ j ++;
545
+ }
546
+ //System.err.println(j);
547
+ } else {
548
+ for (int j = 0 ; j < subChainLength ; j ++) {
549
+ composeProposal (j + subChainLength * sampleNr );
550
+ }
517
551
}
518
552
}
519
553
@@ -546,7 +580,8 @@ public static double logPlus(double x, double y) {
546
580
* the current state
547
581
* @return the selected {@link beast.core.Operator}
548
582
*/
549
- protected Operator composeProposal (final int currState ) {
583
+ protected boolean composeProposal (final int currState ) {
584
+ boolean accept = false ;
550
585
state .store (currState );
551
586
552
587
final Operator operator = operatorSchedule .selectOperator ();
@@ -612,6 +647,7 @@ public double evaluate() {
612
647
}
613
648
if (printDebugInfo )
614
649
System .err .print (" accept" );
650
+ accept = true ;
615
651
} else {
616
652
// reject
617
653
if (currState >= 0 ) {
@@ -637,6 +673,6 @@ public double evaluate() {
637
673
System .err .print (" direct reject" );
638
674
}
639
675
log (currState );
640
- return operator ;
676
+ return accept ;
641
677
}
642
678
}
0 commit comments