משתנים אקראיים בינומיים

משתנים אקראיים בינומיים

בפוסט זה נדון במשתנים אקראיים בינומיים.
תנאי מוקדם: משתנים אקראיים 
סוג מסוים של נִבדָל משתנה אקראי שסופר באיזו תדירות מתרחש אירוע מסוים במספר קבוע של ניסיונות או ניסויים. 
כדי שמשתנה יהיה משתנה אקראי בינומי יש לעמוד בכל התנאים הבאים: 
 

  1. יש מספר קבוע של ניסויים (גודל מדגם קבוע).
  2. בכל משפט אירוע העניין מתרחש או לא.
  3. ההסתברות להתרחשות (או לא) זהה בכל ניסוי.
  4. הניסויים אינם תלויים זה בזה.


סימונים מתמטיים 
 

 n = number of trials   
p = probability of success in each trial
k = number of success in n trials


כעת אנו מנסים לגלות את ההסתברות להצלחה של k ב-n ניסויים.
כאן ההסתברות להצלחה בכל ניסוי היא p בלתי תלויה בניסויים אחרים. 
אז אנחנו קודם כל בוחרים ב-k ניסויים שבהם תהיה הצלחה ובשאר n-k נסיונות יהיה כישלון. מספר הדרכים לעשות זאת הוא 
 

משתנים אקראיים בינומיים


מכיוון שכל n האירועים אינם תלויים, לכן ההסתברות להצלחה של k ב-n ניסויים שווה ערך להכפלת ההסתברות עבור כל ניסוי.
כאן K ההצלחה שלה וכישלונות n-k אז ההסתברות לכל דרך להשיג K הצלחה וכישלון n-k היא 
 

משתנים אקראיים בינומיים


מכאן שההסתברות הסופית היא 
 

 (number of ways to achieve k success   
and n-k failures)
*
(probability for each way to achieve k
success and n-k failure)


אז הסתברות משתנה אקראי בינומי ניתנת על ידי: 
 

משתנים אקראיים בינומיים


תן X להיות משתנה אקראי בינומי עם מספר הניסויים n והסתברות ההצלחה בכל ניסוי תהיה p. 
מספר ההצלחה הצפוי ניתן על ידי 
 

 E[X] = np  


שונות של מספר ההצלחה ניתנת על ידי 
 

 Var[X] = np(1-p)  


דוגמה 1 : שקול ניסוי אקראי שבו מטבע מוטה (הסתברות לראש = 1/3) נזרק במשך 10 פעמים. מצא את ההסתברות שמספר הראשים שיופיעו יהיה 5.
פתרון: 
 

 Let X be binomial random variable    
with n = 10 and p = 1/3
P(X=5) = ? משתנים אקראיים בינומיים
     משתנים אקראיים בינומיים 
    

הנה היישום לאותו הדבר 
 

C++
   // C++ program to compute Binomial Probability   #include          #include         using     namespace     std  ;   // function to calculate nCr i.e. number of    // ways to choose r out of n objects   int     nCr  (  int     n       int     r  )   {      // Since nCr is same as nC(n-r)      // To decrease number of iterations      if     (  r     >     n     /     2  )      r     =     n     -     r  ;      int     answer     =     1  ;      for     (  int     i     =     1  ;     i      <=     r  ;     i  ++  )     {      answer     *=     (  n     -     r     +     i  );      answer     /=     i  ;      }      return     answer  ;   }   // function to calculate binomial r.v. probability   float     binomialProbability  (  int     n       int     k       float     p  )   {      return     nCr  (  n       k  )     *     pow  (  p       k  )     *      pow  (  1     -     p       n     -     k  );   }   // Driver code   int     main  ()   {      int     n     =     10  ;      int     k     =     5  ;      float     p     =     1.0     /     3  ;      float     probability     =     binomialProbability  (  n       k       p  );      cout      < <     'Probability of '      < <     k  ;      cout      < <     ' heads when a coin is tossed '      < <     n  ;      cout      < <     ' times where probability of each head is '      < <     p      < <     endl  ;      cout      < <     ' is = '      < <     probability      < <     endl  ;   }   
Java
   // Java program to compute Binomial Probability   import     java.util.*  ;   class   GFG   {      // function to calculate nCr i.e. number of       // ways to choose r out of n objects      static     int     nCr  (  int     n       int     r  )      {      // Since nCr is same as nC(n-r)      // To decrease number of iterations      if     (  r     >     n     /     2  )      r     =     n     -     r  ;          int     answer     =     1  ;      for     (  int     i     =     1  ;     i      <=     r  ;     i  ++  )     {      answer     *=     (  n     -     r     +     i  );      answer     /=     i  ;      }          return     answer  ;      }          // function to calculate binomial r.v. probability      static     float     binomialProbability  (  int     n       int     k       float     p  )      {      return     nCr  (  n       k  )     *     (  float  )  Math  .  pow  (  p       k  )     *         (  float  )  Math  .  pow  (  1     -     p       n     -     k  );      }          // Driver code      public     static     void     main  (  String  []     args  )      {      int     n     =     10  ;      int     k     =     5  ;      float     p     =     (  float  )  1.0     /     3  ;          float     probability     =     binomialProbability  (  n       k       p  );          System  .  out  .  print  (  'Probability of '     +  k  );      System  .  out  .  print  (  ' heads when a coin is tossed '     +  n  );      System  .  out  .  println  (  ' times where probability of each head is '     +  p  );      System  .  out  .  println  (     ' is = '     +     probability     );      }   }   /* This code is contributed by Mr. Somesh Awasthi */   
