Shawn Brar
Shawn Brar

Reputation: 1420

Calculate p value of a t - statistic using the student_t_distribution

I wanted to calculate p-values of a t-statistic for a two tailed test with 5% level of significance. And I wanted to do this with the standard library. I was wondering if this was possible using the student_t_distribution from the < random > module. My code currently is as following

#include <iostream>

int main(){
    double t_stat = 0.0267; // t-statistic
    double alpha_los = 0.05; // level of significance
    double dof = 30; // degrees of freedom
    // calculate P > |t| and compare with alpha_los
    return 0;
}

Thank you

Upvotes: 0

Views: 1017

Answers (1)

Unlikus
Unlikus

Reputation: 1858

The <random> header just provides you with the ability to get random numbers from different distributions.

If you are able to use boost you can do the following:

#include <boost/math/distributions/students_t.hpp>

int main() {
    double t_stat = 0.0267; // t-statistic
    double alpha_los = 0.05; // level of significance
    double dof = 30; // degrees of freedom
    boost::math::students_t dist(dof);

    double P_x_greater_t = 1.0 - boost::math::cdf(dist, t_stat);
    double P_x_smaller_negative_t = boost::math::cdf(dist, -t_stat);

    if(P_x_greater_t + P_x_smaller_negative_t < alpha_los) {
        
    } else {
        
    }
}

Upvotes: 1

Related Questions