Home

Microsoft Visual C# Example:
The Binomial Probability

     

Introduction

The binomial probability is a measure of the likelihood (probability) of successfully getting a certain of occurrences out of a total number of trials. For example, imagine you have planted peas that produced various offsprings. Also imagine that, from previous tests, you have come to realise that the probability of getting green peas out of your farm is 0.75. Now imagine you plant some peas and they produce five offsprings. You may want to find out the probability of getting three peas.

The formula to calculate the binomial probability is:

Binomial Probability

The factors in this formula are:

Factor Description
n The number of trials
x The number of successful trials among n values.
The values of x go from 0, 1, 2, ..., to n
p The probability of success in a (any one) trial
q The probability of failure in a (any one) trial.
q is considered as q = 1 - p

ApplicationApplication: Creating an Application

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Change the Name to BinomialProbability1
  5. Click OK
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type Exercise.cs and press Enter
  8. Design the form as follows:
     
    Binomial Probability
    Control (Name) Text TextAlign Other Properties
    Label Label   &Number of Trials:    
    TextBox ListBox txtTrials   Right  
    Label Label   Number of &Successes:    
    TextBox Text Box txtSuccesses   Right  
    Label Label   &Probability of Success:    
    TextBox Text Box txtProbabilitySuccess   Right  
    Button Button btnCalculate C&alculate    
    PictureBox PictureBox       BackColor: Black
    Size -> Height: 2
    Label Label   &Binomial Probability:    
    TextBox Text Box txtBinomialProbability   Right  
    Button Button btnClose Close    
  9. Double-click the Calculate button on the form
  10. Change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace BinomialProbability1
    {
        public partial class Exercise : Form
        {
            public Exercise()
            {
                InitializeComponent();
            }
    
            private static long Factorial(long x)
            {
                if (x <= 1)
                    return 1;
                else
                    return x * Factorial(x - 1);
            }
    
            private static long Combination(long a, long b)
            {
                if (a <= 1)
                    return 1;
    
                return Factorial(a) / (Factorial(b) * Factorial(a - b));
            }
    
            private double BinomialProbability(int trials, int successes,
            				   double probabilityOfSuccess)
            {
                double probOfFailures = 1 - probabilityOfSuccess;
    
                double c = Combination(trials, successes);
                double px = Math.Pow(probabilityOfSuccess, successes);
                double qnx = Math.Pow(probOfFailures, trials - successes);
    
                return c * px * qnx;
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                int nbrOfTrials = 0, nbrOfSuccesses = 0;
                double probOfSuccesses = 0.00;
                double binomial = 0.00;
    
                try
                {
                    nbrOfTrials = int.Parse(txtTrials.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("The number of trials is not valid",
                                    "Binomial Probability");
                }
    
                try
                {
                    nbrOfSuccesses  = int.Parse(txtSuccesses.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("The number of successes is not valid",
                                    "Binomial Probability");
                }
    
                try
                {
                    probOfSuccesses = double.Parse(txtProbabilitySuccess.Text);
    
                    binomial = BinomialProbability(nbrOfTrials,
                     			       nbrOfSuccesses,
                     			       probOfSuccesses);
                    txtBinomialProbability.Text = binomial.ToString();
                }
                catch (FormatException)
                {
                    MessageBox.Show("The number of trials is not valid",
                                    "Binomial Probability");
                }
            }
        }
    }
  11. Return to the form and double-click the Close button
  12. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  13. Return to the form
  14. To execute, press F5
  15. Enter some values and click Calculate
     
    Binomial Probability
  16. Close the form and return to your programming environment
 

Home Copyright © 2010-2016, FunctionX