// File: /workspace/github_cpp_makefile_star2_O1/ashlall[P]StreamStats/Test/Chi_Square_test.cpp
// Function: test_chi_square_2nd()
void test_chi_square_2nd() //Two-sample test
{
 	int sketch_method = 1;
  	cout << "Enter the number 1-3 to choose sketch method that you want to use: "<<endl;
  	cout << "1. Greenwald sketch/default." <<endl;
  	cout << "2. Q-Digest sketch." <<endl;
  	cout << "3. Reservoir Sampling sketch." <<endl;
  	cin >> sketch_method;

	int stream_size1;
	int stream_size2;
  	cout << "Enter the first stream's size: "<<endl;
  	cin >> stream_size1;
	cout << "Enter the second stream's size: "<<endl;
	cin >> stream_size2;

  	int sample_size1;
	int sample_size2;
  	cout << "Enter the memory capacity for the first sample size: "<<endl;
 	cin >> sample_size1;
  	cout << "Enter the memory capacity for the second sample size: "<<endl;
 	cin >> sample_size2;
  
	int k;
 	cout << "Enter the number of bins you want to use: " <<endl;
 	cin >> k;

	double mean1, SD1;
	double mean2, SD2;
	cout << "Enter the mean of the first normal distribution: "<<endl;
	cin >> mean1;
	cout << "Enter the mean of the second normal distribution: "<<endl;
	cin >> mean2;
 	cout << "Enter the standard deviation of the first normal distribution: "<<endl;
	cin >> SD1;
 	cout << "Enter the standard deviation of the second normal distribution: "<<endl;
	cin >> SD2;

	ChiSquareContinuous c1(sample_size1, sketch_method), c2(sample_size2, sketch_method); 
	ChiSquareContinuous c3(sample_size1, sketch_method), c4(sample_size2, sketch_method); 
	ChiSquareContinuous c5(sample_size1, sketch_method), c6(sample_size2, sketch_method);

	double *items1=new double[stream_size1]; //used for storing all the generated data for calculating the actual chi^2.
	double *items2=new double[stream_size2];
	double data1;
	double data2;
	default_random_engine generator(5);
	normal_distribution<double> distribution(mean1,SD1);

	for (int i=0; i<stream_size1; i++) 
  	{
   		data1 = distribution(generator);
		items1[i] = data1;
  		c1.insert(data1);
		c3.insert(data1);
		c5.insert(data1);
 	}

	default_random_engine generator2(5);
	normal_distribution<double> distribution2(mean2,SD2);
 	for (int i = 0; i < stream_size2; i++)
  	{
		data2 = distribution2(generator2);
		items2[i] = data2;
		c2.insert(data2);
		c4.insert(data2);
		c6.insert(data2);
 	}

	double chiEestimated;
        chiEestimated = c1.two_sample_statistic(c2, k);
   	double chiActual=0;

   	double *Upper=c1.get_upper();
   	double *Lower=c1.get_lower();
	double constant_1 = sqrt((double)stream_size2/stream_size1);
 	double constant_2 = sqrt((double)stream_size1/stream_size2);

   	/*timeval timeBefore, timeAfter;
   	long diffSeconds, diffUSeconds;
   	gettimeofday(&timeBefore, NULL);*/
   	for (int i=1;i<=k;i++)
   	{	
    		double E=0;
        	for(int j=0;j<stream_size1;j++)
    	    	{
    	    		if(( items1[j]<=Upper[i]) && (items1[j]>=Lower[i]))
		        	E++;
    	    	}
	
    		double O=0;
        	for(int j=0;j<stream_size2;j++)
    	    	{
    	    		if(( items2[j]<=Upper[i]) && (items2[j]>=Lower[i]))
		        	O++;
    	    	}
                //cout <<"actual S_i and R_i: " << E << " " << O<< endl;
	
      	    	double lambda= E*constant_1 - O*constant_2;
	 
	    	//cout<<"i: "<< i << " Expected: "<<E << " "<<"actual:" << O <<endl;

    	   	chiActual=chiActual+((lambda*lambda)/(E+O)); 	    
    	}	
    /*gettimeofday(&timeAfter, NULL);
    diffSeconds = timeAfter.tv_sec - timeBefore.tv_sec;
    diffUSeconds = timeAfter.tv_usec - timeBefore.tv_usec;
    cout << "1st way: " << diffSeconds + diffUSeconds/1000000.0 << endl;

    gettimeofday(&timeBefore, NULL);
    int *frequencies = get_frequencies(Upper, Lower, items, k, stream_size);
    for (int i = 1; i <= k; i++)
      { 
	chiActual2 += (frequencies[i] - E) * (frequencies[i] - E)/E;
	}*/
    /*gettimeofday(&timeAfter, NULL);
    diffSeconds= timeAfter.tv_sec - timeBefore.tv_sec;
    diffUSeconds = timeAfter.tv_usec - timeBefore.tv_usec;
    cout << "2nd way: "<< diffSeconds + diffUSeconds/1000000.0<<" seconds\n";*/
  	cout << "chiEestimated: " << chiEestimated << endl;
	cout << "chiActual: " << chiActual << endl;
	cout << "pvalue: "<<c3.two_sample_pvalue(c4,k) <<endl;
	cout << "decision: "<<c5.two_sample_final_decision(c6,k,0.05) <<endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
leaq -0x40(%rbp), %rbx
movl $0x1, (%rbx)
movq 0x6b6d(%rip), %r14      # 0xbfc8
leaq 0x3095(%rip), %rsi      # 0x84f7
movl $0x43, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x2e23(%rip), %rsi      # 0x82c0
movl $0x1c, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x2e05(%rip), %rsi      # 0x82dd
movl $0x13, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x2dde(%rip), %rsi      # 0x82f1
movl $0x1d, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x6a8a(%rip), %r15      # 0xbfd8
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2110
leaq 0x2fdb(%rip), %rsi      # 0x853b
movl $0x1f, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x38(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
leaq 0x2fb4(%rip), %rsi      # 0x855b
movl $0x20, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x34(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
leaq 0x2f8e(%rip), %rsi      # 0x857c
movl $0x35, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x50(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2110
leaq 0x2f7a(%rip), %rsi      # 0x85b2
movl $0x36, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x4c(%rbp), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x2110
leaq 0x2cea(%rip), %rsi      # 0x836c
movl $0x2a, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x3c(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
leaq 0x2f20(%rip), %rsi      # 0x85e9
movl $0x31, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x80(%rbp), %rsi
movq %r15, %rdi
callq 0x2120
leaq 0x2f0b(%rip), %rsi      # 0x861b
movl $0x32, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x70(%rbp), %rsi
movq %r15, %rdi
callq 0x2120
leaq 0x2ef7(%rip), %rsi      # 0x864e
movl $0x3f, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x78(%rbp), %rsi
movq %r15, %rdi
callq 0x2120
leaq 0x2ef0(%rip), %rsi      # 0x868e
movl $0x40, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x68(%rbp), %rsi
movq %r15, %rdi
callq 0x2120
cvtsi2sdl (%r12), %xmm0
movl (%rbx), %esi
leaq -0xa8(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl (%r13), %xmm0
movl (%rbx), %esi
leaq -0x1b0(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl -0x50(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0x188(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0x160(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl -0x50(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0x138(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x41aa
movslq -0x38(%rbp), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %r14
cmovsq %r14, %rdi
callq 0x2030
movq %rax, %rbx
movslq -0x34(%rbp), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r14, %rdi
callq 0x2030
movq %rax, %r14
movq $0x5, -0x60(%rbp)
movsd -0x80(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
movsd %xmm0, -0xe8(%rbp)
movsd %xmm1, -0xe0(%rbp)
movq $0x0, -0xd8(%rbp)
movb $0x0, -0xd0(%rbp)
cmpl $0x0, -0x38(%rbp)
jle 0x594c
movq -0x88(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x168(%rbp), %r12
movq -0x118(%rbp), %r13
xorl %r15d, %r15d
leaq -0xe8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq %rdi, %rdx
callq 0x7780
movsd %xmm0, (%rbx,%r15,8)
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movq (%r12), %rax
movq %r12, %rdi
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movsd -0x30(%rbp), %xmm0
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
incq %r15
movslq -0x38(%rbp), %rax
cmpq %rax, %r15
jl 0x58f8
movq $0x5, -0x58(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
movsd %xmm0, -0xc8(%rbp)
movsd %xmm1, -0xc0(%rbp)
movq $0x0, -0xb8(%rbp)
movb $0x0, -0xb0(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x59f6
movq -0x190(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x140(%rbp), %r12
movq -0xf0(%rbp), %r13
xorl %r15d, %r15d
leaq -0xc8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movq %rdi, %rdx
callq 0x7780
movsd %xmm0, (%r14,%r15,8)
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movq (%r12), %rax
movq %r12, %rdi
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movsd -0x30(%rbp), %xmm0
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
incq %r15
movslq -0x34(%rbp), %rax
cmpq %rax, %r15
jl 0x59a2
movl -0x3c(%rbp), %edx
leaq -0xa8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x4a86
movsd %xmm0, -0x48(%rbp)
movq -0x98(%rbp), %r15
movq -0x90(%rbp), %r12
xorps %xmm0, %xmm0
cvtsi2sdl -0x34(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl -0x38(%rbp), %xmm1
divsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jb 0x5a43
sqrtsd %xmm0, %xmm6
jmp 0x5a50
callq 0x2160
xorpd %xmm2, %xmm2
movapd %xmm0, %xmm6
xorps %xmm0, %xmm0
cvtsi2sdl -0x38(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl -0x34(%rbp), %xmm1
divsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x5a70
sqrtsd %xmm0, %xmm0
jmp 0x5a7f
movsd %xmm6, -0x30(%rbp)
callq 0x2160
movsd -0x30(%rbp), %xmm6
movl -0x3c(%rbp), %eax
testl %eax, %eax
jle 0x5b4a
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %edx
incq %rax
xorpd %xmm1, %xmm1
movsd %xmm1, -0x30(%rbp)
movl $0x1, %esi
movsd 0x2567(%rip), %xmm1     # 0x8010
xorpd %xmm2, %xmm2
testl %ecx, %ecx
jle 0x5ad8
movsd (%r15,%rsi,8), %xmm3
xorl %edi, %edi
movsd (%rbx,%rdi,8), %xmm4
ucomisd %xmm4, %xmm3
jb 0x5ad0
ucomisd (%r12,%rsi,8), %xmm4
jb 0x5ad0
addsd %xmm1, %xmm2
incq %rdi
cmpq %rdi, %rcx
jne 0x5ab9
testl %edx, %edx
jle 0x5b0a
movsd (%r15,%rsi,8), %xmm4
xorpd %xmm3, %xmm3
xorl %edi, %edi
movsd (%r14,%rdi,8), %xmm5
ucomisd %xmm5, %xmm4
jb 0x5b00
ucomisd (%r12,%rsi,8), %xmm5
jb 0x5b00
addsd %xmm1, %xmm3
incq %rdi
cmpq %rdi, %rdx
jne 0x5ae8
jmp 0x5b0e
xorpd %xmm3, %xmm3
movapd %xmm0, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm2, %xmm5
mulsd %xmm6, %xmm5
subsd %xmm4, %xmm5
mulsd %xmm5, %xmm5
addsd %xmm3, %xmm2
divsd %xmm2, %xmm5
movsd -0x30(%rbp), %xmm2
addsd %xmm5, %xmm2
movsd %xmm2, -0x30(%rbp)
incq %rsi
cmpq %rax, %rsi
jne 0x5aa9
jmp 0x5b53
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x646e(%rip), %rdi      # 0xbfc8
leaq 0x2b6e(%rip), %rsi      # 0x86cf
movl $0xf, %edx
callq 0x21c0
movq 0x6456(%rip), %rdi      # 0xbfc8
movsd -0x48(%rbp), %xmm0
callq 0x2220
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x641b(%rip), %rdi      # 0xbfc8
leaq 0x2881(%rip), %rsi      # 0x8435
movl $0xb, %edx
callq 0x21c0
movq 0x6403(%rip), %rdi      # 0xbfc8
movsd -0x30(%rbp), %xmm0
callq 0x2220
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x63c8(%rip), %rdi      # 0xbfc8
leaq 0x2ad8(%rip), %rsi      # 0x86df
movl $0x8, %edx
callq 0x21c0
movl -0x3c(%rbp), %ebx
leaq -0x188(%rbp), %rdi
leaq -0x160(%rbp), %rsi
movl %ebx, %edx
callq 0x4a86
decl %ebx
movl %ebx, %edi
callq 0x3d24
movq 0x638f(%rip), %rdi      # 0xbfc8
callq 0x2220
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x6359(%rip), %rdi      # 0xbfc8
leaq 0x2a72(%rip), %rsi      # 0x86e8
movl $0xa, %edx
callq 0x21c0
movl -0x3c(%rbp), %ebx
leaq -0x138(%rbp), %rdi
leaq -0x110(%rbp), %rsi
movl %ebx, %edx
callq 0x4a86
decl %ebx
movl %ebx, %edi
callq 0x3d24
movsd 0x24bf(%rip), %xmm1     # 0x8168
xorl %esi, %esi
ucomisd %xmm0, %xmm1
seta %sil
movq 0x630e(%rip), %rdi      # 0xbfc8
callq 0x2260
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x110(%rbp), %rdi
callq 0x434a
leaq -0x138(%rbp), %rdi
callq 0x434a
leaq -0x160(%rbp), %rdi
callq 0x434a
leaq -0x188(%rbp), %rdi
callq 0x434a
leaq -0x1b0(%rbp), %rdi
callq 0x434a
leaq -0xa8(%rbp), %rdi
callq 0x434a
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5d66
jmp 0x5d66
jmp 0x5d66
movq %rax, %rbx
jmp 0x5d75
movq %rax, %rbx
jmp 0x5d81
movq %rax, %rbx
jmp 0x5d8d
movq %rax, %rbx
jmp 0x5d99
movq %rax, %rbx
jmp 0x5da5
jmp 0x5d66
jmp 0x5d66
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x434a
leaq -0x138(%rbp), %rdi
callq 0x434a
leaq -0x160(%rbp), %rdi
callq 0x434a
leaq -0x188(%rbp), %rdi
callq 0x434a
leaq -0x1b0(%rbp), %rdi
callq 0x434a
leaq -0xa8(%rbp), %rdi
callq 0x434a
movq %rbx, %rdi
callq 0x22b0