// File: bobverity[P]MavericK/TI.cpp
// Function: TI_noAdmixture(globals&, int)
void TI_noAdmixture(globals &globals, int Kindex) {
    int K = globals.Kmin+Kindex;
    
    // special case if K==1
    if (K==1) {
        for (int TIrep=0; TIrep<globals.thermodynamicRungs; TIrep++) {
            globals.TIpoint_mean[Kindex][TIrep] = globals.logEvidence_exhaustive[Kindex];
            globals.TIpoint_var[Kindex][TIrep] = 0;
            globals.TIpoint_SE[Kindex][TIrep] = 0;
        }
        globals.logEvidence_TI[Kindex] = globals.logEvidence_exhaustive[Kindex];
        globals.logEvidence_TI_SE[Kindex] = 0;
        return;
    }
    
    // set up beta vector
    vector<double> betaVec(globals.thermodynamicRungs);
    for (int TIrep=0; TIrep<globals.thermodynamicRungs; TIrep++)
        betaVec[TIrep] = double(TIrep)/(globals.thermodynamicRungs-1);
    
    // carry out thermodynamic integration
    for (int TIrep=0; TIrep<globals.thermodynamicRungs; TIrep++) {
        double beta = betaVec[TIrep];
        
        char * buffer = new char[255];
        sprintf(buffer, "%.2f", beta);
        string s = buffer;
        coutAndLog("  power = "+s+"\n", globals.outputLog_on, globals.outputLog_fileStream);
        
        // define MCMC object
        MCMCobject_noAdmixture TI_MCMC(globals, Kindex, globals.thermodynamicBurnin, globals.thermodynamicSamples, globals.thermodynamicThinning, beta);
        
        // perform MCMC
        TI_MCMC.reset(true);
        TI_MCMC.perform_MCMC(globals, false, true, false, false, false, 1);
        
        // calculate autocorrelation
        double autoCorr = calculateAutoCorr(TI_MCMC.logLikeGroup_store);
        double ESS = globals.thermodynamicSamples/autoCorr;
        
        // save results of this power
        globals.TIpoint_mean[Kindex][TIrep] = TI_MCMC.logLikeGroup_sum/double(globals.thermodynamicSamples);
        globals.TIpoint_var[Kindex][TIrep] = TI_MCMC.logLikeGroup_sumSquared/double(globals.thermodynamicSamples) - globals.TIpoint_mean[Kindex][TIrep]*globals.TIpoint_mean[Kindex][TIrep];
		// avoid arithmetic underflow
		if (globals.TIpoint_var[Kindex][TIrep]<0)
			globals.TIpoint_var[Kindex][TIrep] = 0;
        globals.TIpoint_SE[Kindex][TIrep] = sqrt(globals.TIpoint_var[Kindex][TIrep]/ESS);
        
    }
    
    // calculate thermodynamic integral estimate. Note that at this stage we assume equally spaced rungs.
    double Dsum = 0.5*globals.TIpoint_mean[Kindex][0] + 0.5*globals.TIpoint_mean[Kindex][globals.thermodynamicRungs-1];
    double Vsum = 0.25*globals.TIpoint_SE[Kindex][0]*globals.TIpoint_SE[Kindex][0] + 0.25*globals.TIpoint_SE[Kindex][globals.thermodynamicRungs-1]*globals.TIpoint_SE[Kindex][globals.thermodynamicRungs-1];
    if (globals.thermodynamicRungs>2) {
        for (int TIrep=1; TIrep<(globals.thermodynamicRungs-1); TIrep++) {
            Dsum += globals.TIpoint_mean[Kindex][TIrep];
            Vsum += globals.TIpoint_SE[Kindex][TIrep]*globals.TIpoint_SE[Kindex][TIrep];
        }
    }
    Dsum /= double(globals.thermodynamicRungs-1);
    Vsum /= double(globals.thermodynamicRungs-1)*double(globals.thermodynamicRungs-1);
    globals.logEvidence_TI[Kindex] = Dsum;
    globals.logEvidence_TI_SE[Kindex] = sqrt(Vsum);
    
}

