// File: 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
subq $0x260, %rsp            # imm = 0x260
movl $0x0, -0x4(%rbp)
movabsq $0x4024000000000000, %rax # imm = 0x4024000000000000
movq %rax, -0x10(%rbp)
movl $0xf4240, -0x14(%rbp)   # imm = 0xF4240
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl $0x2710, -0x1c(%rbp)    # imm = 0x2710
movl -0x1c(%rbp), %eax
decl %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1e0(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1d8(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl -0x1c(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1d0(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1c8(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movsd 0x3b53(%rip), %xmm0     # 0x6128
movsd %xmm0, -0x58(%rbp)
movsd 0x3b3e(%rip), %xmm0     # 0x6120
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x26ce
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd %xmm0, -0x1f0(%rbp)
movl -0x64(%rbp), %edi
addl $0x1, %edi
movsd -0x58(%rbp), %xmm0
callq 0x3380
movaps %xmm0, %xmm1
movsd -0x1f0(%rbp), %xmm0
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x68(%rbp)
movl $0x1, -0x6c(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x3340
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movl (%rcx), %ecx
addl -0x4c(%rbp), %ecx
movl %ecx, -0x4c(%rbp)
movl -0x4c(%rbp), %edx
movslq -0x64(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd %xmm0, -0x1e8(%rbp)
movl -0x64(%rbp), %edi
addl $0x1, %edi
movsd -0x60(%rbp), %xmm0
callq 0x3380
movaps %xmm0, %xmm1
movsd -0x1e8(%rbp), %xmm0
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x70(%rbp)
movl $0x1, -0x74(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x3340
movq %rax, %rcx
movq -0x1d8(%rbp), %rax
movl (%rcx), %ecx
addl -0x50(%rbp), %ecx
movl %ecx, -0x50(%rbp)
movl -0x50(%rbp), %edx
movslq -0x64(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x25ee
movl $0x0, -0x78(%rbp)
cmpl $0xa, -0x78(%rbp)
jge 0x2c4e
movsd -0x10(%rbp), %xmm0
leaq -0xe8(%rbp), %rdi
callq 0x1360
movsd -0x10(%rbp), %xmm0
leaq -0x150(%rbp), %rdi
callq 0x1360
jmp 0x2703
xorps %xmm0, %xmm0
movsd %xmm0, -0x168(%rbp)
movb $0x1, %al
movsd 0x3a18(%rip), %xmm0     # 0x6130
ucomisd -0x168(%rbp), %xmm0
movb %al, -0x1f1(%rbp)
ja 0x2745
movsd -0x168(%rbp), %xmm0
movsd 0x3a00(%rip), %xmm1     # 0x6138
ucomisd %xmm1, %xmm0
seta %al
movb %al, -0x1f1(%rbp)
movb -0x1f1(%rbp), %al
testb $0x1, %al
jne 0x2754
jmp 0x29b9
movq -0x1d0(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl $0x1, -0x16c(%rbp)
leaq -0x16c(%rbp), %rdx
callq 0x33b0
jmp 0x2776
movq -0x1c8(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl $0x1, -0x170(%rbp)
leaq -0x170(%rbp), %rdx
callq 0x33b0
jmp 0x2798
movl -0x4(%rbp), %edi
callq 0x10e0
movl $0x0, -0x174(%rbp)
movl -0x174(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2824
movq -0x1e0(%rbp), %rsi
movl -0x1c(%rbp), %edi
callq 0x2490
movl %eax, %ecx
movq -0x1d0(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x27aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
jmp 0x2c40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x3400
jmp 0x2c40
movl $0x0, -0x178(%rbp)
movl -0x178(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x286e
movq -0x1d8(%rbp), %rsi
movl -0x1c(%rbp), %edi
callq 0x2490
movl %eax, %ecx
movq -0x1c8(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x178(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x178(%rbp)
jmp 0x282e
cvtsi2sdl -0x18(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x10a0
movsd %xmm0, -0x180(%rbp)
cvtsi2sdl -0x14(%rbp), %xmm0
cvtsi2sdl -0x18(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x10a0
movsd %xmm0, -0x188(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0x18c(%rbp)
movl -0x18c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2984
movq -0x1c8(%rbp), %rax
movq -0x1d0(%rbp), %rcx
movslq -0x18c(%rbp), %rdx
cvtsi2sdl (%rcx,%rdx,4), %xmm0
movsd %xmm0, -0x198(%rbp)
movslq -0x18c(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0x198(%rbp), %xmm0
movsd -0x180(%rbp), %xmm2
movsd -0x1a0(%rbp), %xmm1
mulsd -0x188(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x1a8(%rbp), %xmm0
mulsd -0x1a8(%rbp), %xmm0
movsd -0x198(%rbp), %xmm1
addsd -0x1a0(%rbp), %xmm1
divsd %xmm1, %xmm0
addsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0x18c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18c(%rbp)
jmp 0x28b6
movsd -0x80(%rbp), %xmm0
movl -0x20(%rbp), %edi
callq 0x1e40
movsd %xmm0, -0x200(%rbp)
jmp 0x299b
movsd -0x200(%rbp), %xmm0
movsd %xmm0, -0x168(%rbp)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x270e
movl $0x0, -0x1ac(%rbp)
movl -0x1ac(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2a80
movl $0x0, -0x1b0(%rbp)
movq -0x1d0(%rbp), %rcx
movl -0x1b0(%rbp), %eax
movslq -0x1ac(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x2a1e
cvtsi2sdl -0x1ac(%rbp), %xmm0
leaq -0xe8(%rbp), %rdi
callq 0x1400
jmp 0x2a0b
jmp 0x2a0d
movl -0x1b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b0(%rbp)
jmp 0x29dc
movl $0x0, -0x1b4(%rbp)
movq -0x1c8(%rbp), %rcx
movl -0x1b4(%rbp), %eax
movslq -0x1ac(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x2a6a
cvtsi2sdl -0x1ac(%rbp), %xmm0
leaq -0x150(%rbp), %rdi
callq 0x1400
jmp 0x2a57
jmp 0x2a59
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x2a28
jmp 0x2a6c
movl -0x1ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ac(%rbp)
jmp 0x29c3
leaq -0xe8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
xorl %edx, %edx
callq 0x1510
movsd %xmm0, -0x208(%rbp)
jmp 0x2a9f
movsd -0x208(%rbp), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movq 0x7522(%rip), %rdi      # 0x9fd8
leaq 0x3722(%rip), %rsi      # 0x61df
callq 0x10f0
movq %rax, -0x210(%rbp)
jmp 0x2acb
movq -0x210(%rbp), %rdi
movsd -0x80(%rbp), %xmm0
callq 0x11b0
movq %rax, -0x218(%rbp)
jmp 0x2ae5
movq -0x218(%rbp), %rdi
movq 0x74d5(%rip), %rsi      # 0x9fc8
callq 0x1110
jmp 0x2afa
movq 0x74d7(%rip), %rdi      # 0x9fd8
leaq 0x36e4(%rip), %rsi      # 0x61ec
callq 0x10f0
movq %rax, -0x220(%rbp)
jmp 0x2b16
movq -0x220(%rbp), %rdi
movsd -0x1c0(%rbp), %xmm0
callq 0x11b0
movq %rax, -0x228(%rbp)
jmp 0x2b33
movq -0x228(%rbp), %rdi
movq 0x7487(%rip), %rsi      # 0x9fc8
callq 0x1110
jmp 0x2b48
movq 0x7489(%rip), %rdi      # 0x9fd8
leaq 0x36a3(%rip), %rsi      # 0x61f9
callq 0x10f0
movq %rax, -0x230(%rbp)
jmp 0x2b64
movq -0x230(%rbp), %rdi
movsd -0x168(%rbp), %xmm0
callq 0x11b0
movq %rax, -0x238(%rbp)
jmp 0x2b81
movq -0x238(%rbp), %rdi
movq 0x7439(%rip), %rsi      # 0x9fc8
callq 0x1110
jmp 0x2b96
movq 0x743b(%rip), %rdi      # 0x9fd8
leaq 0x3662(%rip), %rsi      # 0x6206
callq 0x10f0
movq %rax, -0x240(%rbp)
jmp 0x2bb2
movsd -0x1c0(%rbp), %xmm0
movl -0x20(%rbp), %edi
callq 0x1e40
movsd %xmm0, -0x248(%rbp)
jmp 0x2bcc
movsd -0x248(%rbp), %xmm0
movq -0x240(%rbp), %rdi
callq 0x11b0
movq %rax, -0x250(%rbp)
jmp 0x2be9
movq -0x250(%rbp), %rdi
movq 0x73d1(%rip), %rsi      # 0x9fc8
callq 0x1110
movq %rax, -0x258(%rbp)
jmp 0x2c05
movq -0x258(%rbp), %rdi
movq 0x73b5(%rip), %rsi      # 0x9fc8
callq 0x1110
jmp 0x2c1a
leaq -0x150(%rbp), %rdi
callq 0x3400
leaq -0xe8(%rbp), %rdi
callq 0x3400
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x26d5
leaq -0xe8(%rbp), %rdi
callq 0x3400
jmp 0x2c5a
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x158(%rbp), %rdi
callq 0x1190
nopw %cs:(%rax,%rax)