We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 4: Binomial Distribution I
Day 4: Binomial Distribution I
+ 0 comments Basic Python 3 Code:
# Enter your code here. Read input from STDIN. Print output to STDOUT import math data = input() boy, girl = data.split(" ") pb=float(boy)/(float(girl)+float(boy)) pg=1-pb n=6 #x is the number of boys that are 3 and up to 6 x=[3,4,5,6] def fact(n): return math.factorial(n) answer=0 for probx in x: chose = (fact(n))/(fact(probx)*fact(n-probx)) total = chose*(pb**probx)*(pg**(n-probx)) answer = answer + total print(round(answer,3))
+ 0 comments Python 3 Code :
def fact(n): if n == 0: return 1 else: return n * fact(n - 1) def comb(n, x): return (fact(n) // (fact(x) * fact(n - x))) def binomial(x, n, p): return comb(n, x) * (p**x) * ((1 - p)**(n - x)) l, r = list(map(float, input().split(" "))) odds = l / r print(round(sum([binomial(i, 6, odds / (1 + odds)) for i in range(3, 7)]), 3))
+ 0 comments 1) Just calculate p & q from given ratio of p / q, as by logic p + q = 1. (try to write it with pen & paper for better understanding) 2) By cal_nCr function calculating 6C3, 6C4, 6C5, 6C6. 3) For final answer using standard formula of Binomial Distribution.
Code :
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <iomanip> using namespace std; double cal_nCr(int n, int r){ double nCr = 1; for(int i = 1; i <= r; i++){ nCr = nCr * (n - r + i) / i; } return nCr; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ double m, n; cin >> m >> n; double p = m / (m + n); double q = 1 - p; double ans = 0; for(int i = 3; i <= 6; i++){ double x = cal_nCr(6, i); ans = ans + (x) * pow(p, i) * pow(q, 6 - i); } cout << fixed << setprecision(3) << ans; return 0; }
+ 0 comments from math import comb as c x, n, b, g = 3, 6, 1.09, 1 p, q = b/(b+g), g/(b+g) print(f'{sum(map(lambda x:c(n,x)*p**x*q**(n-x),range(3,7))):.3f}')
+ 0 comments JS
function range(a,b) { let prd = a,i = a; while (i++< b) { prd*=i; } return prd; } function comb(n, r) { if (n==r || r==0) return 1; else { r=(r < n-r) ? n-r : r; return range(r+1, n)/range(1,n-r); } } let value = input.split(' '); let p = parseFloat(value[0])/(parseFloat(value[1]) + parseFloat(value[0])); let q = 1-p, b =[]; for(let i = 3; i <= 6; i++) { let f = comb(6, i) * Math.pow(p, i) * Math.pow(q, (6 - i)); b.push(f); } console.log((b.reduce((sum, a) => sum + a, 0)).toFixed(3));
Load more conversations
Sort 213 Discussions, By:
Please Login in order to post a comment