LFD Book Forum

LFD Book Forum (http://book.caltech.edu/bookforum/index.php)
-   Homework 2 (http://book.caltech.edu/bookforum/forumdisplay.php?f=131)
-   -   HW2 Q1 (generating a binomial distribution) (http://book.caltech.edu/bookforum/showthread.php?t=4737)

vahuja4 02-02-2017 01:43 AM

HW2 Q1 (generating a binomial distribution)
Hi All,

Here is the code I wrote for hw2_q1:

from random import randint
import seaborn as sns
import numpy as np

N = 100000
v_first = np.zeros(N)
v_min = np.zeros(N)
v_rand = np.zeros(N)
#Loop starts

for i in range(N):

n,p = 10, 0.5
flips = np.random.binomial(n, p, 1000)

first_coin = 0
min_coin = np.argmin(flips)
rand_coin = np.random.randint(0,1000)

v_first[i] = flips[first_coin]
v_min[i] = flips[min_coin]
v_rand[i] = flips[rand_coin]

#Loop ends

print np.average(v_min)

My answer turns out to be 0.367, while the correct answer is 0.01. Could someone please point out where I am going wrong?

willgao 06-08-2017 08:18 AM

Re: HW2 Q1 (generating a binomial distribution)

nu is the fraction, not the number of the head count.

All times are GMT -7. The time now is 06:10 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.