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