def npFactorial(input_arr): ret = np.array([]) for i in input_arr: ret = np.append(ret, smath.factorial(i)) return ret
def posterior(x, n, p1, p2): """ Calculates the posterior probability that the probability of developing severe side effects falls within a specific range given the data: x is the number of patients that develop severe side effects n is the total number of patients observed p1 is the lower bound on the range p2 is the upper bound on the range You can assume the prior beliefs of p follow a uniform distribution If n is not a positive integer, raise a ValueError with the message n must be a positive integer If x is not an integer that is greater than or equal to 0, raise a ValueError with the message x must be an integer that is greater than or equal to 0 If x is greater than n, raise a ValueError with the message x cannot be greater than n If p1 or p2 are not floats within the range [0, 1], raise a ValueError with the message {p} must be a float in the range [0, 1] where {p} is the corresponding variable if p2 <= p1, raise a ValueError with the message p2 must be greater than p1 The only import you are allowed to use is from scipy import special Returns: the posterior probability that p is within the range [p1, p2] given x and n """ if not isinstance(n, int) or n <= 0: raise ValueError("n must be a positive integer") if not isinstance(x, (int, float)) or (x < 0): mg = "x must be an integer that is greater than or equal to 0" raise ValueError(mg) if x > n: raise ValueError("x cannot be greater than n") if not isinstance(p1, float) or p1 < 0 or p1 > 1: raise ValueError("p1 must be a float in the range [0, 1]") if not isinstance(p2, float) or p2 < 0 or p2 > 1: raise ValueError("p2 must be a float in the range [0, 1]") if p2 <= p1: raise ValueError("p2 must be greater than p1") num = (math.factorial(n+1)) den = (math.factorial(x) * math.factorial(n - x)) fct = num / den hp1 = special.hyp2f1(x+1, x-n, x+2, p1) hp2 = special.hyp2f1(x+1, x-n, x+2, p2) return fct * (((p2**(x+1))*hp2)-((p1**(x+1))*hp1))/(x+1)
def posterior(x, n, p1, p2): """ calculates the posterior probability that the probability of developing severe side effects falls within a specific range given the data - x is the number of patients that develop severe side effects - n is the total number of patients observed - p1 is the lower bound on the range - p2 is the upper bound on the range the prior beliefs of p follow a uniform distribution If n is not a positive integer, raise a ValueError with the message: n must be a positive integer If x is not an integer that is greater than or equal to 0, raise a ValueError with the message: x must be an integer that is greater than or equal to 0 If x is greater than n, raise a ValueError with the message: x cannot be greater than n If p1 or p2 are not floats within the range [0, 1], raise a ValueError with the message: "{p} must be a float in the range [0, 1]" where {p} is the corresponding variable if p2 <= p1, raise a ValueError with the message: "p2 must be greater than p1" """ if not isinstance(n, int) or n < 1: raise ValueError("n must be a positive integer") if type(x) != int or x < 0: mg = "x must be an integer that is greater than or equal to 0" raise ValueError(mg) if x > n: raise ValueError("x cannot be greater than n") if not isinstance(p1, float) or p1 < 0 or p1 > 1: raise ValueError("p1 must be a float in the range [0, 1]") if not isinstance(p2, float) or p2 < 0 or p2 > 1: raise ValueError("p2 must be a float in the range [0, 1]") if p2 <= p1: raise ValueError("p2 must be greater than p1") y = n - x f = math.factorial(n + 1) / (math.factorial(x) * math.factorial(y)) h_1 = special.hyp2f1(x + 1, x - n, x + 2, p1) h_2 = special.hyp2f1(x + 1, x - n, x + 2, p2) posterior = f * (((p2**(x + 1)) * h2) - ((p1**(x + 1)) * h1)) / (x + 1) return posterior
def prob_poisson(n, mu): if mu <= 0 or n < 0: return 0 else: p = 1 for i in range(0, len(n)): nn = int(n[i]) p *= (mu**nn) * math.exp(-mu) / math.factorial(nn) return p
def primitive_Function(x, y, n=0): """Return values of the primitive function (dt. 'Stammfunktion'), see eq. 39""" #x, y must be scalar k = np.arange(n + 1) #index variable ret = (-1.) * ( smath.factorial(n) / npFactorial(n - k) * x**(n - k) / y**(k + 2.) * np.cos(x * y + k * np.pi / 2.)).sum() #sum over k from 0 to n return ret
def posterior(x, n, p1, p2): """https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.beta. html Ross p 147""" if type(n) is not int or n <= 0: raise ValueError("n must be a positive integer") if type(x) is not int or x < 0: error = "x must be an integer that is greater than or equal to 0" raise ValueError(error) if x > n: raise ValueError("x cannot be greater than n") if type(p1) is not float or p1 < 0 or p1 > 1: raise ValueError("p1 must be a float in the range [0, 1]") if type(p2) is not float or p2 < 0 or p2 > 1: raise ValueError("p2 must be a float in the range [0, 1]") if p2 <= p1: raise ValueError("p2 must be greater than p1") cnp = math.factorial(n) / (math.factorial(x) * math.factorial(n - x)) g = special.gamma g1 = (g(x + 1) * g(n - x + 1)) / g(n + 2) ib = special.betainc gg = cnp * g1 * (n + 1) return gg * (ib(x + 1, n - x + 1, p2) - ib(x + 1, n - x + 1, p1))
def TA(self, A=None, B=None, lamb=None, mode="poisson", media=None, desvpad=None, n=None): '''Realiza um teste de aderência dadas as condições de entrada Para o teste, A deve conter o número de trocas, defeitos, etc, e B deve conter quantas vezez essa troca ou defeito ocorreu. Note que é fundamental que haja compatibilidade perfeita entre as entradas''' if self.A: A = self.A if self.B: B = self.B if len(A) != len(B): return print("Entradas incompatíveis") if mode == "poisson": X, N = self.calculateTA(A, B) Oi = B Chi2calc = 0 n = 0 Oiacc = 0 Eiacc = 0 # Generate the Expected number by the poisson model if not lamb: for i in range(len(A)): Eiacc += np.exp(-X) * X**A[i] / math.factorial(A[i]) * N Oiacc += Oi[i] if i != (len(A) - 1) and np.exp(-X) * X**A[i + 1] / math.factorial( A[i + 1]) * N >= 5: Chi2calc += (Oiacc - Eiacc)**2 / Eiacc n += 1 Oiacc = 0 Eiacc = 0 Chi2calc += (Oiacc - Eiacc)**2 / Eiacc n += 1 else: for i in range(len(A)): Eiacc += np.exp(-lamb) * lamb**A[i] / math.factorial( A[i]) * N Oiacc += Oi[i] if i != (len(A) - 1) and np.exp(-X) * X**A[i + 1] / math.factorial( A[i + 1]) * N >= 5: Chi2calc += (Oiacc - Eiacc)**2 / Eiacc n += 1 Oiacc = 0 Eiacc = 0 Chi2calc += (Oiacc - Eiacc)**2 / Eiacc n += 1 print("Graus de Liberdade = {:.3f}".format(n - 2)) Chi2crit = chi2.ppf(1 - self.alfa, n - 2) print("Chi2calc = {:.3f}".format(Chi2calc)) print("Chi2crit = {:.3f}".format(Chi2crit)) if Chi2calc < Chi2crit: print("Aceito H0") return True else: print("Rejeito H0") return False if mode == "normal": Oi = B Chi2calc = 0 n = 0 Oiacc = 0 Eiacc = 0 # Generate the Expected number by the poisson model for i in range(len(A)): Eiacc += (norm.cdf(A[i][0] - A[i][1])) * n Oiacc += Oi[i] Chi2calc += (Oiacc - Eiacc)**2 / Eiacc n += 1 Chi2calc += (Oiacc - Eiacc)**2 / Eiacc n += 1 print("Graus de Liberdade = {:.3f}".format(6)) Chi2crit = chi2.ppf(1 - self.alfa, 6) print("Chi2calc = {:.3f}".format(Chi2calc)) print("Chi2crit = {:.3f}".format(Chi2crit)) if Chi2calc < Chi2crit: print("Aceito H0") return True else: print("Rejeito H0") return False