2018年10月31日,老文章 exercise 1 recall that n!n! is read as "nn factorial" and defined as n!=n×(n−1)×⋯×2×1n!=n×(n−1)×⋯×2×1 there are f nctions to comp te this in vario s mod les, b t let s write o r own version as an exercise in partic lar, write a f nction factorial s ch that factorial(n) ret rns n!n! for any positive integer n # -*- coding: tf-8 -*-  created on t e oct 16 23:01:45 2018 @a thor: wengsway  def factorial(n): if n  2: ret rn 1 else: ret rn n*factorial(n-1) n = int(inp t( please inp t any positive integer: )) print(factorial(n)) exercise 2 the binomial random variable y∼bin(n,p)y∼bin(n,p) represents the n mber of s ccesses in nn binary trials, where each trial s cceeds with probability pp witho t any import besides from n mpy.random import niform, write a f nction binomial_rv s ch that binomial_rv(n, p) generates one draw of yy hint: if uu is niform on (0,1)(0,1) and p∈(0,1)p∈(0,1), then the expression u  p eval ates to tr e with probability p # -*- coding: tf-8 -*-  created on wed oct 17 18:26:42 2018 @a thor: wengsway  import n mpy as np def factorial(n): if n  2: ret rn 1 else: ret rn n*factorial(n-1) = np.random.normal(0,1) n = int(inp t("please inp t the n val e:")) def binomial_rv(n,p): y = p**n * (1-p)**(n-n) * factorial(n)(factorial(n) * factorial(n-n)) ret rn y n = int(inp t("please inp t the n val e:")) p = float(inp t("please inp t the p val e:")) print(binomial_rv(n,p)) exercise 3 comp te an approximation to ππ sing monte carlo. use no imports besides import n mpy as np yo r hints are as follows: if is a bivariate niform random variable on the nit sq are , then the probability that lies in a s bset of is eq al to the area of if u1..,un are iid copies of , then, as gets large, the fraction that fall in converges to the probability of landing in for a circle, area = pi * radi s^2 # -*- coding: tf-8 -*-  created on wed oct 17 10:23:25 2018 @a thor: wengsway  import n mpy as np freq ency = 0 n mbers = int(inp t("please inp t the n mber of times:")) for i in range(1, n mbers): x, y = np.random. niform(0,1), np.random. niform(0,1) area = np.sqrt(x**2 + y**2) if area = 1.0: freq ency = freq ency + 1 pi = 4 * (freq encyn mbers) print(pi) exercise 4 write a program that prints one realization of the following random device: flip an nbiased coin 10 times if 3 consec tive heads occ r one or more times within this seq ence, pay one dollar if not, pay nothing use no import besides from n mpy.random import niform # -*- coding: tf-8 -*-  created on wed oct 17 12:25:01 2018 @a thor: wengsway  from n mpy.random import randint x = randint(0,2,10).tolist j = 0 for i in range(len(x)-2): if x[i] == 1 and x[i+1] == 1 and x[i+2] == 1: j = j + 1 if j = 1: print("yo pay one dollar!") else: print("yo pay nothing!