def nc_of_norm(): f1 = lambda x: x**4 f2 = lambda x: x**2-x+2 rv = norm(loc = 2 , scale=20) print rv.mean() print rv.var() print rv.moment(1) print rv.moment(4) # moments的参数可为m(均值),v(方差值),s(偏度),k(峰度),默认为mv print rv.stats(moments = 'mvsk') # 3)scipy.stats.norm.expect(func,loc=0,scale=1)函数返回func函数相对于正态分布的期望值,其中函数f(x)相对于分布dist的期望值定义为E[x]=Integral(f(x) * dist.pdf(x)) print(norm.expect(f1, loc=1, scale=2)) print(norm.expect(f2, loc=2, scale=5)) # (2)lambda argument_list:expression用来表示匿名函数 # (3)numpy.exp(x)计算x的指数 # (4)numpy.inf表示正无穷大 # (5)scipy.integrate.quad(func,a,b)计算func函数从a至b的积分 # (6)scipy.stats.expon(scale)函数返回符合指数分布的参数为scale的随机变量rv # (7)rv.moment(n,*args,*kwds)返回随机变量的n阶距 #1st non-center moment of expon distribution whose lambda is 0.5 E1 = lambda x: x*0.5*np.exp(-x/2) #2nd non-center moment of expon distribution whose lambda is 0.5 E2 = lambda x: x**2*0.5*np.exp(-x/2) print(integrate.quad(E1, 0, np.inf)) print(integrate.quad(E2, 0, np.inf))
def Atheta_gmm(self, clf, T): k, por, mean, std = len( clf.weights_), clf.weights_, clf.means_, clf.covariances_ t = int(T - 2) theta = np.zeros(int(T)) trunc = 0 re = 0 for i in range(k): trunc = trunc + por[i] * ( 1 - norm.cdf(0, loc=mean[i], scale=np.sqrt(std[i]))) re = re + por[i] * norm.expect(self.norm_pdfx, loc=mean[i], scale=np.sqrt(std[i]), lb=0, ub=np.inf) theta[int(T - 2)] = re / trunc while (t > 0): t = t - 1 re1 = 0 re2 = 0 trunc = 0 for i in range(k): trunc = trunc + por[i] * ( 1 - norm.cdf(0, loc=mean[i], scale=np.sqrt(std[i]))) re1 = re1 + por[i] * norm.expect(self.norm_pdfx, loc=mean[i], scale=np.sqrt(std[i]), lb=0, ub=theta[t + 1]) re2 = re2 + por[i] * theta[t + 1] * (1 - norm.cdf( theta[t + 1], loc=mean[i], scale=np.sqrt(std[i]))) theta[t] = (re1 + re2) / trunc return theta
def choose(original: Data, pca: Data) -> np.ndarray: data_differences = pca.data.sub(original.data) mean_diff = data_differences.mean() std_diff = data_differences.std() latest_data = original.data.iloc[-1] latest_pca = pca.data.iloc[-1] x = (latest_pca - latest_data) #diff = norm.cdf(x, loc=mean_diff, scale=std_diff) - norm.cdf(mean_diff-x, loc=mean_diff, scale=std_diff) props = [0] * len(x) for i in range(len(x)): cur = x[i] if cur > mean_diff[i]: W = norm.cdf(cur, loc=mean_diff[i], scale=std_diff[i]) R_num = norm.expect(lambda y: cur - y, loc=100 * mean_diff[i], scale=10000 * std_diff[i], ub=cur) R_den = norm.expect(lambda y: y - cur, loc=100 * mean_diff[i], scale=10000 * std_diff[i], lb=cur) R = R_num / R_den props[i] = W - (1 - W) / R elif cur < mean_diff[i]: W = 1 - norm.cdf(cur, loc=mean_diff[i], scale=std_diff[i]) R_num = norm.expect(lambda y: y - cur, loc=100 * mean_diff[i], scale=10000 * std_diff[i], lb=cur) R_den = norm.expect(lambda y: cur - y, loc=100 * mean_diff[i], scale=10000 * std_diff[i], ub=cur) R = R_num / R_den props[i] = -(W - (1 - W) / R) """ for i in range(len(diff)): if abs(diff[i]) <= fee: diff[i] = 0 diff = diff/sum(np.absolute(diff)) if diff[0] > 0: diff = [1] + [0]*(len(diff) - 1) elif diff[0] < 0: diff = [-1] + [0]*(len(diff) - 1) else: diff = [0]*len(diff) """ props = [props[0]] + [0] * (len(props) - 1) return pd.DataFrame(props, index=pca.data.columns)
def compute_cvar(losses, ci, distribution='normal'): """ CVaR: measures expected loss given a minimum loss equal to the (theoretical) VaR :param losses: :param ci: :return: """ allowed_distributions = ['normal', 'student'] assert distribution in allowed_distributions, f'distribution should be in : {allowed_distributions}' if distribution == allowed_distributions[0]: pm, ps = losses.mean(), losses.std() var = norm.ppf(ci, loc=pm, scale=ps) tail_loss = norm.expect(lambda x: x, loc=pm, scale=ps, lb=var) if distribution == allowed_distributions[1]: fitted = t.fit(losses) var = t.ppf(ci, *fitted) tail_loss = t.expect(lambda y: y, args=(fitted[0], ), loc=fitted[1], scale=fitted[2], lb=var) cvar = (1 / (1 - ci)) * tail_loss return cvar
def expected(self, loc=0, sd=1, plot=False): #****plugging in change vs percent change not matching up for exp, look for error**** exp = norm.expect(self.value, loc=loc, lb=0, scale=sd) print(exp) if (plot): plb = max(loc - 12 * sd, 0) pub = loc + 12 * sd dx = (pub - plb) / 100 x = np.arange(plb, pub, dx) #print (x) y1 = norm.pdf(x, loc=loc) y2 = [] y3 = [] for i in x: #print (i) y2.append(self.change(i)) for i in x: y3.append(self.value(i)) plt.subplot(311) plt.plot(x, norm.pdf(x, loc)) plt.subplot(312) plt.plot(x, y2) plt.subplot(313) plt.plot(x, y3) plt.show() return (exp)
def calc_risk(self, confidence=0.95): port_returns = self.returns.dot(self.allocation) losses = -port_returns.iloc[:, 0] params = norm.fit(losses) VaR = norm.ppf(confidence, *params) tail_loss = norm.expect(lambda x: x, loc=params[0], scale=params[1], lb=VaR) CVaR = (1 / (1 - confidence)) * tail_loss return losses, VaR, CVaR
def mpc(self): n_interval = int(self.T / self.I) xc = 0 cost_mpc = np.zeros(n_interval) clf = self.estimate_gmm(self.P[:self.N]) ex = 0 k, por, mean, std = len( clf.weights_), clf.weights_, clf.means_, clf.covariances_ for i in range(k): ex = ex + por[i] * norm.expect(self.norm_pdfx, loc=mean[i], scale=np.sqrt(std[i]), lb=0, ub=np.inf) for n in range(1, n_interval + 1): r_pred = self.pred_step(0, (n - 1) * self.I, (n) * self.I, "Wind") r_pred = r_pred.astype(int) r_pred = r_pred.reshape(len(r_pred)) a_real = self.D[self.N + self.V + (n - 1) * self.I:self.N + self.V + n * self.I] r_real = self.R[self.N + self.V + (n - 1) * self.I:self.N + self.V + n * self.I] p_real = self.P[self.N + self.V + (n - 1) * self.I:self.N + self.V + n * self.I] d_real = (a_real - r_real) d_pred = (a_real - r_pred) for i in range(len(d_pred)): d_pred[i] = max(d_pred[i], 0) d_real = d_real.astype(int) d_real = np.r_[np.array(0), d_real] d_pred = d_pred.astype(int) d_pred = np.r_[np.array(0), d_pred] p_real = np.r_[np.array(self.P[self.N + self.V + (n - 1) * self.I - 1]), p_real] x, d, va, vb, cost = self.Ampc_hat(d_pred, p_real, (n - 1) * self.I, n * self.I, xc) cost_mpc[n - 1], xc = self.Aour_demand(vb - d, d_real, p_real, xc) cost_mpc_copy = cost_mpc.copy() for i in range(len(cost_mpc_copy)): cost_mpc[i] = sum(cost_mpc_copy[:i + 1]) return cost_mpc
def calc_risk_montecarlo(self, confidence=0.95, N=1000, total_steps=1, random_state=3567): e_returns = self.returns.mean().to_frame() ## daily expected returns e_cov = self.returns.cov() # e_cov = self.covariance num_of_assets = self.num_of_assets # Initialize daily cumulative loss for the assets, across N runs daily_loss = np.zeros((num_of_assets, N)) # Create the Monte Carlo simulated runs for each asset with correlated randomness # N: number of runs # total_steps: number of minutes per day for n in tqdm(range(N)): # Compute simulated path of length total_steps for correlated returns correlated_randomness = e_cov @ norm.rvs(size=(num_of_assets, total_steps)) # Adjust simulated path by total_steps and mean of portfolio losses steps = 1 / total_steps minute_losses = e_returns * steps + correlated_randomness * np.sqrt( steps) daily_loss[:, n] = list(minute_losses) # Calculate portfolio losses losses = pd.DataFrame(daily_loss).T losses.columns = self.assets losses = -losses.dot(self.allocation).iloc[:, 0] params = norm.fit(losses) VaR = norm.ppf(confidence, *params) tail_loss = norm.expect(lambda x: x, loc=params[0], scale=params[1], lb=VaR) CVaR = (1 / (1 - confidence)) * tail_loss return losses, VaR, CVaR
def prob_z_is_max(z, gps, minz, maxz, verbose=0, tfinp=lambda x:x): assert minz <= z <= maxz, '{} not in [{}, {}]'.format(z, minz, maxz) mu, var = gps.predict(tfinp(z)) ess = gps.ess(tfinp(z)) #print(z, ess) mu, var = array2scalar(mu), array2scalar(var) sigma = np.sqrt(var/ess) global nump nump = 0 def myf(x): global nump nump += 1 return prod_int_div_cdf(x, mu, sigma, gps, minz, maxz, verbose=0, tfinp=tfinp) start = time() val = norm.expect(myf, loc=mu, scale=sigma, lb=mu-3.5*sigma, ub=mu+3.5*sigma, epsabs=0.001, epsrel=0.001) if verbose > 0: print('t[expected]: {}, #p: {}'.format(time()-start, nump)) return val
std_loss = losses.std() # In[409]: std_loss # In[410]: # Compute the mean and variance of the portfolio losses pm = mean_loss ps = std_loss # Compute the 95% VaR using the .ppf() VaR_95 = norm.ppf(0.95, loc=pm, scale=ps) # Compute the expected tail loss and the CVaR in the worst 5% of cases tail_loss = norm.expect(lambda x: x, loc=pm, scale=ps, lb=VaR_95) CVaR_95 = (1 / (1 - 0.95)) * tail_loss # Plot the normal distribution histogram and add lines for the VaR and CVaR plt.hist(norm.rvs(size=100000, loc=pm, scale=ps), bins=100) plt.axvline(x=VaR_95, c='r', label="VaR, 95% confidence level") plt.axvline(x=CVaR_95, c='g', label="CVaR, worst 5% of outcomes") plt.legend() plt.show() # ## VaR of Student's t-distribution # In[411]: from scipy.stats import t
from scipy.stats import norm from scipy.stats import expon from scipy import integrate import numpy as np f1 = lambda x: x**4 f2 = lambda x:x**2 - x + 2 rv = norm(loc = 1, scale=2); print rv.mean(); print rv.var(); print rv.moment(1) print rv.moment(2) print norm.expect(f1, loc=1, scale=2) print "============================" E1 = lambda x: x*0.5*np.exp(-x/2) E2 = lambda x: x**2*0.5*np.exp(-x/2) print integrate.quad(E1,0,np.inf) print integrate.quad(E2,0,np.inf) print expon(scale=2).moment(1) print expon(scale=2).moment(2)
def nc_of_norm(): f1 = lambda x: x**4 f2 = lambda x: x**2-x+2 log(norm.expect(f1, loc=1, scale=2)) log(norm.expect(f2, loc=2, scale=5))
def nc_of_norm(): f1 = lambda x: x**4 f2 = lambda x: x**2 - x + 2 print(norm.expect(f1, loc=1, scale=2)) print(norm.expect(f2, loc=2, scale=5))
regret = np.zeros(N-2) for T in range(2,N): theta = Atheta_gmm(clf,T+1) betas = 0; alphas = 0; for x in range(T-1): trunc = 0; pdf0 = 0; pdfx = 0; for i in range(k): trunc = trunc + por[i]*(1-norm.cdf(trunctry,loc = mean[i], scale = np.sqrt(std[i]))) pdf0 = pdf0 + por[i]*norm.cdf(trunctry,loc = mean[i], scale = np.sqrt(std[i])) pdfx = pdfx + por[i]*norm.pdf(theta[-(i+1)],loc = mean[i], scale = np.sqrt(std[i])) betas = betas + min(pdfx/trunc,pdf0/trunc) for i in range(k): trunc = trunc + por[i]*(1-norm.cdf(trunctry,loc = mean[i], scale = np.sqrt(std[i]))) alphas = alphas+por[i]*norm.expect(gnorm,loc=mean[i],scale=np.sqrt(std[i]),lb=trunctry,ub=10000000) alphas = alphas/trunc regret[T-2] = 2/(betas)-T*pow(alphas/(2*theta[T-2]),T-1)*theta[T-2] regret_uniform = np.zeros((N,ITER)) for n in range(1,N+1): for iternum in range(ITER): p = sample(clf, n_samples=N, random_state=None) p = list(p) o = min(p) theta = Atheta_gmm(clf,n) for t in range(n): if p[t]<= theta[t]: re= (p[t]-o)/o break
from scipy.stats import norm f1 = lambda x: x**4 f2 = lambda x: x**2 - x + 2 print(norm.expect(f1, loc=1, scale=2)) print(norm.expect(f2, loc=1, scale=5))
up = mean + 2 * std / np.sqrt(counter) print("There is a 95.5% probability that daily returns will fall between " + str(low) + " and " + str(up)) #Calculating CVaR df.loc[df['Daily Return'] < 0, 'Losses'] = df['Daily Return'] nLoss = df['Losses'].count() sumLoss = df['Losses'].sum() meanLoss = df['Losses'].mean() stdLoss = df['Losses'].std() # Compute the expected tail loss and the CVaR in the worst 5% of cases tail_loss = norm.expect(lambda x: x, loc=meanLoss, scale=stdLoss, lb=Var_5) CVaR_95 = (1 / (1 - 0.95)) * tail_loss print() print("The expected return on TSLA for the worst 5% of cases (CVAR) = " + str(CVaR_95)) # Plotting the normal distribution histogram and adding lines for the VaR plt.hist(norm.rvs(size=100000, loc=meanLoss, scale=stdLoss), bins=100) plt.axvline(x=Var_5, c='r', label="VaR, 95% confidence level") plt.legend() plt.show()