Python3
   # Python3 program to compute Binomial    # Probability   # function to calculate nCr i.e.   # number of ways to choose r out   # of n objects   def   nCr  (  n     r  ):   # Since nCr is same as nC(n-r)   # To decrease number of iterations   if   (  r   >   n   /   2  ):   r   =   n   -   r  ;   answer   =   1  ;   for   i   in   range  (  1     r   +   1  ):   answer   *=   (  n   -   r   +   i  );   answer   /=   i  ;   return   answer  ;   # function to calculate binomial r.v.   # probability   def   binomialProbability  (  n     k     p  ):   return   (  nCr  (  n     k  )   *   pow  (  p     k  )   *   pow  (  1   -   p     n   -   k  ));   # Driver code   n   =   10  ;   k   =   5  ;   p   =   1.0   /   3  ;   probability   =   binomialProbability  (  n     k     p  );   print  (  'Probability of'     k     'heads when a coin is tossed'     end   =   ' '  );   print  (  n     'times where probability of each head is'     round  (  p     6  ));   print  (  'is = '     round  (  probability     6  ));   # This code is contributed by mits   
C#
   // C# program to compute Binomial   // Probability.   using     System  ;   class     GFG     {          // function to calculate nCr      // i.e. number of ways to       // choose r out of n objects      static     int     nCr  (  int     n       int     r  )      {          // Since nCr is same as      // nC(n-r) To decrease       // number of iterations      if     (  r     >     n     /     2  )      r     =     n     -     r  ;          int     answer     =     1  ;      for     (  int     i     =     1  ;     i      <=     r  ;     i  ++  )      {      answer     *=     (  n     -     r     +     i  );      answer     /=     i  ;      }          return     answer  ;      }          // function to calculate binomial      // r.v. probability      static     float     binomialProbability  (      int     n       int     k       float     p  )      {      return     nCr  (  n       k  )     *         (  float  )  Math  .  Pow  (  p       k  )      *     (  float  )  Math  .  Pow  (  1     -     p        n     -     k  );      }          // Driver code      public     static     void     Main  ()      {      int     n     =     10  ;      int     k     =     5  ;      float     p     =     (  float  )  1.0     /     3  ;          float     probability     =         binomialProbability  (  n       k       p  );          Console  .  Write  (  'Probability of '      +     k  );      Console  .  Write  (  ' heads when a coin '      +     'is tossed '     +     n  );      Console  .  Write  (  ' times where '      +     'probability of each head is '      +     p  );      Console  .  Write  (     ' is = '      +     probability     );      }   }   // This code is contributed by nitin mittal.   
JavaScript
    <  script  >   // Javascript program to compute Binomial Probability      // function to calculate nCr i.e. number of       // ways to choose r out of n objects      function     nCr  (  n       r  )      {      // Since nCr is same as nC(n-r)      // To decrease number of iterations      if     (  r     >     n     /     2  )      r     =     n     -     r  ;          let     answer     =     1  ;      for     (  let     i     =     1  ;     i      <=     r  ;     i  ++  )     {      answer     *=     (  n     -     r     +     i  );      answer     /=     i  ;      }          return     answer  ;      }          // function to calculate binomial r.v. probability      function     binomialProbability  (  n       k       p  )      {      return     nCr  (  n       k  )     *     Math  .  pow  (  p       k  )     *         Math  .  pow  (  1     -     p       n     -     k  );      }       // driver program      let     n     =     10  ;      let     k     =     5  ;      let     p     =     1.0     /     3  ;          let     probability     =     binomialProbability  (  n       k       p  );          document  .  write  (  'Probability of '     +  k  );      document  .  write  (  ' heads when a coin is tossed '     +  n  );      document  .  write  (  ' times where probability of each head is '     +  p  );      document  .  write  (     ' is = '     +     probability     );          // This code is contributed by code_hunt.    <  /script>   
PHP
      // php program to compute Binomial    // Probability   // function to calculate nCr i.e.   // number of ways to choose r out   // of n objects   function   nCr  (  $n     $r  )   {   // Since nCr is same as nC(n-r)   // To decrease number of iterations   if   (  $r   >   $n   /   2  )   $r   =   $n   -   $r  ;   $answer   =   1  ;   for   (  $i   =   1  ;   $i    <=   $r  ;   $i  ++  )   {   $answer   *=   (  $n   -   $r   +   $i  );   $answer   /=   $i  ;   }   return   $answer  ;   }   // function to calculate binomial r.v.   // probability   function   binomialProbability  (  $n     $k     $p  )   {   return   nCr  (  $n     $k  )   *   pow  (  $p     $k  )   *   pow  (  1   -   $p     $n   -   $k  );   }   // Driver code   $n   =   10  ;   $k   =   5  ;   $p   =   1.0   /   3  ;   $probability   =   binomialProbability  (  $n     $k     $p  );   echo   'Probability of '   .   $k  ;   echo   ' heads when a coin is tossed '   .   $n  ;   echo   ' times where probability of '   .   'each head is '   .   $p   ;   echo   ' is = '   .   $probability   ;   // This code is contributed by nitin mittal.   ?>   

תְפוּקָה:  
 

 Probability of 5 heads when a coin is tossed 10 times where probability of each head is 0.333333   
is = 0.136565


 

צור חידון