// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq $0x2710, %rbx           # imm = 0x2710
je 0x1e14
leaq 0x1(%rbx), %r13
cvtsi2sd %r13d, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x137c(%rip), %xmm1     # 0x3130
callq 0x1190
movsd 0x1377(%rip), %xmm1     # 0x3138
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
cmovll %r14d, %eax
addl %eax, %r12d
movl %r12d, -0x27220(%rbp,%rbx,4)
movsd -0x30(%rbp), %xmm0
movsd 0x1358(%rip), %xmm1     # 0x3140
callq 0x1190
movsd 0x1343(%rip), %xmm1     # 0x3138
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
cmovll %r14d, %eax
addl %eax, %r15d
movl %r15d, -0x1d5e0(%rbp,%rbx,4)
movq %r13, %rbx
jmp 0x1d95
leaq -0x27220(%rbp), %r12
leaq -0x118(%rbp), %r13
leaq -0xb0(%rbp), %rbx
xorl %eax, %eax
xorl %r14d, %r14d
cmpl $0xa, %eax
je 0x2142
movl %eax, -0x38(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0xe8(%rbp)
pushq $0x1
popq %rcx
movq %rcx, -0xe0(%rbp)
leaq -0xe8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0xc8(%rbp)
movups %xmm0, 0x28(%rax)
andq $0x0, -0x118(%rbp)
andq $0x0, -0xf8(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movq %rcx, -0x78(%rbp)
leaq -0x80(%rbp), %rax
movups %xmm0, 0x10(%rax)
movl %edx, -0x60(%rbp)
movups %xmm0, 0x28(%rax)
andq $0x0, -0xb0(%rbp)
movabsq $0x4024000000000000, %rcx # imm = 0x4024000000000000
movq %rcx, -0x108(%rbp)
andq $0x0, -0x90(%rbp)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, -0xf0(%rbp)
movq %rcx, -0xa0(%rbp)
movq %rax, -0x88(%rbp)
xorpd %xmm1, %xmm1
movsd 0x1262(%rip), %xmm0     # 0x3148
ucomisd %xmm1, %xmm0
ja 0x1efa
ucomisd 0x125c(%rip), %xmm1     # 0x3150
jbe 0x1fe2
xorl %eax, %eax
cmpq $0x9c40, %rax           # imm = 0x9C40
je 0x1f15
movl $0x1, -0x139a0(%rbp,%rax)
addq $0x4, %rax
jmp 0x1efc
xorl %eax, %eax
cmpq $0x9c40, %rax           # imm = 0x9C40
je 0x1f30
movl $0x1, -0x9d60(%rbp,%rax)
addq $0x4, %rax
jmp 0x1f17
movl %r14d, %r15d
movl %r14d, %edi
callq 0x10f0
movl $0xf4240, %r14d         # imm = 0xF4240
subl $0x1, %r14d
jb 0x1f5f
movl $0x2710, %edi           # imm = 0x2710
movq %r12, %rsi
callq 0x1d46
cltq
incl -0x139a0(%rbp,%rax,4)
jmp 0x1f41
movl $0xf4240, %r14d         # imm = 0xF4240
subl $0x1, %r14d
jb 0x1f87
movl $0x2710, %edi           # imm = 0x2710
leaq -0x1d5e0(%rbp), %rsi
callq 0x1d46
cltq
incl -0x9d60(%rbp,%rax,4)
jmp 0x1f65
xorpd %xmm0, %xmm0
xorl %eax, %eax
movl %r15d, %r14d
cmpq $0x2710, %rax           # imm = 0x2710
je 0x1fc7
cvtsi2sdl -0x139a0(%rbp,%rax,4), %xmm3
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
jmp 0x1f90
movsd %xmm0, -0x48(%rbp)
movl $0x270f, %edi           # imm = 0x270F
callq 0x192a
movapd %xmm0, %xmm1
incl %r14d
jmp 0x1ede
movsd %xmm1, -0x40(%rbp)
movl %r14d, -0x34(%rbp)
xorl %r14d, %r14d
cmpq $0x2710, %r14           # imm = 0x2710
je 0x2058
cvtsi2sd %r14d, %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x139a0(%rbp,%r14,4), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subl $0x1, %r15d
jb 0x202a
movq %r13, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x1360
jmp 0x2015
movl -0x9d60(%rbp,%r14,4), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subl $0x1, %r15d
jb 0x2053
movq %rbx, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x1360
jmp 0x203e
incq %r14
jmp 0x1fee
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x140e
movsd %xmm0, -0x30(%rbp)
movq 0x2f67(%rip), %rdi      # 0x4fd8
leaq 0x1193(%rip), %rsi      # 0x320b
callq 0x1100
movq %rax, %rdi
movsd -0x48(%rbp), %xmm0
callq 0x1170
movq %rax, %rdi
callq 0x1080
movq 0x2f3f(%rip), %rdi      # 0x4fd8
leaq 0x1178(%rip), %rsi      # 0x3218
callq 0x1100
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x1170
movq %rax, %rdi
callq 0x1080
movq 0x2f17(%rip), %rdi      # 0x4fd8
leaq 0x115d(%rip), %rsi      # 0x3225
callq 0x1100
movq %rax, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x1170
movq %rax, %rdi
callq 0x1080
movq 0x2eef(%rip), %rdi      # 0x4fd8
leaq 0x1142(%rip), %rsi      # 0x3232
callq 0x1100
movq %rax, %r14
movsd -0x30(%rbp), %xmm0
movl $0x270f, %edi           # imm = 0x270F
callq 0x192a
movq %r14, %rdi
callq 0x1170
movq %rax, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1080
leaq -0x80(%rbp), %rdi
callq 0x238c
leaq -0xe8(%rbp), %rdi
callq 0x238c
movl -0x38(%rbp), %eax
incl %eax
movl -0x34(%rbp), %r14d
jmp 0x1e2e
addq $0x271f8, %rsp          # imm = 0x271F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2158
jmp 0x2158
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x238c
leaq -0xe8(%rbp), %rdi
callq 0x238c
movq %rbx, %rdi
callq 0x11a0