def MacGyver_method(tickerlist, dfeps, method='integrated'): '''MacGyver method of pairwise calculation''' periodicity = 52 minimal = 10**(-20) xlams, alphas, betas = [], [], [] for it in range(len(tickerlist) - 1): tick1 = tickerlist[it] for jt in range(it + 1, len(tickerlist)): tick2 = tickerlist[jt] InData = np.array(dfeps[[tick1, tick2]]) # global cast # integrated corr. if method == 'integrated': result = minimize_scalar(IntegratedCorrObj) xlamopt = np.exp(result.x) / (1 + np.exp(result.x)) print(tick1, tick2) print(' Optimal lambda:', xlamopt) print(' Optimal objective function:', \ result.fun) if np.absolute(xlamopt) < minimal or xlamopt >= 1: halflife = 0 else: halflife = -np.log(2) / np.log(1 - xlamopt) print(' Half-life (years):', halflife / periodicity) xlams.append(xlamopt) # mean revert corr. elif method == 'meanRev': #alpha and beta positive corr_bounds = scpo.Bounds([0, 0], [np.inf, np.inf]) #Sum of alpha and beta is less than 1 corr_linear_constraint = \ scpo.LinearConstraint([[1, 1]],[0],[.999]) initparams = [.02, .93] results = scpo.minimize(MeanRevCorrObj, \ initparams, \ method='trust-constr', \ jac='2-point', \ hess=scpo.SR1(), \ bounds=corr_bounds, \ constraints=corr_linear_constraint) alpha, beta = results.x print('Optimal alpha, beta:', alpha, beta) print('Optimal objective function:', results.fun) halflife = -np.log(2) / np.log(1 - alpha) print('Half-life (years):', halflife / periodicity) alphas.append(alpha) betas.append(beta) # cout median values if method == 'integrated': print('\nMedian MacGyver lambda:', np.median(xlams)) elif method == 'meanRev': print('\nMedian MacGyver alpha:', np.median(alphas)) print('\nMedian MacGyver beta:', np.median(betas))
def compute_meanRevCorr(df_logs, InData): '''Compute mean reverting correlations''' periodicity = 52 #alpha and beta positive corr_bounds = scpo.Bounds([0, 0], [np.inf, np.inf]) #Sum of alpha and beta is less than 1 corr_linear_constraint = \ scpo.LinearConstraint([[1, 1]],[0],[.999]) initparams = [.02, .93] results = scpo.minimize(MeanRevCorrObj, \ initparams, \ method='trust-constr', \ jac='2-point', \ hess=scpo.SR1(), \ bounds=corr_bounds, \ constraints=corr_linear_constraint) alpha, beta = results.x print('Optimal alpha, beta:', alpha, beta) print('Optimal objective function:', results.fun) halflife = -np.log(2) / np.log(1 - alpha) print('Half-life (years):', halflife / periodicity) #Compute mean reverting correlations nobs = len(InData) nsecs = len(InData[0]) previousq = np.identity(nsecs) Rlong = np.corrcoef(InData.T) rmatrices = [] for i in range(nobs): stdmtrx = np.diag([1 / np.sqrt(previousq[s, s]) for s in range(nsecs)]) rmatrices.append(np.matmul(stdmtrx, np.matmul(previousq, stdmtrx))) shockvec = np.mat(np.array(InData[i])) #Update q matrix shockmat = np.matmul(shockvec.T, shockvec) previousq = (1 - alpha - beta) * Rlong + alpha * shockmat + beta * previousq #Plot mean-reverting correlations iccol = ['r', 'g', 'b'] xtitle = 'Mean Reverting Correlations α=%1.5f' % alpha xtitle += ', β=%1.5f' % beta xtitle+=', '+min(df_logs.index.strftime("%Y-%m-%d"))+':'+ \ max(df_logs.index.strftime("%Y-%m-%d")) dates = df_logs.index stride = 5 * periodicity corr_matrix = df_logs[df_logs.columns].corr() plot_corrs(dates, rmatrices, corr_matrix, iccol, stride, xtitle)
def trainSVM(x, y): x = np.asarray(x, dtype='float64') y = np.asarray(y, dtype='float64') n = x.shape[0] m = x.shape[1] left_bounds = np.zeros(n) right_bounds = np.full((n, ), np.inf) bounds = opt.Bounds(left_bounds, right_bounds) constr_bound = np.zeros((1, )) constr_matr = np.expand_dims(y, axis=0) constraint = opt.LinearConstraint(constr_matr, constr_bound, constr_bound) x0 = np.zeros_like(y) l = opt.minimize( util.lagr, x0, args=(x, y), method='trust-constr', jac='2-point', hess=opt.SR1(), #jac=util.lagr_der, hess=util.lagr_hess, constraints=constraint, bounds=bounds) l = l.x support_points = np.array( [not math.isclose(0., li, rel_tol=0., abs_tol=1e-03) for li in l]) x = x[support_points] y = y[support_points] l = l[support_points] n_supp = x.shape[0] lyx = np.expand_dims(l * y, 1) * x w = np.sum(lyx, axis=0) b = y[0] - np.dot(w, x[0]) def classifier(input): input = np.asarray(input, dtype='float64') to_sum = np.dot(lyx, input.T) s = np.sum(to_sum, axis=0) return np.sign(s + b) return classifier
#Done with MeanRevCorrObj import scipy.optimize as scpo #alpha and beta positive corr_bounds = scpo.Bounds([0, 0], [np.inf, np.inf]) #Sum of alpha and beta is less than 1 corr_linear_constraint = \ scpo.LinearConstraint([[1, 1]],[0],[.999]) initparams = [.02, .93] results = scpo.minimize(MeanRevCorrObj, \ initparams, \ method='trust-constr', \ jac='2-point', \ hess=scpo.SR1(), \ bounds=corr_bounds, \ constraints=corr_linear_constraint) alpha, beta = results.x print('Optimal alpha, beta:', alpha, beta) print('Optimal objective function:', results.fun) halflife = -np.log(2) / np.log(1 - alpha) print('Half-life (months):', halflife) #Plot mean reverting correlations previousq = np.identity(len(InData[0])) Rlong = np.corrcoef(InData.T) rmatrices = [] for i in range(len(InData)): stdmtrx = np.diag(
def sr1_method(initial_approximation, args, eps=1e-3): """ Newton's method with SR1 strategy by scipy.optimize.SR1. param: initial_approximation: initial approximation - tuple of float args: list length of 3 arguments: 1. f: regression function - reference type 2. x: np.array of float - (n, ) 3. y: np.array of float - (n, ) eps: estimated error - 1e-3 return: xn_1: tuple of float params """ # Initialize SR1 hessian hessian = optimize.SR1() hessian.initialize(n=2, approx_type='inv_hess') xn = initial_approximation old_old_error = None def hessian_dot_fprime(xn, f, x, y): """ Method to return multiplication of SR1 hessian and jacobian of MSE function. param: xn: tuple of float parameters f: regression function - reference type x: np.array of float - (n, ) y: np.array of float - (n, ) return: multiplication of matrix SR1 hessian and jacobian of MSE function """ return hessian.dot(error_mse_fprime(xn, f, x, y)) while True: d_xn = hessian_dot_fprime(xn, args[0], args[1], args[2]) # Compute a step size using scipy.optimize.line_search to satisfy # the Wolfe conditions step = optimize.line_search(error_mse, hessian_dot_fprime, xn, -d_xn, gfk=d_xn, old_fval=error_mse(xn, args[0], args[1], args[2]), old_old_fval=old_old_error, args=(args[0], args[1], args[2]), maxiter=10000) step = step[0] if step is None: step = 0 # Compute new params xn_1 = xn - step * d_xn # Write a callback callback_function(xn_1) if abs( error_mse(xn, args[0], args[1], args[2]) - error_mse(xn_1, args[0], args[1], args[2])) < eps: break # Update SR1 hessian hessian.update( xn_1 - xn, error_mse_fprime(xn_1, args[0], args[1], args[2]) - error_mse_fprime(xn, args[0], args[1], args[2])) old_old_error = error_mse(xn, args[0], args[1], args[2]) xn = xn_1 return xn_1
def __init__(self): self.method = 'trust-constr' # Optimization method self.jac = "2-point" # Automatic Jacobian finite differenciation self.hess = opt.SR1() # opt.BFGS() method for the hessian computation self.ul = np.pi / 4 # Max turn constraint for our problem (action limits). How much the vehicle can turn