// Assembly:
subq $0x498, %rsp            # imm = 0x498
movq %rdi, 0x490(%rsp)
movl %esi, 0x48c(%rsp)
movq 0x490(%rsp), %rax
movl 0x2bc8(%rax), %eax
addl 0x48c(%rsp), %eax
movl %eax, 0x488(%rsp)
cmpl $0x1, 0x488(%rsp)
jne 0x2132e
movl $0x0, 0x484(%rsp)
movl 0x484(%rsp), %eax
movq 0x490(%rsp), %rcx
cmpl 0x2c20(%rcx), %eax
jge 0x212b4
movq 0x490(%rsp), %rdi
addq $0x2e00, %rdi           # imm = 0x2E00
movslq 0x48c(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x484(%rsp), %rsi
callq 0x8c20
movsd 0xb0(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x490(%rsp), %rdi
addq $0x2ef0, %rdi           # imm = 0x2EF0
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x484(%rsp), %rsi
callq 0x8c20
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x484(%rsp), %rsi
callq 0x8c20
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl 0x484(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x484(%rsp)
jmp 0x211bb
movq 0x490(%rsp), %rdi
addq $0x2e00, %rdi           # imm = 0x2E00
movslq 0x48c(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f20, %rdi           # imm = 0x2F20
movslq 0x48c(%rsp), %rsi
callq 0x8c20
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x490(%rsp), %rdi
addq $0x2f38, %rdi           # imm = 0x2F38
movslq 0x48c(%rsp), %rsi
callq 0x8c20
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0x21d45
movq 0x490(%rsp), %rax
movslq 0x2c20(%rax), %rax
movq %rax, 0x98(%rsp)
leaq 0x467(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x8780
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x468(%rsp), %rdi
callq 0x8790
jmp 0x21379
leaq 0x467(%rsp), %rdi
callq 0x8910
movl $0x0, 0x450(%rsp)
movl 0x450(%rsp), %eax
movq 0x490(%rsp), %rcx
cmpl 0x2c20(%rcx), %eax
jge 0x21432
cvtsi2sdl 0x450(%rsp), %xmm0
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movslq 0x450(%rsp), %rsi
leaq 0x468(%rsp), %rdi
callq 0x8c20
movsd 0x90(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x450(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x450(%rsp)
jmp 0x21391
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
leaq 0x467(%rsp), %rdi
callq 0x8910
jmp 0x21d5a
movl $0x0, 0x44c(%rsp)
movl 0x44c(%rsp), %eax
movq 0x490(%rsp), %rcx
cmpl 0x2c20(%rcx), %eax
jge 0x21990
movslq 0x44c(%rsp), %rsi
leaq 0x468(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x440(%rsp)
movl $0xff, %edi
callq 0x4030
movq %rax, 0x88(%rsp)
jmp 0x2148e
movq 0x88(%rsp), %rax
movq %rax, 0x438(%rsp)
movq 0x438(%rsp), %rdi
movsd 0x440(%rsp), %xmm0
leaq 0x3ad8a(%rip), %rsi     # 0x5c240
movb $0x1, %al
callq 0x40b0
movq 0x438(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x417(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x4740
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
leaq 0x418(%rsp), %rdi
callq 0x4580
jmp 0x214fb
leaq 0x417(%rsp), %rdi
callq 0x4490
leaq 0x3ad36(%rip), %rsi     # 0x5c245
leaq 0x3d0(%rsp), %rdi
leaq 0x418(%rsp), %rdx
callq 0x22950
jmp 0x21526
leaq 0x3b365(%rip), %rdx     # 0x5c892
leaq 0x3f0(%rsp), %rdi
leaq 0x3d0(%rsp), %rsi
callq 0x22900
jmp 0x21544
movq 0x490(%rsp), %rax
movq %rax, %rdx
addq $0x970, %rdx            # imm = 0x970
movzbl 0x2c3c(%rax), %esi
andl $0x1, %esi
leaq 0x3f0(%rsp), %rdi
callq 0x3a160
jmp 0x2156f
leaq 0x3f0(%rsp), %rdi
callq 0x4270
leaq 0x3d0(%rsp), %rdi
callq 0x4270
movq 0x490(%rsp), %rsi
movl 0x48c(%rsp), %edx
movl 0x2c24(%rsi), %ecx
movl 0x2c28(%rsi), %r8d
movl 0x2c2c(%rsi), %r9d
movsd 0x440(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x1c600
jmp 0x215c4
leaq 0xe0(%rsp), %rdi
movl $0x1, %esi
callq 0x1dfe0
jmp 0x215d8
movq 0x490(%rsp), %rsi
movq %rsp, %rax
movl $0x1, 0x8(%rax)
movl $0x0, (%rax)
leaq 0xe0(%rsp), %rdi
movl $0x1, %ecx
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %r8d
callq 0x1edf0
jmp 0x2160d
leaq 0x210(%rsp), %rdi
callq 0x35f80
movsd %xmm0, 0x70(%rsp)
jmp 0x21622
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movq 0x490(%rsp), %rax
cvtsi2sdl 0x2c28(%rax), %xmm0
divsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x208(%rsp), %xmm0
movq 0x490(%rsp), %rax
cvtsi2sdl 0x2c28(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x228(%rsp), %xmm0
movq 0x490(%rsp), %rax
cvtsi2sdl 0x2c28(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd (%rax), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2ef0, %rdi           # imm = 0x2EF0
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x490(%rsp), %rdi
addq $0x2ef0, %rdi           # imm = 0x2EF0
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
jbe 0x218d4
movq 0x490(%rsp), %rdi
addq $0x2ef0, %rdi           # imm = 0x2EF0
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
jmp 0x218d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
jmp 0x21d4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
leaq 0x417(%rsp), %rdi
callq 0x4490
jmp 0x21d4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
jmp 0x2197e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
jmp 0x2189c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
leaq 0x3f0(%rsp), %rdi
callq 0x4270
leaq 0x3d0(%rsp), %rdi
callq 0x4270
jmp 0x2197e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x458(%rsp)
movl %eax, 0x454(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x22a80
jmp 0x2197e
movq 0x490(%rsp), %rdi
addq $0x2ef0, %rdi           # imm = 0x2EF0
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
divsd 0xd0(%rsp), %xmm0
callq 0x4370
movsd %xmm0, 0x48(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0x44c(%rsp), %rsi
callq 0x8c20
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xe0(%rsp), %rdi
callq 0x22a80
leaq 0x418(%rsp), %rdi
callq 0x4270
movl 0x44c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44c(%rsp)
jmp 0x2143d
leaq 0x418(%rsp), %rdi
callq 0x4270
jmp 0x21d4d
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8c20
movsd 0x28(%rsp), %xmm0
movsd 0x3a830(%rip), %xmm1    # 0x5c238
mulsd (%rax), %xmm1
movsd 0x3a824(%rip), %xmm2    # 0x5c238
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8c20
movsd 0x3a7db(%rip), %xmm0    # 0x5c230
mulsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8c20
movsd 0x3a75f(%rip), %xmm0    # 0x5c230
mulsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8c20
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm2
mulsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0x490(%rsp), %rax
cmpl $0x2, 0x2c20(%rax)
jle 0x21c5a
movl $0x1, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movq 0x490(%rsp), %rcx
movl 0x2c20(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x21c58
movq 0x490(%rsp), %rdi
addq $0x2ed8, %rdi           # imm = 0x2ED8
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0xbc(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0xbc(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f08, %rdi           # imm = 0x2F08
movslq 0x48c(%rsp), %rsi
callq 0x8bc0
movq %rax, %rdi
movslq 0xbc(%rsp), %rsi
callq 0x8c20
movsd 0x20(%rsp), %xmm0
movsd (%rax), %xmm2
movsd 0xc0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x21b5a
jmp 0x21c5a
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0xc8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movq 0x490(%rsp), %rax
movl 0x2c20(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
mulsd %xmm0, %xmm1
movsd 0xc0(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f20, %rdi           # imm = 0x2F20
movslq 0x48c(%rsp), %rsi
callq 0x8c20
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0xc0(%rsp), %xmm0
callq 0x4370
movsd %xmm0, 0x18(%rsp)
movq 0x490(%rsp), %rdi
addq $0x2f38, %rdi           # imm = 0x2F38
movslq 0x48c(%rsp), %rsi
callq 0x8c20
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x468(%rsp), %rdi
callq 0x88b0
addq $0x498, %rsp            # imm = 0x498
retq
leaq 0x468(%rsp), %rdi
callq 0x88b0
movq 0x458(%rsp), %rdi
callq 0x4730
nopw (%rax,%rax)