def euro_put(self): ''' Calculate the value of a European put option using Black-Scholes. No dividends. @rtype: float @return: The value for an option with the given parameters.''' return (norm.cdf(-self.d[1]) * self.k * np.exp(-self.r * self.t) - norm.cdf(-self.d[0]) * self.s)
def randomwalkincome(points, sigma): '''markov process of random component Parameters ---------- points : int number of grid points for income process sigma : float standard deviation for the transition matrix Returns ------- PROB : array transition matrix from shock i to j e : array random shocks to income ''' e = np.linspace(-3, 3, points) avg = (e[-1] - e[-2])/2. PROB = np.zeros((points, points)) for i in xrange(points): for j in xrange(points): diff = abs(e[i] - e[j]) PROB[i,j] = norm.cdf(diff+avg, 0, sigma) - \ norm.cdf(diff-avg, 0, sigma) PROB /= PROB.sum(axis=0) return PROB, e
def pixcont(i, x0, sig, hpix=0.5): '''Integrate a gaussian profile.''' z = (i - x0) / sig hpixs = hpix / sig z2 = z + hpixs z1 = z - hpixs return norm.cdf(z2) - norm.cdf(z1)
def get_power(effect_size, N, p1, p2, significance, two_sided): # assumption 1: n1=n2 # assumption 2: one-sided test p2 = p1 - effect_size # Our random var is the difference between event rate p1 and event rate p2. # So the variance of our random variable is Var[x] = Var[p1] + Var[p2] sigma = np.sqrt(p1*(1-p1) + p2*(1-p2)) if two_sided: Z_crit = norm.ppf((1- (1-significance)/2 )) else: Z_crit = norm.ppf((1- (1-significance) )) # Note: our random var is the difference between control and test, so for every pair of # control/test observations, we have only one observation for our rand var. Thus, use n_control # or n_test but do not use n_total. Hence the N/2 sizes in the formulas below. if two_sided: power2 = 1 - norm.cdf(Z_crit - effect_size * np.sqrt(N/2)/sigma) + norm.cdf(-Z_crit - effect_size * np.sqrt(N/2)/sigma) else: power2 = 1 - norm.cdf(Z_crit - effect_size * np.sqrt(N/2)/sigma) return power2, Z_crit
def _get_edr(self, obs, expected, stddev, bandwidth=0.01, multiplier=3.0): """ Calculated the Euclidean Distanced-Based Rank for a set of observed and expected values from a particular GMPE """ nvals = len(obs) min_d = bandwidth / 2. kappa = self._get_kappa(obs, expected) mu_d = obs - expected d1c = np.fabs(obs - (expected - (multiplier * stddev))) d2c = np.fabs(obs - (expected + (multiplier * stddev))) dc_max = ceil(np.max(np.array([np.max(d1c), np.max(d2c)]))) num_d = len(np.arange(min_d, dc_max, bandwidth)) mde = np.zeros(nvals) for iloc in range(0, num_d): d_val = (min_d + (float(iloc) * bandwidth)) * np.ones(nvals) d_1 = d_val - min_d d_2 = d_val + min_d p_1 = norm.cdf((d_1 - mu_d) / stddev) -\ norm.cdf((-d_1 - mu_d) / stddev) p_2 = norm.cdf((d_2 - mu_d) / stddev) -\ norm.cdf((-d_2 - mu_d) / stddev) mde += (p_2 - p_1) * d_val inv_n = 1.0 / float(nvals) mde_norm = np.sqrt(inv_n * np.sum(mde ** 2.)) edr = np.sqrt(kappa * inv_n * np.sum(mde ** 2.)) return mde_norm, np.sqrt(kappa), edr
def norm_cdf (x_range, mu, var=1, std=None): """ computes the probability that a Gaussian distribution lies within a range of values. Parameters ---------- x_range : (float, float) tuple of range to compute probability for mu : float mean of the Gaussian var : float, optional variance of the Gaussian. Ignored if `std` is provided std : float, optional standard deviation of the Gaussian. This overrides the `var` parameter Returns ------- probability : float probability that Gaussian is within x_range. E.g. .1 means 10%. """ if std is None: std = math.sqrt(var) return abs(norm.cdf(x_range[0], loc=mu, scale=std) - norm.cdf(x_range[1], loc=mu, scale=std))
def test_normal(mean, stddev, offset): # create 100 buckets, from -5.0 to 4.9 inclusive x_values = [(.01 * i - 5) for i in range(1001)] y_values = [(norm.cdf(right) - norm.cdf(left)) for left, right in zip(x_values, x_values[1:])] points = [Point(x, y) for x, y in zip(x_values, y_values)] assert "???" == analysers.get_analysis(points=points)
def pearson4cdf(X, m, nu, a, _lambda, mu, sigma): # pearson4pdf # p = pearson4pdf(X,m,nu,a,lambda) # # Returns the pearson type IV probability density function with # parameters m, nu, a and lambda at the values of X. # # Example # # See also # pearson4pdf betapdf normpdf # pearspdf pearsrnd # Xx = (X - _lambda) / a if Xx < -sqrt(3): p1 = fx(X, m, nu, a, _lambda) * a / (2 * m - 1) * (1j - Xx) * hyp2f1(1, m + nu / 2 * 1j, 2 * m, 2 / (1 - 1j * Xx)) p = float(p1.real) elif Xx > sqrt(3): p1 = 1 - fx(-X, m, -nu, a, -_lambda) * a / (2 * m - 1) * (1j + Xx) * hyp2f1(1, m - nu / 2 * 1j, 2 * m, 2 / (1 + 1j * Xx)) p = float(p1.real) elif Xx < 0 and Xx > -sqrt(3) and abs(nu) < (4 - 2 * sqrt(3)) * m: p1 = norm.cdf(X, mu, sigma) p = float(p1.real) elif Xx < 0 and Xx > -sqrt(3) and abs(nu) > (4 - 2 * sqrt(3)) * m: p1 = (1 - exp(-(nu + 1j * 2 * m) * pi)) ** (-1) - (1j * a * fx(X, m, nu, a, _lambda)) / (1j * nu - 2 * m + 2) * (1 + Xx ** 2) * hyp2f1(1, 2 - 2 * m, 2 - m + 1j * nu / 2, (1 + 1j * Xx) / 2) p = float(p1.real) elif Xx > 0 and Xx < sqrt(3) and abs(nu) < (4 - 2 * sqrt(3)) * m: p1 = norm.cdf(X, mu, sigma) p = float(p1.real) else: p1 = 1 - (1 - exp(-(-nu + 1j * 2 * m) * pi)) ** (-1) + (1j * a * fx(-X, m, -nu, a, -_lambda)) / (1j * (-nu) - 2 * m + 2) * (1 + (-Xx) ** 2) * hyp2f1(1,2-2*m,2-m-1j*nu/2,(1-1j*Xx)/2) p = float(p1.real) return p
def call_option_pricer(S0, K, T, r, sigma, d): d1 = (log(S0*exp(-d*T)/K) + (r + 0.5 * sigma *sigma) * T) / sigma / sqrt(T) d2 = d1 - sigma * sqrt(T) price = S0*exp(-d*T) * norm.cdf(d1) - K * exp(-r*T) * norm.cdf(d2) return price
def slsqp(g, xdists, u_to_x, T, maxitr, tol, ftol, eps): """ Wrapper for scipy.optimize SLSQP constrained minimisation routine. Minimizes beta subject to the constraint g(x)=0. More stable than Rackwitz-Fiessler. """ u0 = zeros(len(xdists)) # Define constraint function to be applied during minimisation g_SLSQP = lambda u: g(u_to_x(u, xdists, T)) ans = minimize(lambda u:(u*u).sum(), x0=u0, method='SLSQP', tol=tol, constraints={'type': 'eq', 'fun': g_SLSQP}, options={'ftol': ftol, 'eps': eps, 'maxiter': maxitr, 'disp': False}) u_beta = ans['x'] x_beta = u_to_x(u_beta, xdists, T) g_beta = g(x_beta) beta = sqrt(ans['fun']) # Sensitivity factors alpha = u_beta/sqrt((u_beta*u_beta).sum()) # Check if g-function is negative at the origin of U-space if g(u_to_x(u0, xdists, T)) < 0: Pf = norm.cdf(beta) else: Pf = norm.cdf(-beta) return {'vars': xdists, 'beta': beta, 'Pf': Pf, 'u_beta': u_beta, 'x_beta': x_beta, 'alpha': alpha, 'alpha_tr': dot(T, alpha), 'nitr': ans['nit'], 'g_beta': g_beta, 'tol': tol, 'msgs': ans['message']}
def VOIfunc(self, n, pointNew, grad): xNew = pointNew nTraining = self._GP._numberTraining tempN = nTraining + n # n=n-1 vec = np.zeros(tempN) X = self._PointsHist for i in xrange(tempN): vec[i] = self._GP.muN(X[i, :], n) maxObs = np.max(vec) std = np.sqrt(self._GP.varN(xNew, n)) muNew, gradMu = self._GP.muN(xNew, n, grad=True) Z = (muNew - maxObs) / std temp1 = (muNew - maxObs) * norm.cdf(Z) + std * norm.pdf(Z) if grad == False: return temp1 var, gradVar = self._GP.varN(xNew, n, grad=True) gradstd = 0.5 * gradVar / std gradZ = ((std * gradMu) - (muNew - maxObs) * gradstd) / var temp10 = ( gradMu * norm.cdf(Z) + (muNew - maxObs) * norm.pdf(Z) * gradZ + norm.pdf(Z) * gradstd + std * (norm.pdf(Z) * Z * (-1.0)) * gradZ ) return temp1, temp10
def leverage(self, short=False): """Delta * underlying/price.""" if short: return -exp(-self.r_foreign * self.expiry) * norm.cdf(-self.d1) * (self.underlying/self.strike) else: return exp(-self.r_foreign * self.expiry) * norm.cdf(self.d1) * (self.underlying/self.strike)
def rho(self, short=False): """Derivative of value with respect to the interest rate.""" if short: return -self.expiry * exp(-self.r_domestic*self.expiry) * self.strike * norm.cdf(-self.d2) else: return self.expiry * exp(-self.r_domestic*self.expiry) * self.strike * norm.cdf(self.d2)
def distrNormalRange(w, n): ''' The CDF of the range of n IID standard normals evaluated at w ''' innerInt = lambda x: norm.pdf(x)*(norm.cdf(x+w) - norm.cdf(x))**(n-1) tmp = integrate.quad(innerInt, -2*w, 2*w) return n*(tmp[0] - tmp[1])
def delta(self, short=False): """Derivative of the value with respect to the underlying.""" if short: return -exp(-self.r_foreign * self.expiry) * norm.cdf(-self.d1) else: return exp(-self.r_foreign * self.expiry) * norm.cdf(self.d1)
def visualizeData_vs_Fortet(abg, binnedTrain, theta, title_tag = '', save_fig_name=''): from scipy.stats import norm bins = binnedTrain.bins phis = bins.keys() N_phi = len(phis) def getMovingThreshold(a,g, phi): psi = arctan(theta) mvt = lambda ts: 1. - ( a*(1 - exp(-ts)) + \ g / sqrt(1+theta*theta) * ( sin ( theta * ( ts + phi) - psi) \ -exp(-ts)*sin(phi*theta - psi) )) return mvt figure() a,b,g = abg[0], abg[1], abg[2] for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): Is = bins[phi_m]['Is'] uniqueIs = bins[phi_m]['unique_Is'] movingThreshold = getMovingThreshold(a,g, phi_m) LHS_numerator = movingThreshold(uniqueIs) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*uniqueIs)) LHS = 1 - norm.cdf(LHS_numerator / LHS_denominator) RHS = zeros_like(LHS) N = len(Is) for rhs_idx in xrange(len(uniqueIs)): t = uniqueIs[rhs_idx] lIs = Is[Is<t] taus = t - lIs; numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*taus)) RHS[rhs_idx] = sum(1. - norm.cdf(numerator/denominator)) / N ax = subplot(N_phi, 1, phi_idx + 1); hold (True) plot(uniqueIs, LHS, 'b', linewidth=3); plot(uniqueIs, RHS, 'r+', markersize=12); xlim((.0, max(uniqueIs))) ylim((.0, max([max(LHS), max(RHS)]))) if (0 == phi_idx): title(title_tag + "$\\alpha, \\beta, \gamma = (%.2g,%.2g,%.2g)$" %(abg[0], abg[1], abg[2]), fontsize = 42 ) ylabel('$1- F(v_{th},t)$', fontsize = 24) # annotate('$\phi_{norm} = %.2g $'%(phi_m/2/pi*theta), (.1, max(LHS)/2.), # fontsize = 24 ) # ylabel('$\phi = %.2g \cdot 2\pi/ \\theta$'%(phi_m/2/pi*theta)) if N_phi != phi_idx+1: setp(ax.get_xticklabels(), visible=False) else: xlabel('$t$', fontsize = 18) # setp(ax.get_yticklabels(), visible=False) get_current_fig_manager().window.showMaximized() if '' != save_fig_name: filename = os.path.join(FIGS_DIR, save_fig_name + '.png') print 'Saving to ', filename savefig(filename)
def black_sholes(S, K, T, r, v, callPutFlag = 'c'): d1 = (log(S / K) + (r + 0.5 * v**2) * T) / (v * sqrt(T)) d2 = d1 - v * sqrt(T) if (callPutFlag == 'c') or (callPutFlag == 'C'): return S * norm.cdf(d1) - K * exp(-r * T) * norm.cdf(d2) else: return K * exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)
def __init__(self, sigma=1, mu=0, limits=(-inf, inf)): self.limits = limits self.sigma, self.mu = sigma, mu self._left = normal_distribution.cdf((limits[0]-mu)/sigma) self._delta = normal_distribution.cdf((limits[1]-mu)/sigma) - self._left self._nllf_scale = log(sqrt(2 * pi * sigma ** 2)) + log(self._delta)
def getCurrentQuote(self,Strike): #Generate sigma from Geometric Brownian Motion sigma = self.GBM(0.3,0.05) #Randomly pick a strike price from quote list strike = float(Strike) #Calculate Black-Scholes formula dPositive = (math.log(100.0/strike)+(0.01 + 0.5*sigma**2))/sigma dNegative = (math.log(100.0/strike)+(0.01 - 0.5*sigma**2))/sigma EuroCall = 100.0*norm.cdf(dPositive) - strike*math.exp(-0.01)*norm.cdf(dNegative) #Et is the noise parameter Et = np.random.normal(1,0.05) #Generate the direction randomly randomNum = random.uniform(0,1) if randomNum < 0.5: direction = 1 elif randomNum >= 0.5: direction = -1 else: direction = 0 #Consider the 5% edge that broker is willing to pay if direction == 1: et = 1.05 elif direction == -1: et = 0.95 else: et = 0 #Final quote price Price = EuroCall*Et*et return [Price,strike,direction]
def theta_spot(forward, strike, maturity, vol, is_call=True, interest_rate=0.0): """Spot Theta, the sensitivity of the present value to a change in time to maturity. Unlike most of the methods in this module, this requires an interest rate, as we are dealing with option price with respect to the Spot underlying Parameters ---------- forward : numeric ndarray Forward price or rate of the underlying asset strike : numeric ndarray Strike price of the option maturity : numeric ndarray Time to maturity of the option, expressed in years vol : numeric ndarray Lognormal (Black) volatility is_call : bool, optional True if option is a Call, False if a Put. interest_rate : numeric ndarray, optional Continuously compounded interest rate, i.e. Zero = Z(0,T) = exp(-rT) """ zero = np.exp(-interest_rate * maturity) stddev = vol * np.sqrt(maturity) sign = np.where(is_call, 1, -1) d1 = _d1(forward, strike, stddev) d2 = d1 - stddev price_ish = sign * (forward * norm.cdf(sign * d1) - strike * zero * norm.cdf(sign * d2)) theta_fwd = theta_forward(forward, strike, maturity, vol, is_call) return theta_fwd + interest_rate * price_ish
def vratio(a, lag=2, cor='hom'): t = np.std(np.subtract(a[lag:], a[1:-lag+1])) ** 2 b = np.std(np.subtract(a[2:], a[1:-1])) ** 2 n = len(a) mu = sum(np.subtract(a[1:], a[:-1])) / float(n) m = (n - lag + 1) * (1 - lag / float(n)) b = sum(np.square([i - mu for i in np.subtract(a[1:n], a[:n-1])])) / float(n - 1) t = sum(np.square([i - lag * mu for i in np.subtract(a[lag:n], a[:n-lag])])) / m vratio = t / (lag * b) la = float(lag) # homoskedastic price series if cor == 'hom': varvrt = 2 * (2 * la - 1) * (la - 1) / (3 * la * n) # heteroskedastic price series elif cor == 'het': varvrt = 0 sum2 = sum(np.square([i - mu for i in np.subtract(a[1:n], a[:n-1])])) for j in range(lag - 1): sum1a = np.square([i - mu for i in np.subtract(a[j + 1:n], a[j:n-1])]) sum1b = np.square([i - mu for i in np.subtract(a[1:n - j], a[0:n - j - 1])]) sum1 = np.dot(sum1a, sum1b) delta = sum1 / (sum2 ** 2) varvrt += ((2 * (la - j) / la) ** 2) * delta zscore = (vratio - 1) / np.sqrt(float(varvrt)) pval = 2 * min(norm.cdf(zscore), norm.cdf(-zscore)) return {'vratio': vratio, 'zscore': zscore, 'pval': pval}
def test_ars_trunc_gaussian(): """ Test ARS on a truncated Gaussian distribution """ from scipy.stats import norm mu = 0 sig = 1 lb = 0.5 ub = np.inf p = lambda x: norm(mu, sig).pdf(x) / (norm.cdf(ub) - norm.cdf(lb)) # Truncated Gaussian log pdf def f(x): x1d = np.atleast_1d(x) lp = np.empty_like(x1d) oob = (x1d < lb) | (x1d > ub) lp[oob] = -np.inf lp[~oob] = -0.5 * x1d[~oob]**2 return lp.reshape(x.shape) x_init = np.array([ 0.5, 2.0]) v_init = f(x_init) N_samples = 10000 smpls = np.zeros(N_samples) ars = AdaptiveRejectionSampler(f, lb, ub, x_init, v_init) for s in np.arange(N_samples): smpls[s] = ars.sample() import matplotlib.pyplot as plt f = plt.figure() _, bins, _ = plt.hist(smpls, 25, normed=True, alpha=0.2) bincenters = 0.5*(bins[1:]+bins[:-1]) plt.plot(bincenters, p(bincenters), 'r--', linewidth=1) plt.show()
def price(self): vol = np.sqrt(self.vol1 ** 2 + self.vol2 ** 2 - 2 * self.rho * self.vol1 * self.vol2) d1 = ( np.log(self.S2_t / self.S1_t) / (vol * np.sqrt(self.T)) + 0.5 * vol * np.sqrt(self.T)) d2 = d1 - vol * np.sqrt(self.T) price = (self.CallPut * (self.S2_t * norm.cdf(self.CallPut * d1, 0, 1) - self.S1_t * norm.cdf(self.CallPut * d2, 0, 1))) return price
def __init__(self, option_type, S0, strike, T, r, div, sigma): try: self.option_type = option_type assert isinstance(option_type, str) self.S0 = float(S0) self.strike = float(strike) self.T = float(T) self.r = float(r) self.div = float(div) self.sigma = float(sigma) except ValueError: print('Error passing Options parameters') if option_type != 'call' and option_type != 'put': raise ValueError("Error: option type not valid. Enter 'call' or 'put'") if S0 < 0 or strike < 0 or T <= 0 or r < 0 or div < 0 or sigma < 0: raise ValueError('Error: Negative inputs not allowed') d1 = (np.log(self.S0 / self.strike) + (self.r + 0.5 * self.sigma ** 2) * self.T) / ( self.sigma * np.sqrt(self.T)) d2 = (d1 - self.sigma * np.sqrt(self.T)) self.Nd1 = norm.cdf(d1, 0, 1) self.pNd1 = norm.pdf(d1, 0, 1) self.Nnd1 = norm.cdf(-d1, 0, 1) self.Nd2 = norm.cdf(d2, 0, 1) self.Nnd2 = norm.cdf(-d2, 0, 1)
def blackscholes_norm(moneyness, maturity, vol, call): """Standardized Black-Scholes Function. Parameters ---------- moneyness : array_like Log-forward moneyness maturity : array_like Fraction of the year, i.e. = 30/365 vol : array_like Annualized volatility (sqrt of variance), i.e. = .15 call : bool array_like Call/put flag. True for call, False for put Returns ------- array_like Option premium standardized by current asset price """ accum_vol = np.atleast_1d(vol)*np.atleast_1d(maturity)**.5 d1arg = - np.atleast_1d(moneyness) / accum_vol + accum_vol/2 d2arg = d1arg - accum_vol out1 = norm.cdf(d1arg) - np.exp(moneyness)*norm.cdf(d2arg) out2 = np.exp(moneyness)*norm.cdf(-d2arg) - norm.cdf(-d1arg) premium = out1 * call + out2 * np.logical_not(call) return premium
def computeAcquisitionFunction_EI(self,target,c1,c2): #acquisitoon function self.x_acquis=self.x_pred z=(target-self.y_pred)/np.sqrt(self.sigma2_pred) self.y_acquis=np.sqrt(self.sigma2_pred)*(z*norm.cdf(z)+norm.pdf(z)) self.y_acquis=self.y_acquis*norm.cdf(-c1.y_pred/np.sqrt(c1.sigma2_pred)) self.y_acquis=self.y_acquis*norm.cdf(-c2.y_pred/np.sqrt(c2.sigma2_pred))
def compute_hardy_weinberg(self): """ Computes and returns statistics related to HW equilibrium Returns: (float, float): Het excess and ChiSq 100 statistics, respectively """ num_calls = self.get_num_calls() if num_calls == 0: return (0.0, 0.0) if self.aa_count + self.ab_count == 0 or self.ab_count + self.bb_count == 0: return (1.0, 0.0) num_calls = float(num_calls) q = self.get_minor_frequency() p = 1 - q temp = 0.013 / q k = temp * temp * temp * temp dh = ((self.ab_count / num_calls + k) / (2 * p * q + k)) - 1 if dh < 0: hw = (2 * norm.cdf(dh, 0, 1 / 10.0)) else: hw = (2 * (1 - norm.cdf(dh, 0, 1 / 10.0))) return (hw, dh)
def generate_multivariate_uniform_optimal(cpu,mem,num_tasks): mean = [0, 0, 0, 0] cov = cov00 a,b,c,d = np.random.multivariate_normal(mean, cov, num_tasks).T # for i,ia in enumerate(a): # print(a[i],b[i],c[i],d[i],'\n') cpus = [] mems = [] values = [] for ix in a: cpus.append(norm.cdf(ix)*(cpu/4-1)+1) for iy in b: mems.append(norm.cdf(iy)*(mem/4-1)+1) for index, iz in enumerate(c): if cpus[index]> mems[index]: values.append(norm.cdf(iz)*(100-1)+1) else: values.append(norm.cdf(d[index])*(100-1)+1) # for i,icpus in enumerate(cpus): # print(cpus[i],mems[i],values[i],'\n') return cpus,mems,values
def gauss_box_model(x, amplitude=1.0, mean=0.0, stddev=1.0, hpix=0.5): '''Integrate a gaussian profile.''' z = (x - mean) / stddev m2 = z + hpix / stddev m1 = z - hpix / stddev return amplitude * (norm.cdf(m2) - norm.cdf(m1))
def calculate_bayes_error(model): """ Returns the bayes error of the given mixture model. This is calculated by integrating the false class density around the decision boundary of the model. :requires: the model is composed of two gaussian components! :param model: the mixture model of a given gene :type model: sklearn.mixture.GMM :returns: the bayes error of the classifier as a float """ #first we find the intersection point coeffs = model.weights_ mus = [x[0] for x in model.means_] sigmas = [x[0] ** 0.5 for x in model.covars_] r1, r2 = findIntersection(mus[0], sigmas[0], mus[1], sigmas[1]) root = 0 if r1 < max(mus[0], mus[1]) and r1 > min(mus[0], mus[1]): root = r1 else: root = r2 #now that we have the intersectionm we need the CDF/survival function of both plots err = 0 if(root < mus[0]): err += norm.sf(root, loc=mus[1], scale=sigmas[1]) * coeffs[1] err += norm.cdf(root, loc=mus[0], scale=sigmas[0]) * coeffs[0] else: err += norm.sf(root, loc=mus[0], scale=sigmas[0]) * coeffs[0] err += norm.cdf(root, loc=mus[1], scale=sigmas[1]) * coeffs[1] return err #/ (norm.sf(-10000, loc=mus[0], scale=sigmas[0]) + norm.sf(-10000, loc=mus[1], scale=sigmas[1]) - err)
def bs_call(S, K, T, r, sigma): return S * norm.cdf(d1(S, K, T, r, sigma)) - K * exp(-r * T) * norm.cdf( d2(S, K, T, r, sigma))
def put_rho(S, K, T, r, sigma): return 0.01 * (-K * T * exp(-r * T) * norm.cdf(-d2(S, K, T, r, sigma)))
def put_theta(S, K, T, r, sigma): return 0.01 * (-(S * norm.pdf(d1(S, K, T, r, sigma)) * sigma) / (2 * sqrt(T)) + r * K * exp(-r * T) * norm.cdf(-d2(S, K, T, r, sigma)))
def put_delta(S, K, T, r, sigma): return -norm.cdf(-d1(S, K, T, r, sigma))
def call_rho(S, K, T, r, sigma): return 0.01 * (K * T * exp(-r * T) * norm.cdf(d2(S, K, T, r, sigma)))
def call_delta(S, K, T, r, sigma): return norm.cdf(d1(S, K, T, r, sigma))
def bs_put(S, K, T, r, sigma): return np.exp(-r * T) * K * norm.cdf( -d2(S, K, T, r, sigma)) - S * norm.cdf(-d1(S, K, T, r, sigma))
def BayesianOptimise(X, y, bounds, cost=None, integers=None, categorics=None, num_categories=None, return_pi=False, alphas=None): """ X: previously evaluated points y: previous results bounds: numerical bounds for new X cost: cost to evaluate previous results - if not None then return best per unit cost integers: index of integer variables categorics: index of categoric variables num_categories: number of categories in each categoric variable return_pi: return probability of improvement or not alphas: error in y term """ X = np.array(X) y = np.array(y) bounds = np.array(bounds) # Clean results - set nans to the worst value for i in range(len(y)): if y[i] in [np.nan, np.inf, -np.inf]: y[i] = np.amin(y) if alphas is None: alphas = 1e-5 else: alphas = np.array(alphas) # Remove non categoric info num_categories = [n for n in num_categories if n != 0] if cost is not None: cost = np.array(cost) # Total columns after one hot encoding categoric features total_columns = X.shape[1] if num_categories is not None: total_columns += sum(num_categories) - len(num_categories) kernel = MixedTypeMatern(integers, categorics, nu=2.5, length_scale=[1.0] * total_columns, num_categories=num_categories) y_model = gp.GaussianProcessRegressor(kernel=1.0 * kernel + 1.0 * gp.kernels.WhiteKernel(), n_restarts_optimizer=50, alpha=0, normalize_y=True) y_model.fit(X, y) # Estimate the cost if cost is not None: kernel = 1.0 * MixedTypeMatern(integers, categorics, nu=2.5, length_scale=[1.0] * total_columns, num_categories=num_categories) cost_model = gp.GaussianProcessRegressor( kernel=1.0 * kernel + 1.0 * gp.kernels.WhiteKernel(), n_restarts_optimizer=50, normalize_y=True) cost_model.fit(X, np.log(cost)) else: cost_model = None #Expected imrpovement in gaussian process at point x per unit time def expected_improvement(x, y_mod, previous_results, cost_mod=None, greater_is_better=True, n_params=1): x_to_predict = x.reshape(-1, n_params) mu, sigma = y_model.predict(x_to_predict, return_std=True) if greater_is_better: loss_optimum = np.max(previous_results) else: loss_optimum = np.min(previous_results) scaling_factor = (-1)**(not greater_is_better) # In case sigma equals zero with np.errstate(divide='ignore'): Z = scaling_factor * (mu - loss_optimum) / sigma ei = scaling_factor * ( mu - loss_optimum) * norm.cdf(Z) + sigma * norm.pdf(Z) ei[sigma == 0.0] == 0.0 # Divide by time if cost_mod is None: return -1 * ei else: return -1 * ei / np.exp(cost_model.predict(x_to_predict)) best_x = None best_func_value = 1 n_restarts = 20 n_params = len(bounds) # Optimise expected improvement for 10 runs for starting_point in np.random.uniform(bounds[:, 0], bounds[:, 1], size=(n_restarts, n_params)): res = minimize(fun=expected_improvement, x0=starting_point.reshape(1, -1), bounds=bounds, method='L-BFGS-B', args=(y_model, np.array(y), cost_model, True, len(bounds))) if res.fun < best_func_value: best_func_value = res.fun best_x = res.x # Sample 1000 random points and take best - quicker than locally optimising n_random = 10000 random_x = np.random.uniform(bounds[:, 0], bounds[:, 1], size=(n_random, n_params)) random_ei = expected_improvement(random_x, y_model, np.array(y), cost_model, True, len(bounds)) # If random is best use that if np.amin(random_ei) < best_func_value: best_func_value = np.amin(random_ei) best_x = random_x[np.argmin(random_ei), :] # Clean categoric and integer values best_x = list(best_x) for i in range(len(best_x)): if i in integers or i in categorics: best_x[i] = int(np.round(best_x[i])) # Probability of improvement if return_pi: mu, sigma = y_model.predict(np.array(best_x).reshape(1, -1), return_std=True) mu, sigma = mu[0], sigma[0] if sigma == 0: pi = 1 if mu > np.amax(y) else 0 else: pi = norm.cdf((mu - np.amax(y)) / sigma) return best_x, pi else: return best_x
def bias_correction(graph, bottoms, targ_type, bits_weight=8, bn_type=torch.nn.BatchNorm2d, signed=False): """ Perform bias correction. Expectation of input activations will be summed for elementwise addition, concate for torch.cat """ from utils.layer_transform import find_prev_bn from scipy.stats import norm print("Start bias correction") # standard_normal = lambda x: torch.exp(-(x * x) / 2) / torch.sqrt(torch.tensor(2 * np.pi)) standard_normal = lambda x: torch.from_numpy(norm(0, 1).pdf(x)).float() standard_cdf = lambda x: torch.from_numpy(norm.cdf(x)).float() calculate_mean = lambda weight, bias: weight * standard_normal( -bias / weight) + bias * (1 - standard_cdf(-bias / weight)) # calculate_var = lambda weight, bias, mean: (1-standard_cdf(-bias/weight)) * (bias*bias + weight*weight + mean * mean - 2 * mean * bias) +\ # weight * (bias - 2 * mean) * (standard_normal(-bias/weight)) + \ # mean * mean * standard_cdf(-bias/weight) bn_module = {} bn_out_shape = {} relu_attached = {} bias_prev = None with torch.no_grad(): for idx_layer in graph: bot = bottoms[idx_layer] if bot is None or bot[0] == 'Data': continue if type(graph[idx_layer]) == bn_type: bn_module[idx_layer] = graph[idx_layer] bn_out_shape[idx_layer] = graph[idx_layer] relu_attached[idx_layer] = False if bias_prev is not None: graph[idx_layer].fake_bias.add_(bias_prev) bias_prev = None continue if type(graph[idx_layer]) == torch.nn.ReLU: if bot[0] in bn_module: relu_attached[bot[0]] = True if type(graph[idx_layer]) in targ_type: # 1 to many or 1 to 1 bn_list, relu_attach_list, connect_type_list, _ = find_prev_bn( bn_module, relu_attached, graph, bottoms, bot[:]) weight = getattr(graph[idx_layer], 'weight').detach().clone() # eps = _quantize_error(weight.cuda(), 8, reduction=None).cpu() ## different results on gpu or cpu, move to gpu eps = _quantize_error(weight, 8, reduction=None, signed=signed) eps = torch.sum(eps.view(weight.size(0), weight.size(1), -1), -1) bn_branch = {} for idx, tmp in enumerate(bn_list): _, bid = tmp if bid[0] in bn_branch: bn_branch[bid[0]].append((tmp, relu_attach_list[idx], connect_type_list[idx])) else: bn_branch[bid[0]] = [(tmp, relu_attach_list[idx], connect_type_list[idx])] bn_res = {} for key in bn_branch: tmp_list = sorted(bn_branch[key], key=lambda x: len(x[0][1]), reverse=True) node_cur, use_relu, connect_type = tmp_list[0] layer_cur, bid = node_cur depth = len(bid) tmp_list.pop(0) bn_bias = layer_cur.fake_bias.detach().clone() bn_weight = layer_cur.fake_weight.detach().clone() if use_relu: expect = calculate_mean(bn_weight, bn_bias) expect[expect < 0] = 0 else: expect = bn_bias while len(tmp_list) > 0: idx_bound = 0 while idx_bound < len(tmp_list) and len( tmp_list[idx_bound][0][1]) == depth: idx_bound += 1 if idx_bound == 0 and len(tmp_list) > 0: # cut depth, add node_cur back depth = len(tmp_list[idx_bound][0][1]) else: for idx in range(idx_bound): node_tmp, use_relu_tmp, connect_type = tmp_list[ idx] bn_bias = node_tmp[0].fake_bias.detach().clone( ) bn_weight = node_tmp[0].fake_weight.detach( ).clone() if use_relu_tmp: expect_tmp = calculate_mean( bn_weight, bn_bias) expect_tmp[expect_tmp < 0] = 0 else: expect_tmp = bn_bias if 'cat' == connect_type: expect = torch.cat([expect, expect_tmp], 0) else: expect += expect_tmp tmp_list = tmp_list[idx_bound:] # expect /= (idx_bound + 1) bn_res[key] = (connect_type, expect) assert len( bn_res ) == 1, "Error while calculating expectation for bias correction" if 'cat' == list(bn_res.values())[0][0]: expect = torch.cat( list(zip(list(bn_res.values())[0]))[1], 0) # group operation num_group = expect.size(0) // eps.size(1) step_size_o = eps.size(0) // num_group step_size_i = expect.size(0) // num_group bias = torch.zeros(eps.size(0)) for g in range(num_group): bias[g * step_size_o:(g + 1) * step_size_o] = torch.matmul( eps[g * step_size_o:(g + 1) * step_size_o], expect[g * step_size_i:(g + 1) * step_size_i]) # bias = torch.matmul(eps, expect) graph[idx_layer].bias.add_(-bias) bias_prev = -bias
def p(x, s): c = x.dropna() ct = c.count() pctile = norm.cdf(-abs(s)) return numpy.percentile(c, norm.cdf(s)*100) if ct >= 1/pctile else numpy.NaN
def mackinnonp(stat, regression="c", num_unit_roots=1, dist_type='ADF-t'): """ Returns MacKinnon's approximate p-value for test stat. Parameters ---------- stat : float "T-value" from an Augmented Dickey-Fuller or DFGLS regression. regression : {'c', 'nc', 'ct', 'ctt'} This is the method of regression that was used. Following MacKinnon's notation, this can be "c" for constant, "nc" for no constant, "ct" for constant and trend, and "ctt" for constant, trend, and trend-squared. num_unit_roots : int The number of series believed to be I(1). For (Augmented) Dickey- Fuller N = 1. dist_type : {'ADF-t', 'ADF-z', 'DFGLS'} The test type to use when computing p-values. Options include 'ADF-t' - ADF t-stat based bootstrap 'ADF-z' - ADF z bootstrap 'DFGLS' - GLS detrended Dickey Fuller Returns ------- p-value : float The p-value for the ADF statistic estimated using MacKinnon 1994. References ---------- MacKinnon, J.G. 1994 "Approximate Asymptotic Distribution Functions for Unit-Root and Cointegration Tests." Journal of Business & Economics Statistics, 12.2, 167-76. Notes ----- Most values are from MacKinnon (1994). Values for DFGLS test statistics and the 'nc' version of the ADF z test statistic were computed following the methodology of MacKinnon (1994). """ dist_type = dist_type.lower() if num_unit_roots > 1 and dist_type.lower() != 'adf-t': raise ValueError('Cointegration results (num_unit_roots > 1) are' + 'only available for ADF-t values') if dist_type == 'adf-t': maxstat = tau_max[regression][num_unit_roots - 1] minstat = tau_min[regression][num_unit_roots - 1] starstat = tau_star[regression][num_unit_roots - 1] small_p = tau_small_p[regression][num_unit_roots - 1] large_p = tau_large_p[regression][num_unit_roots - 1] elif dist_type == 'adf-z': maxstat = adf_z_max[regression] minstat = adf_z_min[regression] starstat = adf_z_star[regression] small_p = adf_z_small_p[regression] large_p = adf_z_large_p[regression] elif dist_type == 'dfgls': maxstat = dfgls_tau_max[regression] minstat = dfgls_tau_min[regression] starstat = dfgls_tau_star[regression] small_p = dfgls_small_p[regression] large_p = dfgls_large_p[regression] else: raise ValueError('Unknown test type {0}'.format(dist_type)) if stat > maxstat: return 1.0 elif stat < minstat: return 0.0 if stat <= starstat: poly_coef = small_p if dist_type == 'adf-z': stat = log(abs(stat)) # Transform stat for small p ADF-z else: poly_coef = large_p return norm.cdf(polyval(poly_coef[::-1], stat))
def _gelu_ref(_X): return (_X * norm.cdf(_X).astype(np.float32), )
def get_exact_value(self): """ Print exact analytic value, based on s0=k. """ d1 = (self.r + .5 * self.sigma**2) * self.t / (self.sigma * sqrt(self.t)) d2 = (self.r - 0.5 * self.sigma**2) * self.t / (self.sigma * sqrt(self.t)) if self.option == 'european': val = self.k * (norm.cdf(d1) - exp(-self.r * self.t) * norm.cdf(d2)) elif self.option == 'asian': print('Exact value unknown for asian option') val = None elif self.option == 'lookback': kk = .5 * self.sigma**2 / r val = self.k * (norm.cdf(d1) - norm.cdf(-d1) * kk - exp(-self.r * self.t) * (norm.cdf(d2) - norm.cdf(d2) * kk)) elif self.option == 'digital': val = self.k * exp(-self.r * self.t) * norm.cdf(d2) elif self.option == 'barrier': kk = .5 * self.sigma**2 / self.r d3 = (2 * log(self.b / self.k) + (self.r + .5 * self.sigma**2) * self.t) / (self.sigma * sqrt(self.t)) d4 = (2 * log(self.b / self.k) + (self.r - .5 * self.sigma**2) * self.t) / (self.sigma * sqrt(self.t)) val = self.k * (norm.cdf(d1) - exp(-self.r * self.t) * norm.cdf(d2) - (self.k / self.b)**(1 - 1 / kk) * ((self.b / self.k)**2 * norm.cdf(d3) - exp(-self.r * self.t) * norm.cdf(d4))) return val
def _compute_poi(self, x, y_max): mean, std = self.gaussian_process.predict(x, return_std=True) z = (mean - y_max - self.eps) / std return norm.cdf(z)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from scipy.stats import norm from math import sqrt # a) mean_population = 100 sd_population = 10 sample_size = 25 sd_sample = sd_population / sqrt(sample_size) prob_sample_mean_below_95 = norm.cdf(x=95, loc=mean_population, scale=sd_sample) print('P(X<95)={:.4f}'.format(prob_sample_mean_below_95)) # b) sample_size = 40 mean_population = (4 + 6) / 2 var_population = ((6 - 4)**2) / 12 mean_x = mean_population var_x = sqrt(var_population) / sqrt(sample_size) print('μ={:.4f}, Var={:.4f}'.format(mean_x, var_x))
def p_mh_minus(self, gamma): qmh = self.q_mh_minus(gamma) if qmh: return 1 - norm.cdf(qmh) else: return None
### Imports ### import math import numpy as np import scipy.special as special from scipy.integrate import quad, dblquad from scipy.stats import norm import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import os import codecs ### Define & Create Variables ### pList = np.linspace(-3, 3, num=50) # generate 50 values for p for comparison mList = range(3, 10, 2) # test every other value of m until 9 (M = 4) normcdfList = norm.cdf( pList) # generate 50 values for the p with std. normal cdf for comparison numInt = plt.figure() # start figure... ### Do Double Numeric Integration to Estimate values for the CDF of P ### for eachParam in mList: # do it for all M values of m cdfList = [] # create empty list to store estimated CDF values m = eachParam # shorter notation # estimate the constant const = special.gamma(m - 1) * 2**(3 * (1 - m / 2)) / ( special.gamma(m / 2) * special.gamma((m - 1) / 2) * special.gamma( (m - 1) / 2)) for eachPoint in pList: # estimate F_P(p) for all 50 data points (-3 to 3) cdf = dblquad( lambda t, p: const * 1 / abs(t) * (p / t)**(m - 1) * np.exp(-0.5 * (p / t)**2) * (1 - t**2)**(
def N(d): # Cumulative Standard Normal Distribution return norm.cdf(d)
def predy(self): if 'predy' not in self._cache: self._cache['predy'] = norm.cdf(self.xb) return self._cache['predy']
def transform(self, a): return norm.cdf(a)
def w3(self, b): # b is bid price return norm.cdf(np.log(b + epsilon), self.mu, self.sigma)
def ll(self, par): beta = np.reshape(np.array(par), (self.k, 1)) q = 2 * self.y - 1 qxb = q * np.dot(self.x, beta) ll = sum(np.log(norm.cdf(qxb))) return ll
a_param = a.rvs() b_param = b.rvs() beta_fun = beta.cdf(input_space, a_param, b_param) warped_objective = fixed_objective_function( beta.cdf(input_space, a_param, b_param)) e_w_max = argrelextrema(warped_objective, np.greater)[0].shape[0] e_w_min = argrelextrema(warped_objective, np.less)[0].shape[0] counter += 1 print counter if e_w_max == extrema_max_obj and e_w_min == extrema_min_obj: discovered = True a_warped = a_param b_warped = b_param beta_warped_space = beta.cdf(input_space, a_warped, b_warped) final_space = norm.cdf(beta_warped_space, 0, 1) #Now we transform this space with a normal cdf. #Ahora sabes que los parametros a y b son los que warpean. Ya puedes hacer el grid para deswarpear. print 'Starting grid search to look for the best beta params to squash the input params in order to get a stationary function' print 'The obj function is to maximize KPSS s.t. having the same number of local minimas and maximas' a_grid = np.linspace(0.1, 10, 50) b_grid = np.linspace(0.1, 10, 50) c_grid = np.linspace(0.1, 10, 50) a_dewarp = -1 b_dewarp = -1 c_dewarp = -1 min_distance = 100000 counter = 0
def bayesianoptimization(X, y, candidates_of_X, acquisition_function_flag, cumulative_variance=None): """ Bayesian optimization Gaussian process regression model is constructed between X and y. A candidate of X with the highest acquisition function is selected using the model from candidates of X. Parameters ---------- X: numpy.array or pandas.DataFrame m x n matrix of X-variables of training dataset (m is the number of samples and n is the number of X-variables) y: numpy.array or pandas.DataFrame m x 1 vector of a y-variable of training dataset candidates_of_X: numpy.array or pandas.DataFrame Candidates of X acquisition_function_flag: int 1: Mutual information (MI), 2: Expected improvement(EI), 3: Probability of improvement (PI) [0: Estimated y-values] cumulative_variance: numpy.array or pandas.DataFrame cumulative variance in mutual information (MI)[acquisition_function_flag=1] Returns ------- selected_candidate_number : int selected number of candidates_of_X selected_X_candidate : numpy.array selected X candidate cumulative_variance: numpy.array cumulative variance in mutual information (MI)[acquisition_function_flag=1] """ X = np.array(X) y = np.array(y) if cumulative_variance is None: cumulative_variance = np.empty(len(y)) else: cumulative_variance = np.array(cumulative_variance) relaxation_value = 0.01 delta = 10**-6 alpha = np.log(2 / delta) autoscaled_X = (X - X.mean(axis=0)) / X.std(axis=0, ddof=1) autoscaled_candidates_of_X = (candidates_of_X - X.mean(axis=0)) / X.std( axis=0, ddof=1) autoscaled_y = (y - y.mean(axis=0)) / y.std(axis=0, ddof=1) gaussian_process_model = GaussianProcessRegressor(ConstantKernel() * RBF() + WhiteKernel()) gaussian_process_model.fit(autoscaled_X, autoscaled_y) autoscaled_estimated_y_test, autoscaled_std_of_estimated_y_test = gaussian_process_model.predict( autoscaled_candidates_of_X, return_std=True) if acquisition_function_flag == 1: acquisition_function_values = autoscaled_estimated_y_test + alpha**0.5 * ( (autoscaled_std_of_estimated_y_test**2 + cumulative_variance)**0.5 - cumulative_variance**0.5) cumulative_variance = cumulative_variance + autoscaled_std_of_estimated_y_test**2 elif acquisition_function_flag == 2: acquisition_function_values = (autoscaled_estimated_y_test - max(autoscaled_y) - relaxation_value) * \ norm.cdf((autoscaled_estimated_y_test - max(autoscaled_y) - relaxation_value) / autoscaled_std_of_estimated_y_test) + \ autoscaled_std_of_estimated_y_test * \ norm.pdf((autoscaled_estimated_y_test - max(autoscaled_y) - relaxation_value) / autoscaled_std_of_estimated_y_test) elif acquisition_function_flag == 3: acquisition_function_values = norm.cdf( (autoscaled_estimated_y_test - max(autoscaled_y) - relaxation_value) / autoscaled_std_of_estimated_y_test) elif acquisition_function_flag == 0: acquisition_function_values = autoscaled_estimated_y_test selected_candidate_number = np.where( acquisition_function_values == max(acquisition_function_values))[0][0] selected_X_candidate = candidates_of_X[selected_candidate_number, :] return selected_candidate_number, selected_X_candidate, cumulative_variance
def norm_range_y(x): return norm.cdf(max_y,loc=x,scale=sigma)-norm.cdf(min_y,loc=x,scale=sigma)
def _slow_negative_log_likelihood(args, Y, D, X, Z): """ Negative Log-likelihood function of the generalized Roy model. """ # Distribute parametrization Y1_coeffs = np.array(args['TREATED']['all']) Y0_coeffs = np.array(args['UNTREATED']['all']) C_coeffs = np.array(args['COST']['all']) U1_var = args['TREATED']['var'] U0_var = args['UNTREATED']['var'] var_V = args['COST']['var'] U1V_rho = args['RHO']['treated'] U0V_rho = args['RHO']['untreated'] # Auxiliary objects. U1_sd = np.sqrt(U1_var) U0_sd = np.sqrt(U0_var) V_sd = np.sqrt(var_V) num_agents = Y.shape[0] choice_coeffs = np.concatenate((Y1_coeffs - Y0_coeffs, -C_coeffs)) # Initialize containers likl = np.tile(np.nan, num_agents) choice_idx = np.tile(np.nan, num_agents) # Likelihood construction. for i in range(num_agents): G = np.concatenate((X[i, :], Z[i, :])) choice_idx[i] = np.dot(choice_coeffs, G) # Select outcome information if D[i] == 1.00: coeffs, rho, sd = Y1_coeffs, U1V_rho, U1_sd else: coeffs, rho, sd = Y0_coeffs, U0V_rho, U0_sd arg_one = (Y[i] - np.dot(coeffs, X[i, :])) / sd arg_two = (choice_idx[i] - rho * V_sd * arg_one) / \ np.sqrt((1.0 - rho ** 2) * var_V) pdf_evals, cdf_evals = norm.pdf(arg_one), norm.cdf(arg_two) if D[i] == 1.0: contrib = (1.0 / float(sd)) * pdf_evals * cdf_evals else: contrib = (1.0 / float(sd)) * pdf_evals * (1.0 - cdf_evals) likl[i] = contrib # Transformations. likl = -np.mean(np.log(np.clip(likl, 1e-20, np.inf))) # Quality checks. assert (isinstance(likl, float)) assert (np.isfinite(likl)) # Finishing. return likl
def L(xn, theta, n): return n * norm.pdf(xn, theta) * norm.cdf(xn, theta)**(n - 1)
def normal_cdf(s: pd.Series) -> pd.Series: """Transforms the Series via the CDF of the Normal distribution""" return pd.Series(norm.cdf(s), index=s.index)
def f_h(x, r): y = 1 - 2 * norm.cdf(-r / x) - 2 / (np.sqrt(2 * np.pi) * r / x) * (1 - np.exp(-(r**2 / (2 * (x**2))))) return y
def norm_range_x(x): return norm.cdf(max_x,loc=x,scale=sigma)-norm.cdf(min_x,loc=x,scale=sigma)