def p_value(beta_hat_j: float, sigma_hat_j: float) -> float: if beta_hat_j > 0: # if the coefficient is positive, we need to compute twice the # probability of seeing an even *larger* value return 2 * (1 - normal_cdf(beta_hat_j / sigma_hat_j)) else: # otherwise twice the probability of seeing a *smaller* value return 2 * normal_cdf(beta_hat_j / sigma_hat_j)
def p_value(beta_hat_j: float, sigma_hat_j: float) -> float: if beta_hat_j > 0: # Jeżeli współczynnik ma wartość dodatnią, musimy obliczyć dwukrotność # prawdopodobieństwa spotkania „większej” wartości. return 2 * (1 - normal_cdf(beta_hat_j / sigma_hat_j)) else: # W przeciwnym wypadku obliczamy dwukrotność prawdopodobieństwa spotkania „mniejszej” wartości. return 2 * normal_cdf(beta_hat_j / sigma_hat_j)
def normal_probability_between(lo: float, hi: float, mu: float = 0, sigma: float = 1) -> float: return normal_cdf(hi, mu, sigma) - normal_cdf(lo, mu, sigma)
def normal_probability_above(lo: float, mu: float = 0, sigma: float = 1) -> float: return 1 - normal_cdf(lo, mu, sigma)
def binomial_histogram(p: float, n: int, num_points: int) -> None: """Picks points from a Binomial(n, p) and plots their histogram""" data = [binomial(n, p) for _ in range(num_points)] # use a bar chart to show the actual binomial samples histogram = Counter(data) plt.bar([x - 0.4 for x in histogram.keys()], [v / num_points for v in histogram.values()], 0.8, color='0.75') mu = p * n sigma = math.sqrt(n * p * (1 - p)) # use a line chart to show the normal approximation xs = range(min(data), max(data) + 1) ys = [ normal_cdf(i + 0.5, mu, sigma) - normal_cdf(i - 0.5, mu, sigma) for i in xs ] plt.plot(xs, ys) plt.title("Binomial Distribution vs Normal Approximation") plt.show()
def normal_probability_between(lo: float, hi: float, mu: float = 0, sigma: float = 1) -> float: """The probability that a N(mu, sigma) is between lo and hi.""" return normal_cdf(hi, mu, sigma) - normal_cdf(lo, mu, sigma)
def normal_probability_above(lo: float, mu: float = 0, sigma: float = 1) -> float: """The probability that a N(mu, sigma) is greater than lo.""" return 1 - normal_cdf(lo, mu, sigma)
def normal_probability_above(lo: float, mu: float = 0, sigma: float = 1) -> float: """N(mu, sigma)를 따르는 정규 분포가 lo보다 클 확률을 나타내준다.""" return 1 - normal_cdf(lo, mu, sigma)
from matplotlib import pyplot as plt from scratch.probability import normal_cdf xs = [x / 10.0 for x in range(-50, 50)] plt.plot(xs, [normal_cdf(x, sigma=1) for x in xs], '-', label='mu=0,sigma=1') plt.plot(xs, [normal_cdf(x, sigma=2) for x in xs], '-', label='mu=0,sigma=2') plt.plot(xs, [normal_cdf(x, sigma=0.5) for x in xs], '-', label='mu=0,sigma=0.5') plt.plot(xs, [normal_cdf(x, mu=-1) for x in xs], '-', label='mu=-1,sigma=1') plt.legend(loc=4) plt.title("Various Normal cdfs") plt.show()
def normal_probability_between(lo: float, hi: float, mu: float = 0, sigma: float = 1) -> float: """Prawdopodobieństwo tego, że N(mi, sigma) jest pomiędzy lo i hi.""" return normal_cdf(hi, mu, sigma) - normal_cdf(lo, mu, sigma)
def normal_probability_above(lo: float, mu: float = 0, sigma: float = 1) -> float: """Prawdopodobieństwo tego, że N(mi, sigma) jest większe niż lo.""" return 1 - normal_cdf(lo, mu, sigma)
def normal_probability_between(lo: float, hi: float, mu: float = 0, sigma: float = 1): """mu(평균)와 sigma(표준편차)를 따르는 정규분포가 lo와 hi 사이에 있을 확률""" return normal_cdf(hi, mu, sigma) - normal_cdf(lo, mu, sigma)
def normal_probability_above(lo: float, mu: float = 0, sigma: float = 1): """mu(평균)와 sigma(표준편차)를 따르는 정규분포가 lo보다 클 확률""" return 1 - normal_cdf(lo, mu, sigma)
def p_value(beta_hat_j: float, sigma_hat_j: float) -> float: if beta_hat_j > 0: return 2 * (1 - normal_cdf(beta_hat_j / sigma_hat_j)) else: return 2 * normal_cdf(beta_hat_j / sigma_hat_j)