Compute a Cumulative Distribution Function (CDF) in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the Cumulative Distribution Function (CDF) in C. The lab covers two main steps: reading distribution parameters and the x-value, and then computing the CDF by summing probabilities from negative infinity to the given x-value using the standard normal distribution. The lab provides the complete code implementation and guides you through the process step-by-step, ensuring you have a solid understanding of the CDF calculation in C.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435339{{"`Compute a Cumulative Distribution Function (CDF) in C`"}} c/variables -.-> lab-435339{{"`Compute a Cumulative Distribution Function (CDF) in C`"}} c/user_input -.-> lab-435339{{"`Compute a Cumulative Distribution Function (CDF) in C`"}} c/math_functions -.-> lab-435339{{"`Compute a Cumulative Distribution Function (CDF) in C`"}} end

Read Distribution Parameters and x

In this step, you will learn how to read distribution parameters and x-value for calculating the Cumulative Distribution Function (CDF) in C.

First, let's create a new C file to implement our CDF calculation:

cd ~/project
nano cdf_calculator.c

Now, add the following code to read distribution parameters:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Distribution parameters
    double mean, std_dev;
    double x_value;

    // Prompt user for distribution parameters
    printf("Enter the mean (μ): ");
    scanf("%lf", &mean);

    printf("Enter the standard deviation (σ): ");
    scanf("%lf", &std_dev);

    // Prompt user for x value
    printf("Enter the x value to compute CDF: ");
    scanf("%lf", &x_value);

    // Print input parameters for verification
    printf("\nInput Parameters:\n");
    printf("Mean (μ): %.2f\n", mean);
    printf("Standard Deviation (σ): %.2f\n", std_dev);
    printf("X Value: %.2f\n", x_value);

    return 0;
}

Compile and run the program:

gcc cdf_calculator.c -o cdf_calculator
./cdf_calculator

Example output:

Enter the mean (μ): 5.0
Enter the standard deviation (σ): 2.0
Enter the x value to compute CDF: 3.5

Input Parameters:
Mean (μ): 5.00
Standard Deviation (σ): 2.00
X Value: 3.50

Sum Probabilities from -∞ to x

In this step, you will learn how to compute the Cumulative Distribution Function (CDF) by summing probabilities from negative infinity to a given x-value using the standard normal distribution.

Let's modify the previous C program to implement the CDF calculation:

cd ~/project
nano cdf_calculator.c

Replace the previous code with the following implementation:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Standard Normal CDF approximation function (Abramowitz and Stegun)
double standard_normal_cdf(double x) {
    const double a1 =  0.254829592;
    const double a2 = -0.284496736;
    const double a3 =  1.421413741;
    const double a4 = -1.453152027;
    const double a5 =  1.061405429;
    const double p  =  0.3275911;

    // Handle negative values
    int sign = (x < 0) ? -1 : 1;
    x = fabs(x);

    // Approximation formula
    double t = 1.0 / (1.0 + p * x);
    double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * exp(-x * x);

    return 0.5 * (1.0 + sign * y);
}

// Compute CDF for normal distribution
double normal_cdf(double x, double mean, double std_dev) {
    // Z-score calculation
    double z_score = (x - mean) / std_dev;
    return standard_normal_cdf(z_score);
}

int main() {
    // Distribution parameters
    double mean, std_dev;
    double x_value;

    // Prompt user for distribution parameters
    printf("Enter the mean (μ): ");
    scanf("%lf", &mean);

    printf("Enter the standard deviation (σ): ");
    scanf("%lf", &std_dev);

    // Prompt user for x value
    printf("Enter the x value to compute CDF: ");
    scanf("%lf", &x_value);

    // Calculate and print CDF
    double cdf_value = normal_cdf(x_value, mean, std_dev);

    printf("\nCDF Calculation Results:\n");
    printf("Mean (μ): %.2f\n", mean);
    printf("Standard Deviation (σ): %.2f\n", std_dev);
    printf("X Value: %.2f\n", x_value);
    printf("CDF P(X ≤ x): %.4f\n", cdf_value);

    return 0;
}

Compile the program with math library:

gcc cdf_calculator.c -o cdf_calculator -lm

Run the program and test with sample inputs:

./cdf_calculator

Example output:

Enter the mean (μ): 5.0
Enter the standard deviation (σ): 2.0
Enter the x value to compute CDF: 3.5

CDF Calculation Results:
Mean (μ): 5.00
Standard Deviation (σ): 2.00
X Value: 3.50
CDF P(X ≤ x): 0.2525

Print the CDF Value

In this step, you will learn how to enhance the CDF calculation program by adding more detailed output and interpretation of the Cumulative Distribution Function (CDF) results.

Let's modify the previous C program to improve the output and add some interpretative comments:

cd ~/project
nano cdf_calculator.c

Update the main() function to include more detailed output:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Previous standard_normal_cdf and normal_cdf functions remain the same

int main() {
    // Distribution parameters
    double mean, std_dev;
    double x_value;

    // Prompt user for distribution parameters
    printf("Cumulative Distribution Function (CDF) Calculator\n");
    printf("-----------------------------------------------\n");
    printf("Enter the mean (μ): ");
    scanf("%lf", &mean);

    printf("Enter the standard deviation (σ): ");
    scanf("%lf", &std_dev);

    // Prompt user for x value
    printf("Enter the x value to compute CDF: ");
    scanf("%lf", &x_value);

    // Calculate and print CDF
    double cdf_value = normal_cdf(x_value, mean, std_dev);

    // Detailed output with interpretation
    printf("\n--- CDF Calculation Results ---\n");
    printf("Mean (μ):             %.2f\n", mean);
    printf("Standard Deviation (σ): %.2f\n", std_dev);
    printf("X Value:              %.2f\n", x_value);
    printf("CDF P(X ≤ x):         %.4f (%.2f%%)\n",
           cdf_value, cdf_value * 100);

    // Interpret the CDF value
    printf("\nInterpretation:\n");
    if (cdf_value < 0.5) {
        printf("The value is below the mean distribution.\n");
    } else if (cdf_value > 0.5) {
        printf("The value is above the mean distribution.\n");
    } else {
        printf("The value is at the mean of the distribution.\n");
    }

    // Probability explanation
    printf("Probability of observing a value less than or equal to %.2f: %.2f%%\n",
           x_value, cdf_value * 100);

    return 0;
}

Compile the program:

gcc cdf_calculator.c -o cdf_calculator -lm

Run the program with sample inputs:

./cdf_calculator

Example output:

Cumulative Distribution Function (CDF) Calculator
-----------------------------------------------
Enter the mean (μ): 5.0
Enter the standard deviation (σ): 2.0
Enter the x value to compute CDF: 3.5

--- CDF Calculation Results ---
Mean (μ):             5.00
Standard Deviation (σ): 2.00
X Value:              3.50
CDF P(X ≤ x):         0.2525 (25.25%)

Interpretation:
The value is below the mean distribution.
Probability of observing a value less than or equal to 3.50: 25.25%

Summary

In this lab, you learned how to read distribution parameters (mean and standard deviation) and an x-value to compute the Cumulative Distribution Function (CDF) in C. You then implemented the CDF calculation by summing probabilities from negative infinity to the given x-value using the standard normal distribution approximation function. Finally, you printed the CDF value for the provided input parameters.

The key learning points from this lab are the steps required to compute the CDF, including reading the necessary input data and applying the standard normal CDF approximation formula to calculate the desired CDF value.

Other C Tutorials you may like