Approximate Normal Distribution PDF in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to approximate the Normal Distribution Probability Density Function (PDF) in C. We will first read the input values for the point (x), mean (μ), and standard deviation (σ), then compute the PDF using the standard formula. Finally, we will print the calculated PDF value.

The lab covers the step-by-step process of implementing this functionality in C, including reading user input, computing the PDF, and displaying the result. This lab aims to provide a practical understanding of working with probability and combinatorics concepts in C programming.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435338{{"`Approximate Normal Distribution PDF in C`"}} c/variables -.-> lab-435338{{"`Approximate Normal Distribution PDF in C`"}} c/operators -.-> lab-435338{{"`Approximate Normal Distribution PDF in C`"}} c/if_else -.-> lab-435338{{"`Approximate Normal Distribution PDF in C`"}} c/user_input -.-> lab-435338{{"`Approximate Normal Distribution PDF in C`"}} c/math_functions -.-> lab-435338{{"`Approximate Normal Distribution PDF in C`"}} end

Read x, mean μ, std dev σ

In this step, we'll learn how to read input values for calculating the Normal Distribution Probability Density Function (PDF) in C. We'll create a program that takes three key parameters: x (the point), μ (mean), and σ (standard deviation).

First, let's create a new C file for our implementation:

cd ~/project
nano normal_pdf.c

Now, let's write the initial code to read input values:

#include <stdio.h>

int main() {
    double x, mean, std_dev;

    printf("Enter the point x: ");
    scanf("%lf", &x);

    printf("Enter the mean (μ): ");
    scanf("%lf", &mean);

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

    printf("Input values:\n");
    printf("x = %.2f\n", x);
    printf("Mean (μ) = %.2f\n", mean);
    printf("Standard Deviation (σ) = %.2f\n", std_dev);

    return 0;
}

Compile and run the program:

gcc normal_pdf.c -o normal_pdf
./normal_pdf

Example output:

Enter the point x: 2.5
Enter the mean (μ): 0
Enter the standard deviation (σ): 1
Input values:
x = 2.50
Mean (μ) = 0.00
Standard Deviation (σ) = 1.00

This code demonstrates how to:

  1. Use scanf() to read double-precision floating-point numbers
  2. Store input values for x, mean, and standard deviation
  3. Print the input values to confirm correct input

Compute PDF = (1/(σ√(2π))) * exp(-((x-μ)²/(2σ²)))

In this step, we'll extend our previous program to compute the Probability Density Function (PDF) for the Normal Distribution using the mathematical formula.

Update the normal_pdf.c file:

nano ~/project/normal_pdf.c

Replace the previous content with:

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

double compute_normal_pdf(double x, double mean, double std_dev) {
    // Compute PDF using the standard normal distribution formula
    double pi = 3.14159265358979323846;
    double coefficient = 1.0 / (std_dev * sqrt(2 * pi));
    double exponent = -pow(x - mean, 2) / (2 * pow(std_dev, 2));

    return coefficient * exp(exponent);
}

int main() {
    double x, mean, std_dev, pdf;

    printf("Enter the point x: ");
    scanf("%lf", &x);

    printf("Enter the mean (μ): ");
    scanf("%lf", &mean);

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

    // Compute PDF
    pdf = compute_normal_pdf(x, mean, std_dev);

    printf("Input values:\n");
    printf("x = %.2f\n", x);
    printf("Mean (μ) = %.2f\n", mean);
    printf("Standard Deviation (σ) = %.2f\n", std_dev);
    printf("Probability Density = %.6f\n", pdf);

    return 0;
}

Compile the program with math library:

gcc ~/project/normal_pdf.c -o ~/project/normal_pdf -lm

Run the program:

~/project/normal_pdf

Example output:

Enter the point x: 2.5
Enter the mean (μ): 0
Enter the standard deviation (σ): 1
Input values:
x = 2.50
Mean (μ) = 0.00
Standard Deviation (σ) = 1.00
Probability Density = 0.017528

Key points in this implementation:

  1. We created a separate function compute_normal_pdf() to calculate the PDF
  2. Used math.h library functions like sqrt(), exp(), and pow()
  3. Implemented the standard normal distribution PDF formula
  4. Printed the computed probability density

Print the Value

In this final step, we'll enhance our Normal Distribution PDF program by adding more detailed output formatting and visualization options.

Update the normal_pdf.c file:

nano ~/project/normal_pdf.c

Replace the previous content with:

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

double compute_normal_pdf(double x, double mean, double std_dev) {
    double pi = 3.14159265358979323846;
    double coefficient = 1.0 / (std_dev * sqrt(2 * pi));
    double exponent = -pow(x - mean, 2) / (2 * pow(std_dev, 2));

    return coefficient * exp(exponent);
}

void print_pdf_details(double x, double mean, double std_dev, double pdf) {
    printf("\n--- Normal Distribution PDF Details ---\n");
    printf("Point (x)               : %.2f\n", x);
    printf("Mean (μ)               : %.2f\n", mean);
    printf("Standard Deviation (σ)  : %.2f\n", std_dev);
    printf("Probability Density (f(x)): %.6f\n", pdf);

    // Add interpretation of the PDF value
    if (pdf > 0.3) {
        printf("Interpretation: High probability density\n");
    } else if (pdf > 0.1) {
        printf("Interpretation: Moderate probability density\n");
    } else {
        printf("Interpretation: Low probability density\n");
    }
}

int main() {
    double x, mean, std_dev, pdf;

    printf("Normal Distribution Probability Density Function Calculator\n");
    printf("Enter the point x: ");
    scanf("%lf", &x);

    printf("Enter the mean (μ): ");
    scanf("%lf", &mean);

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

    // Compute PDF
    pdf = compute_normal_pdf(x, mean, std_dev);

    // Print detailed results
    print_pdf_details(x, mean, std_dev, pdf);

    return 0;
}

Compile the program:

gcc ~/project/normal_pdf.c -o ~/project/normal_pdf -lm

Run the program:

~/project/normal_pdf

Example output:

Normal Distribution Probability Density Function Calculator
Enter the point x: 2.5
Enter the mean (μ): 0
Enter the standard deviation (σ): 1

--- Normal Distribution PDF Details ---
Point (x)               : 2.50
Mean (μ)               : 0.00
Standard Deviation (σ)  : 1.00
Probability Density (f(x)): 0.017528
Interpretation: Low probability density

Key enhancements in this implementation:

  1. Added a dedicated function print_pdf_details() for formatted output
  2. Included a simple interpretation of the PDF value
  3. Improved user interface with a descriptive title
  4. Maintained the core PDF calculation logic from previous steps

Summary

In this lab, we learned how to read input values for the Normal Distribution Probability Density Function (PDF) in C, including the point x, mean μ, and standard deviation σ. We then implemented the mathematical formula to compute the PDF, which involves calculating the coefficient and exponent terms. The final step is to print the computed PDF value.

The key learning points from this lab are: 1) using scanf() to read double-precision floating-point numbers, 2) storing input values for x, mean, and standard deviation, 3) computing the PDF using the standard normal distribution formula, and 4) printing the resulting PDF value.

Other C Tutorials you may like