/** * \brief Runs a one-sample Z test on a vector of numbers. * \param[in] values The vector of numbers. * \param[in] distributionMean The mean of the entire population. * \param[in] distributionStandardDeviation the standard deviation of the entire population. * \param[in] The confidence level (commonly used values: 0.95, 0.999) * \param[in] testCase The test case containing the hypothesis and null hypothesis chosen in germanStudentsTest() */ void StatisticalTesting::oneSampleZTest(const std::vector<double>& values, const double& distributionMean, const double& distributionStandardDeviation, const double& confidenceLevel, const TestCase& testCase) { /** * TODO: Execute the Z test for the given vector of numbers and either * reject the null hypothesis or state that you cannot reject the null hypothesis. */ /* * Available methods: * - lookupZTable(double Z): returns the cumulative density function * of the standard normal distribution at Z (see slide 23 for examples). * - testCase.getHypothesis(): returns the hypothesis (see germanStudentsTest() above) * - testCase.getNullHypothesis(): returns the null hypothesis (see germanStudentsTest() above) * * For both the hypothesis and the null hypothesis, the following methods are available: * - hypothesis.getDirection(): Returns one element from the following enumeration: * LESS, GREATER, AT_LEAST, AT_MOST, EQUAL, or DIFFERENT. * - hypothesis.reject(): Rejects the hypothesis. * - hypothesis.cannotReject(): States that we cannot reject the hypothesis based on the data. */ double zscore = zScore(values,distributionMean,distributionStandardDeviation); double zlookup = lookupZTable(zscore); const Hypothesis& h1 = testCase.getHypothesis(); const Hypothesis& h0 = testCase.getNullHypothesis(); double zconfidence = 0; normal s; /* If greater or lesser the area of significance is on one side of curve. so we use quantile with same * confidence level. Else we can divide alpha by 2 for two sided test * */ if (h0.getDirection()==AT_MOST) { zconfidence = confidenceLevel; if (zlookup>zconfidence) h0.reject(); else { h0.cannotReject(); } } else { if(h0.getDirection()==AT_LEAST) { zconfidence = (1-confidenceLevel); if (zlookup<zconfidence) h0.reject(); else { h0.cannotReject(); } } else { double left_boundry = (1-confidenceLevel); double right_boundry = confidenceLevel; if((zlookup<left_boundry)||(zlookup>right_boundry)) h0.reject(); else { h0.cannotReject(); } } } }
/** * \brief Runs a one-sample t test on a vector of numbers. * \param[in] values The vector of numbers. * \param[in] distributionMean The mean of the entire population. * \param[in] The confidence level (commonly used values: 0.95, 0.999) * \param[in] testCase The test case containing the hypothesis and null hypothesis chosen in germanStudentsTest() */ void StatisticalTesting::oneSampleTTest(const std::vector<double>& values, const double& distributionMean, const double& confidenceLevel, const TestCase& testCase) { /** * TODO: Execute the one sample T test for the given vector of numbers and either * reject the null hypothesis or state that you cannot reject the null hypothesis. */ /* * Available methods: * - lookupTTable(size_t degreeOfFreedom, double confidenceLevel, TestType testType): * returns the quantile function of the student's t distribution (see slide * of the standard normal distribution at Z (see slide 29 for examples). * testType can be either ONE_SIDED or TWO_SIDED. * - testCase.getHypothesis(): returns the hypothesis (see germanStudentsTest() above) * - testCase.getNullHypothesis(): returns the null hypothesis (see germanStudentsTest() above) * * For both the hypothesis and the null hypothesis, the following methods are available: * - hypothesis.getDirection(): Returns one element from the following enumeration: * LESS, GREATER, AT_LEAST, AT_MOST, EQUAL, or DIFFERENT. * - hypothesis.reject(): Rejects the hypothesis. * - hypothesis.cannotReject(): States that we cannot reject the hypothesis based on the data. */ size_t dof = values.size() -1; double tscore = tValue(values,distributionMean); double tlookup ; const Hypothesis& h1 = testCase.getHypothesis(); const Hypothesis& h0 = testCase.getNullHypothesis(); /* If greater or lesser the area of significance is on one side of curve. so we use quantile with same * confidence level. Else we can divide alpha by 2 for two sided test * */ if (h0.getDirection()==AT_MOST) { tlookup = lookupTTable(dof,confidenceLevel,StatisticalTesting::ONE_SIDED); if (tscore >tlookup ) h0.reject(); else { h0.cannotReject(); } } else { if(h0.getDirection()==AT_LEAST) { tlookup = lookupTTable(dof,(1-confidenceLevel),StatisticalTesting::ONE_SIDED); if (tscore < tlookup ) h0.reject(); else { h0.cannotReject(); } } else { double right_boundry = lookupTTable(dof,confidenceLevel,StatisticalTesting::ONE_SIDED); double left_boundry = lookupTTable(dof,(1-confidenceLevel),StatisticalTesting::ONE_SIDED); if((tscore<left_boundry)||(tscore>right_boundry)) h0.reject(); else { h0.cannotReject(); } } } }