// File: /workspace/github_cpp_makefile_star2_O3/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 0x6ccc(%rip), %r14      # 0xbfc8
leaq 0x31f4(%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 0x2f82(%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 0x2f64(%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 0x2f3d(%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 0x6be9(%rip), %r15      # 0xbfd8
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2110
leaq 0x313a(%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 0x3113(%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 0x30ed(%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 0x30d9(%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 0x2e49(%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 0x307f(%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 0x306a(%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 0x3056(%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 0x304f(%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 -0x170(%rbp), %rdi
callq 0x4028
xorps %xmm0, %xmm0
cvtsi2sdl (%r13), %xmm0
movl (%rbx), %esi
leaq -0x148(%rbp), %rdi
callq 0x4028
xorps %xmm0, %xmm0
cvtsi2sdl -0x50(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0x120(%rbp), %rdi
callq 0x4028
xorps %xmm0, %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0xf8(%rbp), %rdi
callq 0x4028
xorps %xmm0, %xmm0
cvtsi2sdl -0x50(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0xd0(%rbp), %rdi
callq 0x4028
xorps %xmm0, %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm0
movl -0x40(%rbp), %esi
leaq -0xa8(%rbp), %rdi
callq 0x4028
movslq -0x38(%rbp), %r14
leaq (,%r14,8), %rdi
testq %r14, %r14
movq $-0x1, %r12
cmovsq %r12, %rdi
callq 0x2030
movq %rax, %r15
movslq -0x34(%rbp), %rbx
leaq (,%rbx,8), %rdi
testq %rbx, %rbx
cmovsq %r12, %rdi
callq 0x2030
movq %rax, %r12
movq $0x5, -0x60(%rbp)
movsd -0x80(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
movsd %xmm0, -0x1b0(%rbp)
movsd %xmm1, -0x1a8(%rbp)
movq $0x0, -0x1a0(%rbp)
movb $0x0, -0x198(%rbp)
testl %r14d, %r14d
jle 0x57ed
movq -0x150(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x100(%rbp), %r14
movq -0xb0(%rbp), %r13
xorl %ebx, %ebx
leaq -0x1b0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq %rdi, %rdx
callq 0x766c
movsd %xmm0, (%r15,%rbx,8)
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movq (%r14), %rax
movq %r14, %rdi
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movsd -0x30(%rbp), %xmm0
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
incq %rbx
movslq -0x38(%rbp), %rax
cmpq %rax, %rbx
jl 0x5797
movl -0x34(%rbp), %ebx
movq $0x5, -0x58(%rbp)
movsd -0x70(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
movsd %xmm0, -0x190(%rbp)
movsd %xmm1, -0x188(%rbp)
movq $0x0, -0x180(%rbp)
movb $0x0, -0x178(%rbp)
testl %ebx, %ebx
jle 0x5893
movq -0x128(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xd8(%rbp), %r14
movq -0x88(%rbp), %r13
xorl %ebx, %ebx
leaq -0x190(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movq %rdi, %rdx
callq 0x766c
movsd %xmm0, (%r12,%rbx,8)
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movq (%r14), %rax
movq %r14, %rdi
movsd -0x30(%rbp), %xmm0
callq *(%rax)
movsd -0x30(%rbp), %xmm0
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
incq %rbx
movslq -0x34(%rbp), %rax
cmpq %rax, %rbx
jl 0x5840
movl -0x3c(%rbp), %edx
leaq -0x170(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x48ea
movsd %xmm0, -0x48(%rbp)
movq -0x160(%rbp), %r14
movq -0x158(%rbp), %rbx
xorps %xmm0, %xmm0
cvtsi2sdl -0x34(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl -0x38(%rbp), %xmm1
divsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jb 0x58e0
sqrtsd %xmm0, %xmm6
jmp 0x58ed
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 0x590d
sqrtsd %xmm0, %xmm0
jmp 0x591c
movsd %xmm6, -0x30(%rbp)
callq 0x2160
movsd -0x30(%rbp), %xmm6
movl -0x3c(%rbp), %eax
testl %eax, %eax
jle 0x59e6
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %edx
incq %rax
xorpd %xmm1, %xmm1
movsd %xmm1, -0x30(%rbp)
movl $0x1, %esi
movsd 0x26ca(%rip), %xmm1     # 0x8010
xorpd %xmm2, %xmm2
testl %ecx, %ecx
jle 0x5975
movsd (%r14,%rsi,8), %xmm3
xorl %edi, %edi
movsd (%r15,%rdi,8), %xmm4
ucomisd %xmm4, %xmm3
jb 0x596d
ucomisd (%rbx,%rsi,8), %xmm4
jb 0x596d
addsd %xmm1, %xmm2
incq %rdi
cmpq %rdi, %rcx
jne 0x5956
testl %edx, %edx
jle 0x59a6
movsd (%r14,%rsi,8), %xmm4
xorpd %xmm3, %xmm3
xorl %edi, %edi
movsd (%r12,%rdi,8), %xmm5
ucomisd %xmm5, %xmm4
jb 0x599c
ucomisd (%rbx,%rsi,8), %xmm5
jb 0x599c
addsd %xmm1, %xmm3
incq %rdi
cmpq %rdi, %rdx
jne 0x5985
jmp 0x59aa
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 0x5946
jmp 0x59ef
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x65d2(%rip), %rdi      # 0xbfc8
leaq 0x2cd2(%rip), %rsi      # 0x86cf
movl $0xf, %edx
callq 0x21c0
movq 0x65ba(%rip), %rdi      # 0xbfc8
movsd -0x48(%rbp), %xmm0
callq 0x2220
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x657f(%rip), %rdi      # 0xbfc8
leaq 0x29e5(%rip), %rsi      # 0x8435
movl $0xb, %edx
callq 0x21c0
movq 0x6567(%rip), %rdi      # 0xbfc8
movsd -0x30(%rbp), %xmm0
callq 0x2220
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x652c(%rip), %rdi      # 0xbfc8
leaq 0x2c3c(%rip), %rsi      # 0x86df
movl $0x8, %edx
callq 0x21c0
movl -0x3c(%rbp), %r15d
leaq -0x120(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
movl %r15d, %edx
callq 0x48ea
decl %r15d
movl %r15d, %edi
callq 0x3ba1
movq 0x64ef(%rip), %rdi      # 0xbfc8
callq 0x2220
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x64b9(%rip), %rdi      # 0xbfc8
leaq 0x2bd2(%rip), %rsi      # 0x86e8
movl $0xa, %edx
callq 0x21c0
movl -0x3c(%rbp), %r15d
leaq -0xd0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movl %r15d, %edx
callq 0x48ea
decl %r15d
movl %r15d, %edi
callq 0x3ba1
movsd 0x261b(%rip), %xmm1     # 0x8168
xorl %esi, %esi
ucomisd %xmm0, %xmm1
seta %sil
movq 0x646a(%rip), %rdi      # 0xbfc8
callq 0x2260
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq -0x88(%rbp), %rdi
callq 0x2190
movq -0x98(%rbp), %rdi
callq 0x2190
movq -0x90(%rbp), %rdi
callq 0x2190
movq -0xb0(%rbp), %rdi
callq 0x2190
movq -0xc0(%rbp), %rdi
callq 0x2190
movq -0xb8(%rbp), %rdi
callq 0x2190
movq -0xd8(%rbp), %rdi
callq 0x2190
movq -0xe8(%rbp), %rdi
callq 0x2190
movq -0xe0(%rbp), %rdi
callq 0x2190
movq -0x100(%rbp), %rdi
callq 0x2190
movq -0x110(%rbp), %rdi
callq 0x2190
movq -0x108(%rbp), %rdi
callq 0x2190
movq -0x128(%rbp), %rdi
callq 0x2190
movq -0x138(%rbp), %rdi
callq 0x2190
movq -0x130(%rbp), %rdi
callq 0x2190
movq -0x150(%rbp), %rdi
callq 0x2190
movq %r14, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2190
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5c92
jmp 0x5c92
jmp 0x5c92
movq %rax, %rbx
jmp 0x5ca1
movq %rax, %rbx
jmp 0x5cad
movq %rax, %rbx
jmp 0x5cb9
movq %rax, %rbx
jmp 0x5cc5
movq %rax, %rbx
jmp 0x5cd1
jmp 0x5c92
jmp 0x5c92
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x41c6
leaq -0xd0(%rbp), %rdi
callq 0x41c6
leaq -0xf8(%rbp), %rdi
callq 0x41c6
leaq -0x120(%rbp), %rdi
callq 0x41c6
leaq -0x148(%rbp), %rdi
callq 0x41c6
leaq -0x170(%rbp), %rdi
callq 0x41c6
movq %rbx, %rdi
callq 0x22b0