Math- Small Sample Tests

 


#include <bits/stdc++.h>
using namespace std;
int t_test(void){
    /* Will ask the user for the type of question and will generate the 
     * value of t_calc according to the case
     */
     
     // T-Calculated
     float t_calc=0;
     int choice=0;
     cout<<"T-test for One Sample: 1\n"<<"T-test for 2 Sample(independent): 2\n"<<"T-test for 2 Sample(dependent): 3\n"<<"T-test for Correlation Coefficient: 4\n";
     cout<<"Enter your choice: ";
     int chk_repeat=0;
     foo:
     
     if(chk_repeat==1)cout<<"Shove off boy...\n";
     if(chk_repeat==2)cout<<"What are you doing man? Enter a valid choice!\n";
     if(chk_repeat==3)cout<<"Seriously? Are you soo free? Valid choice?\n";
     if(chk_repeat==4)cout<<"I am out now, you continue...\n";
     cin>>choice;
     cout<<endl;
     
     // Variables for 1st Case
     vector<float> data;
     float t_tab=0,mean=0,q_mean=0,sD=0,n=0,sum=0;
     float sum2=0;
     // Variables for 2nd Case
     vector<float> data2;
    float sum_data2=0,sum1=0;
    float mean_d2=0;
    int n1=0,n2=0;
     switch(choice){
         
         case 1:
        // For One Sample Case
            cout<<"Enter the size of the data: ";
            cin>>n;
            cout<<"Enter the value of given mean in question(meu): ";
            cin>>q_mean;
            cout<<endl<<"Enter the value of t_tab: ";
            cin>>t_tab;
            cout<<endl<<"Now Enter the values of x(space separated):\n";
            for(int i=0;i<n;i++){
                float temp;
                cin>>temp;
                sum+=temp;
                data.push_back(temp);
            }
            // Calculate mean
            
            mean=sum/n;
            cout<<"mean is "<<mean<<endl;
            // Now Standard Deviation
            for(int i=0;i<n;i++){
                sum2+=(data[i]-mean)*(data[i]-mean);   
            }
            sD=sqrt(sum2/(n-1));
            cout<<"sD is "<<sD<<endl;
            // Now will just use the formula of t_calc
            t_calc=((mean-q_mean)/sD)*sqrt(n);
            cout<<"t_calc is "<<t_calc<<endl;
            cout<<((abs(t_calc)<t_tab)?"Null Hypothesis!\n":"Alternate Hypothesis\n");
            return t_calc;
            break;
        case 2:
        // 2 Sample independent
            
            cout<<"Enter the value of n1: ";
            cin>>n1;
            cout<<endl<<"Enter the value of n2: ";
            cin>>n2;
            // cout<<endl;
            // cout<<"Enter the value of given mean in question(meu): ";
            // cin>>q_mean;
            cout<<endl<<"Enter the value of t_tab: ";
            cin>>t_tab;
            cout<<endl<<"Now Enter the data1:\n";
            for(int i=0;i<n1;i++){
                float temp;
                cin>>temp;
                sum+=temp;
                data.push_back(temp);
            }
            mean=sum/n1;
            cout<<"mean1 is "<<mean;
            cout<<endl<<"Now Enter the data2:\n";
            for(int i=0;i<n2;i++){
                float temp;
                cin>>temp;
                sum_data2+=temp;
                data2.push_back(temp);
                // data2[i]=temp;
            }
            // Calculate mean
            
            mean_d2=sum_data2/n2;
            cout<<"mean_d2 is "<<mean_d2<<endl;
            // Now Standard Deviation
            //sum2: summation of square of difference of x2-x2_mean
            for(int i=0;i<n2;i++){
                sum2+=(data2[i]-mean_d2)*(data2[i]-mean_d2);   
            }
            cout<<"sum2 is "<<sum2<<endl;
            // Now for x1
            for(int i=0;i<n1;i++){
                sum1+=(data[i]-mean)*(data[i]-mean);
            }
            cout<<"sum is "<<sum1<<endl;
            // sD=sqrt(sum2/(n-1));
            sD=sqrt((sum1+sum2)/(n1+n2-2));
            cout<<"sD is "<<sD<<endl;
            // Now will just use the formula of t_calc
            // t_calc=((mean-q_mean)/sD)*sqrt(n);
            t_calc=((mean-mean_d2)/sD)*sqrt((n1*n2)/(n1+n2));
            cout<<"t_calc is "<<t_calc<<endl;
            cout<<((abs(t_calc)<t_tab)?"Null Hypothesis!\n":"Alternate Hypothesis\n");
            return t_calc;
            break;
        case 3:
            
             break;
        case 4:
             // Currently I am not handling the case of 
             // Correlation Coefficient
             break;
        default:
            cout<<"Please select a valid choice!\n";
            chk_repeat+=1;
            goto foo;
     }
     return t_calc;
    
}
int main() {
    t_test();
    return 0;
}

Comments

Popular Posts