// File: j-faria[P]kima/src/Data.cpp
// Function: Data::get_adjusted_RV_var() const
double Data::get_adjusted_RV_var() const
{
    int ni;
    double sum, mean;
    std::vector<double> rva(t.size());

    for(size_t j=0; j<number_instruments; j++)
    {
      ni = 0;
      sum = 0.;
      for (size_t i=0; i<t.size(); i++)
        if(obsi[i] == j+1) {sum += y[i]; ni++;}
      mean = sum / ni;
      // cout << "sum: " << sum << endl;
      // cout << "mean: " << mean << endl;
      for (size_t i=0; i<t.size(); i++)
        if(obsi[i] == j+1) rva[i] = y[i] - mean;
    }

    mean = std::accumulate(rva.begin(), rva.end(), 0.0) / rva.size();
    double accum = 0.0;
    std::for_each (rva.begin(), rva.end(), [&](const double d) {
        accum += (d - mean) * (d - mean);
    });
    return accum / (y.size()-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x7c90
movq %rax, -0xc0(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x7e10
movq -0xc0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x163f0
jmp 0x140bf
leaq -0x39(%rbp), %rdi
callq 0x8190
movq $0x0, -0x58(%rbp)
movq -0xc8(%rbp), %rcx
movq -0x58(%rbp), %rax
movslq 0xf8(%rcx), %rcx
cmpq %rcx, %rax
jae 0x1426f
movl $0x0, -0xc(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x18(%rbp)
movq $0x0, -0x60(%rbp)
movq -0xc8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq %rax, -0xd0(%rbp)
callq 0x7c90
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x141a6
movq -0xc8(%rbp), %rdi
addq $0x48, %rdi
movq -0x60(%rbp), %rsi
callq 0x166f0
movslq (%rax), %rax
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x14193
movq -0xc8(%rbp), %rdi
addq $0x18, %rdi
movq -0x60(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x14193
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x8190
jmp 0x14410
jmp 0x14195
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x14102
movsd -0x18(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movq $0x0, -0x68(%rbp)
movq -0xc8(%rbp), %rdi
movq -0x68(%rbp), %rax
movq %rax, -0xd8(%rbp)
callq 0x7c90
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1425c
movq -0xc8(%rbp), %rdi
addq $0x48, %rdi
movq -0x68(%rbp), %rsi
callq 0x166f0
movslq (%rax), %rax
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x14249
movq -0xc8(%rbp), %rdi
addq $0x18, %rdi
movq -0x68(%rbp), %rsi
callq 0xa6a0
movsd (%rax), %xmm0
subsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x68(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xc6a0
movsd -0xe0(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x1424b
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x141c1
jmp 0x1425e
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x140d0
leaq -0x38(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x7890
movq -0xf0(%rbp), %rdi
movq %rax, -0x70(%rbp)
callq 0x78c0
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
xorps %xmm0, %xmm0
callq 0x15e90
movsd %xmm0, -0xe8(%rbp)
jmp 0x142ad
leaq -0x38(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x7c90
movsd -0xe8(%rbp), %xmm0
movq -0x108(%rbp), %rdi
movq %rax, %xmm1
movaps 0xa2d38(%rip), %xmm2    # 0xb7010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xa2d3c(%rip), %xmm2    # 0xb7020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movq $0x0, -0x80(%rbp)
callq 0x7890
movq -0x108(%rbp), %rdi
movq %rax, -0x88(%rbp)
callq 0x78c0
movq %rax, -0x90(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x20(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x14420
movq %rdx, -0x100(%rbp)
movq %rax, -0xf8(%rbp)
jmp 0x1436a
movq -0xc8(%rbp), %rdi
movq -0x100(%rbp), %rax
movq -0xf8(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
movq %rax, -0xa8(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
addq $0x18, %rdi
callq 0x7c90
movsd -0x118(%rbp), %xmm0
decq %rax
movq %rax, %xmm1
movaps 0xa2c56(%rip), %xmm2    # 0xb7010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xa2c5a(%rip), %xmm2    # 0xb7020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x110(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x78f0
movsd -0x110(%rbp), %xmm0
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x78f0
movq -0x48(%rbp), %rdi
callq 0x6830
nopl (%rax)