Pages

Sunday, December 11, 2011

Hypothesis Testing advanced

I have written basic article - http://quantfinanceindia.blogspot.com/2011/06/distributions-hypothesis-testing.html

Hypothesis testing is used to test the validity of data at certain confidence interval and then to use that to make decisions. There is step wise procedure to d othe hypothesis testing , below are the main steps-
  1. Establish the decision creteria and choose null hypothesis. (Ho)
  2. Decision criteria is usually taken as alternative hypothesis (H1)
  3. Calculate Z scor using the sample data provided.
  4. Make decision using sample z score and test type (can be one tail or two tail)
  5. Share your finding
Examples
1. Two tail test
At edible sweet packaging unit, If i want to test that mean weight of population is equal to 500g then i will use two tail sample test. First establish the hypothesis
Ho Mean of Population is 500g
H1 Mean is not equal to 500g
At 99% confidence interval we will find out p value for the test by calculating z score which nothing but sample mean/std deviation
In case P value is .07, then we will say there is not enough evidence that mean of population is different from 500g and we cannot reject null hypothesis
In case P value is .001 then we will say at 99% there is enough evidence to reject null hypothesis and accept the alternative hypothesis that mean is different from 500g.
Obvious Fact – Mean need to be significantly different from the population mean value for us to establish that it falls in the rejection region.

2. One tail test
In a manufacturing company if there is 3% error in every product they manufacture, to find out if the mean error of the population is less than 3% we will use one tail test..
Ho Mean is 3%
H1 Mean is Less than 3%
This will be left tail test where we will take sample mean and standard deviation to find z score for the distribution and then calculate p value.
In case P value is .78 then we can say there is not enough evidence to reject null hypothesis and we cannot reject null hypothesis
In case P value is .002 then we can reject the null hypothesis that mean is 3% and we can say the mean is less than 3%.
Obvious fact –  Sample mean need to significantly lower than population mean for it to fall in rejection region and have low Z-score value.

Implementatino in C++ :

In case you need help to compile programs in UNIX take a look at my guide on running C++ codes in UNIX machine in g++.@ http://quantfinanceindia.blogspot.com/2010/01/unix-for-quants.html
Copy of the code

#include // for normal_distribution
using boost::math::normal; // typedef provides default type is double.

#include
using std::cout; using std::endl; using std::left; using std::showpoint; using std::noshowpoint;
#include
using std::setw; using std::setprecision;
#include
using std::numeric_limits;
#include

using namespace std;
int main()
{

int mean,std,pvalue;
int testchoice,conf;
int z=0;
cout << "\n\n\n Hypothesis Testing - Using Normal Distribution";

try
{
{
int precision = 17; // traditional tables are only computed to much lower precision.

normal s; // (default mean = zero, and standard deviation = unity)
cout.precision(5);
cout.precision(6); // default

cout <<"\n Enter the mean value of the Sample"<
cin>>mean;

cout << "\n Enter Std Deviation for Sample"<
cin>>std;
z=0;
z = mean/std;


cout<<" \n Please use the following menu to Enter the hypothesis you want to test"<
cout<<"\n 1. to validate if population mean is equal to sample mean"
"\n 2. to test if population mean is less than sample mean"

"\n 3. to find if population mean is more than sample mean"<
cin>>testchoice;

cout<<" Enter the confidence interval you want to test for"<
cout<< "\n 1. for 90% confidence interval"
"\n 2. for 95% confidence interval"

"\n 3. for 99% confidence interval"<
cin>>conf;
if( testchoice == 1 && conf ==1 )
{
if (pvalue < .05 or pvalue > .95)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 1 && conf ==2 )
{
if (pvalue < .10)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 1 && conf ==3 )
{ if (pvalue > .90)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
if( testchoice == 2 && conf ==1 )
{
if (pvalue < .025 or pvalue > .975)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 2 && conf ==2 )
{
if (pvalue < .05 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 2 && conf ==3 )
{
if(pvalue > .95 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
if( testchoice == 3 && conf ==1 )
{
if (pvalue < .005 or pvalue > .995)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 3 && conf ==2 )
{
if (pvalue < .01)
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}
else if( testchoice == 3 && conf ==3 )
{
if (pvalue > .01 )
cout <<" \n Null Hypothesis not rejected as there is not enough statistical evidence";
else
cout<<"\n Null Hypothesis rejected and Alternative Hypothesis accepted";
}


cout<


}
}

catch(const std::exception& e)
{
std::cout <<
"\n""Message from thrown exception was:\n " << e.what() << std::endl;
}
return 0;

}

Boost
It is c++ library for the implementation of many utilities, we will be utilizing mathematical distribution library for calculation of p value. Installation of boos can be done following below steps -
    2. unzip the file in /usr/include/ directort using cp or tar commands
    3. Go inside the folder and run ./bootstrap.sh
    4. /b2 install
working of the code
1. Code will ask you the mean of the sample you want to use and then the standard deviation of the sample
2. Next ask will be the test you want to conduct, are you looking of one tail or two tail test (equality of the mean or less than or greater than relation)
3. Confidence interval you want to choose
4. Result will shown as if Null Hypothesis is accepted or not and what is the result.
Code is stored [at] -
Output of the c++ code for hypothesis testing is
Hypothesis Testing - Using Normal Distribution
Enter the mean value of the Sample
3

Enter Std Deviation for Sample
3
Please use the following menu to Enter the hypothesis you want to test

1. to validate if population mean is equal to sample mean
2. to test if population mean is less than sample mean
3. to find if population mean is more than sample mean
1
Enter the confidence interval you want to test for

1. for 90% confidence interval
2. for 95% confidence interval
3. for 99% confidence interval
1
Null Hypothesis not rejected as there is not enough statistical evidence

No comments:

Post a Comment