// File: /workspace/github_cpp_makefile_star2_O1/ashlall[P]StreamStats/Test/Chi_Square_Categorical_test.cpp
// Function: categorical_test4()
void categorical_test4()
{	int seed=0;
	double memory_percent=10;
	int stream_size1 = 1000000;
	int stream_size2 = stream_size1;
	int num_cat = 10000, df = num_cat - 1;
	int cumulative_freq1[num_cat];
	int cumulative_freq2[num_cat];
	int count_1[num_cat], count_2[num_cat];

	// generate a Zipfian distribution with num_cat categories
	// and parameters alpha1 and alpha2
	int sum1 = 0, sum2 = 0;
	double alpha1 = 1.1, alpha2 = 1.105;
	for(int i = 0; i < num_cat; ++i)
	{
	    sum1 += max((int)(num_cat/pow(i+1, alpha1)), 1); 
	    cumulative_freq1[i] = sum1;

	    sum2 += max((int)(num_cat/pow(i+1, alpha2)), 1);
            cumulative_freq2[i] = sum2; 

	}

	for(int repeat=0;repeat<10;repeat++)
	{
	    double actual_chi;
	    ChiSquareCategorical c1(memory_percent);
	    ChiSquareCategorical c2(memory_percent);
	    
	    double p_value = 0;
	    
	    while (p_value < 0.02 || p_value > .1)
	    {			
		std::fill_n(count_1,num_cat,1);
		std::fill_n(count_2,num_cat,1);
		
		srand(seed);
		
		for(int j=0; j < stream_size1; ++j)
		    count_1[sample(num_cat, cumulative_freq1)]++;
		
		for(int j=0; j < stream_size2; ++j)
		    count_2[sample(num_cat, cumulative_freq2)]++;
		
		//for(int j=0; j < 10; ++j)
		//	cout << count_1[j] << " " << count_2[j] << endl;
		
		double constant_1 = sqrt((double)stream_size2/stream_size1);
		double constant_2 = sqrt((double)stream_size1/stream_size2);
		
		actual_chi = 0.0;
		for(int i=0;i<num_cat;i++)
		{
		    double frequency_1=count_1[i];
		    double frequency_2=count_2[i];
		    double value = frequency_1 * constant_1 - frequency_2 * constant_2;
		    actual_chi += (value * value) / (frequency_1 + frequency_2);
		}
		
		p_value=pochisq(actual_chi, df);
		//cout << actual_chi << " " << p_value << endl;
		seed=seed+1;  
	    }
	    
	    for(int i=0;i<num_cat;i++)
	    {
		for(int j=0;j<count_1[i];j++)
		    c1.insert(i);
		
		for(int k=0;k<count_2[i];k++)
		    c2.insert(i);
	    }

	    double chi=c1.calculate_statistic(c2);
	    
	    cout << "Actual stat:" << actual_chi << endl;
	    cout << "Calculated :" << chi << endl;
	    
	    cout<<"Actual   p: "<<p_value<<endl;
	    cout<<"Calculated: "<<pochisq(chi,df) <<endl << endl;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x271f8, %rsp          # imm = 0x271F8
xorl %ebx, %ebx
movl $0x1, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
leaq 0x1(%rbx), %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x11f6(%rip), %xmm1     # 0x3130
callq 0x11b0
movsd 0x11f1(%rip), %xmm1     # 0x3138
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
cmovll %r14d, %eax
addl %eax, %r15d
movl %r15d, -0x27220(%rbp,%rbx,4)
movsd -0x30(%rbp), %xmm0
movsd 0x11d2(%rip), %xmm1     # 0x3140
callq 0x11b0
movsd 0x11bd(%rip), %xmm1     # 0x3138
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
cmovll %r14d, %eax
addl %eax, %r12d
movl %r12d, -0x1d5e0(%rbp,%rbx,4)
movq %r13, %rbx
cmpq $0x2710, %r13           # imm = 0x2710
jne 0x1f21
movabsq $0x100000000, %r14      # imm = 0x100000000
leaq -0xe8(%rbp), %r12
leaq -0x80(%rbp), %r13
leaq -0x27220(%rbp), %rax
movl 0x9c3c(%rax), %r15d
leaq -0x1d5e0(%rbp), %rax
movl 0x9c3c(%rax), %ebx
movl $0x0, -0x34(%rbp)
xorl %eax, %eax
movl %eax, -0x38(%rbp)
xorl %ecx, %ecx
movq %rcx, -0xf0(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0x1, %edx
movq %rdx, -0xe0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%r12)
movl $0x3f800000, %esi       # imm = 0x3F800000
movl %esi, -0xc8(%rbp)
movupd %xmm0, 0x28(%r12)
movq %rcx, -0x118(%rbp)
movq %rcx, -0xf8(%rbp)
movabsq $0x4024000000000000, %rdi # imm = 0x4024000000000000
movq %rdi, -0x108(%rbp)
movq %r14, -0xf0(%rbp)
movq %rcx, -0x88(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
movupd %xmm0, 0x10(%r13)
movl %esi, -0x60(%rbp)
movupd %xmm0, 0x28(%r13)
movq %rcx, -0xb0(%rbp)
movq %rcx, -0x90(%rbp)
movq %rdi, -0xa0(%rbp)
movq %r14, -0x88(%rbp)
movl -0x34(%rbp), %edi
xorl %eax, %eax
movl $0x1, -0x139a0(%rbp,%rax)
addq $0x4, %rax
cmpq $0x9c40, %rax           # imm = 0x9C40
jne 0x2086
xorl %eax, %eax
movl $0x1, -0x9d60(%rbp,%rax)
addq $0x4, %rax
cmpq $0x9c40, %rax           # imm = 0x9C40
jne 0x209f
movl %edi, %r13d
callq 0x1100
xorl %r12d, %r12d
callq 0x1050
cltd
idivl %r15d
leaq -0x27220(%rbp), %rcx
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %r14, %rax
leaq 0x4(%rcx), %rsi
cmpl %edx, (%rcx)
movq %rsi, %rcx
jl 0x20db
sarq $0x1e, %rax
incl -0x139a0(%rbp,%rax)
incl %r12d
cmpl $0xf4240, %r12d         # imm = 0xF4240
jne 0x20c1
xorl %r12d, %r12d
callq 0x1050
cltd
idivl %ebx
leaq -0x1d5e0(%rbp), %rcx
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %r14, %rax
leaq 0x4(%rcx), %rsi
cmpl %edx, (%rcx)
movq %rsi, %rcx
jl 0x211c
sarq $0x1e, %rax
incl -0x9d60(%rbp,%rax)
incl %r12d
cmpl $0xf4240, %r12d         # imm = 0xF4240
jne 0x2103
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorps %xmm3, %xmm3
cvtsi2sdl -0x139a0(%rbp,%rax,4), %xmm3
xorps %xmm1, %xmm1
cvtsi2sdl -0x9d60(%rbp,%rax,4), %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm3
divsd %xmm3, %xmm2
addsd %xmm2, %xmm0
incq %rax
cmpq $0x2710, %rax           # imm = 0x2710
jne 0x2147
movsd %xmm0, -0x48(%rbp)
movl $0x270f, %edi           # imm = 0x270F
callq 0x1a48
movapd %xmm0, %xmm1
movl %r13d, %edi
incl %edi
movsd 0xfa6(%rip), %xmm0      # 0x3148
ucomisd %xmm1, %xmm0
ja 0x2084
ucomisd 0xf9c(%rip), %xmm1      # 0x3150
ja 0x2084
movsd %xmm1, -0x40(%rbp)
movl %edi, -0x34(%rbp)
xorl %r12d, %r12d
movl -0x139a0(%rbp,%r12,4), %r13d
testl %r13d, %r13d
jle 0x21f5
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x118(%rbp), %rdi
movsd -0x30(%rbp), %xmm0
callq 0x1392
decl %r13d
jne 0x21df
movl -0x9d60(%rbp,%r12,4), %r13d
testl %r13d, %r13d
jle 0x2225
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0xb0(%rbp), %rdi
movsd -0x30(%rbp), %xmm0
callq 0x1392
decl %r13d
jne 0x220f
incq %r12
cmpq $0x2710, %r12           # imm = 0x2710
jne 0x21c5
leaq -0x118(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
xorl %edx, %edx
callq 0x1472
movsd %xmm0, -0x30(%rbp)
movq 0x2d86(%rip), %r12      # 0x4fd8
movl $0xc, %edx
movq %r12, %rdi
leaq 0xfaa(%rip), %rsi       # 0x320b
callq 0x1130
movq %r12, %rdi
movsd -0x48(%rbp), %xmm0
callq 0x1190
movq (%rax), %rcx
movq -0x18(%rcx), %rdi
addq %rax, %rdi
movl $0xa, %esi
movq %rax, %r13
callq 0x1110
movsbl %al, %esi
movq %r13, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
movl $0xc, %edx
movq %r12, %rdi
leaq 0xf6c(%rip), %rsi       # 0x3218
callq 0x1130
movq %r12, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x1190
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x1110
movsbl %al, %esi
movq %r13, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
movl $0xc, %edx
movq %r12, %rdi
leaq 0xf2e(%rip), %rsi       # 0x3225
callq 0x1130
movq %r12, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x1190
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x1110
movsbl %al, %esi
movq %r13, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
movl $0xc, %edx
movq %r12, %rdi
leaq 0xef0(%rip), %rsi       # 0x3232
callq 0x1130
movsd -0x30(%rbp), %xmm0
movl $0x270f, %edi           # imm = 0x270F
callq 0x1a48
movq %r12, %rdi
callq 0x1190
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x1110
movsbl %al, %esi
movq %r12, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x1110
movsbl %al, %esi
movq %r12, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
leaq -0x80(%rbp), %r13
movq %r13, %rdi
callq 0x2606
leaq -0xe8(%rbp), %r12
movq %r12, %rdi
callq 0x2606
movl -0x38(%rbp), %eax
incl %eax
cmpl $0xa, %eax
jne 0x1fda
addq $0x271f8, %rsp          # imm = 0x271F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x23f1
jmp 0x23f1
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2606
leaq -0xe8(%rbp), %rdi
callq 0x2606
movq %rbx, %rdi
callq 0x11c0