def Chaikin_vol(df, n=14): HLRange = df['High'] - df['Low'] EMA = HLRange.ewm(span=n, min_periods=n).mean() Chikinin_volat = bMA.get_return(ul.fnp(EMA), lag=n, cval=np.NaN) Chikinin_volat = ul.fnp(Chikinin_volat) return [ul.fnp(EMA), Chikinin_volat]
def preproces_data_3D(self, X, Y): # Preprocess the variables X and Y ### X axis date properties, just to be seen clearly ### First we transform everything to python format X = ul.fnp(X) Y = ul.fnp(Y) # Each plot can perform several plotting on the same X axis # So there could be more than one NcX and NcY # NpX and NpY must be the same. NpX, NcX = X.shape NpY, NcY = Y.shape if (X.size > 0): if (type(X[0, 0]).__name__ == "str" or type(X[0, 0]).__name__ == "string_"): self.Xticklabels = X.T.tolist()[0] X = ul.fnp(range(X.size)) if (Y.size > 0): if (type(Y[0, 0]).__name__ == "str" or type(Y[0, 0]).__name__ == "string_"): self.Yticklabels = Y.T.tolist()[0] Y = ul.fnp(range(X.size)) # The given X becomes the new axis self.X = X self.Y = Y return X, Y
def get_ThresholdMask(self, feature, ths = None, reverse = False): # This function computes a mask of the values of a Matrix (Nsamples, Nfeatures) # ths is suposed to be a list of thresholds for the feature ths = [0.3, 0.5, 0.9] # The selection will have "1" where the selection was. # The samples to the left of the first th are "1" and then it conmutes with each new th. # If you want the opposite then you use the "reverse" # TODO: Apply histeresis to the filters so that they not fluctuate as much ? # Feature is an Nx1 matrix feature = ul.fnp(feature) # We order the ths in increasing order # TODO ths = ul.fnp(ths) # nan_mask = np.argwhere(feature.isNan())[:,0] # feature[nan_mask] = 0 # We conmutate in the odd number of ths mask_sel = np.ones(feature.size) * (reverse^1) for i in range(ths.size): mask_sel_aux = np.argwhere(feature > ths[i])[:,0] mask_sel[mask_sel_aux] = (i % 2 == reverse^1) # print "PENE" # TODO: We cannot do it like this if the original has Nans. # mask_sel[nan_mask] = 0 mask_sel = mask_sel.astype(bool) return mask_sel
def MDD(timeSeries, window): """ A maximum drawdown (MDD) is the maximum loss from a peak to a trough of a portfolio, before a new peak is attained. Maximum Drawdown (MDD) is an indicator of downside risk over a specified time period. It can be used both as a stand-alone measure or as an input into other metrics such as "Return over Maximum Drawdown" and Calmar Ratio. Maximum Drawdown is expressed in percentage terms and computed as: Read more: Maximum Drawdown (MDD) Definition """ ## Rolling_max calculates puts the maximum value seen at time t. # We # Calculate the max drawdown in the past window days for each day in the series. # Use min_periods=1 if you want to let the first 252 days data have an expanding window Roll_Max = pd.rolling_max(timeSeries, window, min_periods=1) Roll_Max = ul.fnp(Roll_Max) # print (Roll_Max.shape) # How much we have lost compared to the maximum so dar ## Daily_Drawdown = timeSeries/Roll_Max - 1.0 print (Daily_Drawdown.shape) # Next we calculate the minimum (negative) daily drawdown in that window. # Again, use min_periods=1 if you want to allow the expanding window Max_Daily_Drawdown = pd.rolling_min(Daily_Drawdown, window, min_periods=1) Max_Daily_Drawdown = ul.fnp(Max_Daily_Drawdown) return Daily_Drawdown, Max_Daily_Drawdown
def PSAR(df, iaf = 0.02, maxaf = 0.2): length = len(df) dates = list(df.index) high = list(df['High']) low = list(df['Low']) close = list(df['Close']) psar = close[0:len(close)] psarbull = [None] * length psarbear = [None] * length bull = True af = iaf ep = low[0] hp = high[0] lp = low[0] for i in range(2,length): if bull: psar[i] = psar[i - 1] + af * (hp - psar[i - 1]) else: psar[i] = psar[i - 1] + af * (lp - psar[i - 1]) reverse = False if bull: if low[i] < psar[i]: bull = False reverse = True psar[i] = hp lp = low[i] af = iaf else: if high[i] > psar[i]: bull = True reverse = True psar[i] = lp hp = high[i] af = iaf if not reverse: if bull: if high[i] > hp: hp = high[i] af = min(af + iaf, maxaf) if low[i - 1] < psar[i]: psar[i] = low[i - 1] if low[i - 2] < psar[i]: psar[i] = low[i - 2] else: if low[i] < lp: lp = low[i] af = min(af + iaf, maxaf) if high[i - 1] > psar[i]: psar[i] = high[i - 1] if high[i - 2] > psar[i]: psar[i] = high[i - 2] if bull: psarbull[i] = psar[i] else: psarbear[i] = psar[i] psarbear = ul.fnp(psarbear) psarbull = ul.fnp(psarbull) return psarbull,psarbear
def PSAR(df, iaf=0.02, maxaf=0.2): length = len(df) dates = list(df.index) high = list(df['High']) low = list(df['Low']) close = list(df['Close']) psar = close[0:len(close)] psarbull = [None] * length psarbear = [None] * length bull = True af = iaf ep = low[0] hp = high[0] lp = low[0] for i in range(2, length): if bull: psar[i] = psar[i - 1] + af * (hp - psar[i - 1]) else: psar[i] = psar[i - 1] + af * (lp - psar[i - 1]) reverse = False if bull: if low[i] < psar[i]: bull = False reverse = True psar[i] = hp lp = low[i] af = iaf else: if high[i] > psar[i]: bull = True reverse = True psar[i] = lp hp = high[i] af = iaf if not reverse: if bull: if high[i] > hp: hp = high[i] af = min(af + iaf, maxaf) if low[i - 1] < psar[i]: psar[i] = low[i - 1] if low[i - 2] < psar[i]: psar[i] = low[i - 2] else: if low[i] < lp: lp = low[i] af = min(af + iaf, maxaf) if high[i - 1] > psar[i]: psar[i] = high[i - 1] if high[i - 2] > psar[i]: psar[i] = high[i - 2] if bull: psarbull[i] = psar[i] else: psarbear[i] = psar[i] psarbear = ul.fnp(psarbear) psarbull = ul.fnp(psarbull) return psarbull, psarbear
def MDD(timeSeries, window): """ A maximum drawdown (MDD) is the maximum loss from a peak to a trough of a portfolio, before a new peak is attained. Maximum Drawdown (MDD) is an indicator of downside risk over a specified time period. It can be used both as a stand-alone measure or as an input into other metrics such as "Return over Maximum Drawdown" and Calmar Ratio. Maximum Drawdown is expressed in percentage terms and computed as: Read more: Maximum Drawdown (MDD) Definition """ ## Rolling_max calculates puts the maximum value seen at time t. # We # Calculate the max drawdown in the past window days for each day in the series. # Use min_periods=1 if you want to let the first 252 days data have an expanding window Roll_Max = pd.rolling_max(timeSeries, window, min_periods=1) Roll_Max = ul.fnp(Roll_Max) print Roll_Max.shape # How much we have lost compared to the maximum so dar Daily_Drawdown = timeSeries / Roll_Max - 1.0 print Daily_Drawdown.shape # Next we calculate the minimum (negative) daily drawdown in that window. # Again, use min_periods=1 if you want to allow the expanding window Max_Daily_Drawdown = pd.rolling_min(Daily_Drawdown, window, min_periods=1) Max_Daily_Drawdown = ul.fnp(Max_Daily_Drawdown) return Daily_Drawdown, Max_Daily_Drawdown
def get_ThresholdMask(self, feature, ths=None, reverse=False): # This function computes a mask of the values of a Matrix (Nsamples, Nfeatures) # ths is suposed to be a list of thresholds for the feature ths = [0.3, 0.5, 0.9] # The selection will have "1" where the selection was. # The samples to the left of the first th are "1" and then it conmutes with each new th. # If you want the opposite then you use the "reverse" # TODO: Apply histeresis to the filters so that they not fluctuate as much ? # Feature is an Nx1 matrix feature = ul.fnp(feature) # We order the ths in increasing order # TODO ths = ul.fnp(ths) # nan_mask = np.argwhere(feature.isNan())[:,0] # feature[nan_mask] = 0 # We conmutate in the odd number of ths mask_sel = np.ones(feature.size) * (reverse ^ 1) for i in range(ths.size): mask_sel_aux = np.argwhere(feature > ths[i])[:, 0] mask_sel[mask_sel_aux] = (i % 2 == reverse ^ 1) # print "PENE" # TODO: We cannot do it like this if the original has Nans. # mask_sel[nan_mask] = 0 mask_sel = mask_sel.astype(bool) return mask_sel
def Chaikin_vol(df, n = 14): HLRange = df['High'] - df['Low'] EMA = HLRange.ewm( span = n, min_periods = n).mean() Chikinin_volat = bMA.get_return(ul.fnp(EMA), lag = n, cval = np.NaN) Chikinin_volat = ul.fnp(Chikinin_volat) return [ul.fnp(EMA), Chikinin_volat]
def compute_allocations(self, allocations): # This function performs a fast computing of the Return and STD # of a set of portfolios. No caring about Rf rate. Nalloc = len(allocations) fast_flag = 1 if (len(allocations) == 1): fast_flag = 0 if (fast_flag == 0): ## Properway Using Library Function ## P_Ret_s = [] P_std_Return_s = [] for i in range(Nalloc): self.set_allocation(allocations[i]) expRet = self.get_PortfolioMeanReturn() stdRet = self.get_PortfolioStd() P_Ret_s.append(expRet) P_std_Return_s.append(stdRet) # Fast Way !## else: Allocations = ul.fnp(allocations) P_Ret_s = np.dot(ul.fnp(Allocations), ul.fnp(self.get_MeanReturns())) P_std_Return_s = np.dot(ul.fnp(Allocations), self.get_covMatrix()) * ul.fnp(Allocations) P_std_Return_s = np.sqrt(np.sum(P_std_Return_s, axis=1)) ## These are the expectd return and std for the differen portfolios return P_Ret_s, P_std_Return_s
def bar_3D (self, Xgrid,Ygrid, Zvalues, labels = [], # Labels for tittle, axis and so on. legend = [], # Legend for the curves plotted nf = 1, # New figure na = 0, # New axis. To plot in a new axis # Basic parameters that we can usually find in a plot color = None, # Color lw = 2, # Line width alpha = 1.0, # Alpha width = 1.0, fontsize = 15, # The font for the labels in the title fontsizeL = 10, # The font for the labels in the legeng fontsizeA = 20, # The font for the labels in the axis loc = 1, ): # Plots the training and Validation score of a realization # Initial position of the cube in 3D X = np.array(Xgrid) Y = np.array(Ygrid) self.preproces_data_3D(X,Y) X = self.X Y = self.Y xpos, ypos = np.meshgrid(X, Y) xpos = xpos.flatten() ypos = ypos.flatten() zpos = np.zeros(ul.fnp(Zvalues).shape).flatten() # print xpos.shape, ypos.shape, zpos.shape ## Lenght of the cubes dx = np.ones(xpos.size).flatten() * width dy = np.ones(ypos.size).flatten() * width dz = ul.fnp(Zvalues).flatten() self.figure_management(nf, na, labels, fontsize, dim = "3D") fig = self.fig ax = self.axes if (nf == 1): color = 'copper' else: color = cm.coolwarm ax.bar3d(xpos, ypos, zpos, dx, dy, dz, cmap="copper", color='#00ceaa', alpha = alpha) plt.show() self.format_axis_3D(nf, fontsize = fontsizeA)
def MACD(df, n_fast=26, n_slow=12, n_smooth=9): EMAfast = df['Close'].ewm(span=n_fast, min_periods=n_slow - 1).mean() EMAslow = df['Close'].ewm(span=n_slow, min_periods=n_slow - 1).mean() MACD = EMAfast - EMAslow MACDsign = MACD.ewm(span=9, min_periods=8).mean() MACDdiff = MACD - MACDsign MACD = ul.fnp(MACD) MACDsign = ul.fnp(MACDsign) MACDdiff = ul.fnp(MACDdiff) ret = ul.fnp([MACD, MACDsign, MACDdiff]) return ret
def MACD(df, n_fast = 26, n_slow = 12, n_smooth = 9): EMAfast = df['Close'].ewm( span = n_fast, min_periods = n_slow - 1).mean() EMAslow = df['Close'].ewm( span = n_slow, min_periods = n_slow - 1).mean() MACD = EMAfast - EMAslow MACDsign = MACD.ewm( span = 9, min_periods = 8).mean() MACDdiff = MACD - MACDsign MACD = ul.fnp(MACD) MACDsign = ul.fnp(MACDsign) MACDdiff = ul.fnp(MACDdiff) ret = ul.fnp([MACD,MACDsign,MACDdiff]) return ret
def MACD(df, n_fast=26, n_slow=12, n_smooth=9): EMAfast = pd.ewma(df['Close'], span=n_fast, min_periods=n_slow - 1) EMAslow = pd.ewma(df['Close'], span=n_slow, min_periods=n_slow - 1) MACD = EMAfast - EMAslow MACDsign = pd.ewma(MACD, span=9, min_periods=8) MACDdiff = MACD - MACDsign MACD = ul.fnp(MACD) MACDsign = ul.fnp(MACDsign) MACDdiff = ul.fnp(MACDdiff) ret = ul.fnp([MACD, MACDsign, MACDdiff]) return ret
def kde_sklearn(x, x_grid, bandwidth=0.2, **kwargs): """Kernel Density Estimation with Scikit-learn""" kde_skl = KernelDensity(bandwidth=bandwidth, **kwargs) x = ul.fnp(x) x_grid = ul.fnp(x_grid) print (x.shape) # if (x.shape[1] == 1): # x = x[:, np.newaxis] # x_grid = x_grid[:, np.newaxis] kde_skl.fit(x) # score_samples() returns the log-likelihood of the samples log_pdf = kde_skl.score_samples(x_grid) return np.exp(log_pdf)
def kde_sklearn(x, x_grid, bandwidth=0.2, **kwargs): """Kernel Density Estimation with Scikit-learn""" kde_skl = KernelDensity(bandwidth=bandwidth, **kwargs) x = ul.fnp(x) x_grid = ul.fnp(x_grid) print x.shape # if (x.shape[1] == 1): # x = x[:, np.newaxis] # x_grid = x_grid[:, np.newaxis] kde_skl.fit(x) # score_samples() returns the log-likelihood of the samples log_pdf = kde_skl.score_samples(x_grid) return np.exp(log_pdf)
def marketTiming(self,returns = [], ind_ret = [], mode = "Treynor-Mazuy"): # Investigate if the model is good. # We put a cuatric term of the error. returns = ul.fnp(returns) ind_ret = ul.fnp(ind_ret) if (returns.size == 0): returns = self.get_PortfolioReturn() if (ind_ret.size == 0): ind_ret = self.get_indexReturns() # Instead of fitting a line, we fit a parabola, to try to see # if we do better than the market return. If when Rm is higher, we have # higher beta, and if when Rm is lower, we have lower beta. So higher # and lowr return fitting a curve, cuatric, gl.scatter(ind_ret, returns, labels = ["Treynor-Mazuy", "Index Return", "Portfolio Return"], legend = ["Returns"]) ## Linear regression: Xres = ind_ret coeffs = bMl.get_linearRef(Xres, returns) Npoints = 10000 x_grid = np.array(range(Npoints))/float(Npoints) x_grid = x_grid*(max(ind_ret) - min(ind_ret)) + min(ind_ret) x_grid = x_grid.reshape(Npoints,1) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid), axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) gl.plot(x_grid, y_grid, legend = ["Linear Regression"], nf = 0) Xres = np.concatenate((ind_ret,np.power(ind_ret,2)),axis = 1) coeffs = bMl.get_linearRef(Xres, returns) x_grid_2 = np.concatenate((np.ones((Npoints,1)),x_grid,np.power(x_grid,2).reshape(Npoints,1) ),axis = 1) y_grid = x_grid_2.dot(np.array(coeffs)) # print y_grid.shape gl.plot(x_grid, y_grid, legend = ["Quadratic Regression"], nf = 0) print coeffs return 1
def draw_HMM_indexes(pi, A, Nchains=10, Nsamples=30): # If Nsamples is a number then all the chains have the same length # If it is a list, then each one can have different length K = pi.size # Number of clusters Chains_list = [] Cluster_index = range(K) Nsamples = ul.fnp(Nsamples) if (Nsamples.size == 1): # If we only have one sample Nsamples = [int(Nsamples)] * Nchains for nc in range(Nchains): Chains_list.append([]) sample_indx = np.random.choice(Cluster_index, 1, p=pi) Chains_list[nc].append(int(sample_indx)) for isam in range(1, Nsamples[nc]): # Draw a sample according to the previous state sample_indx = np.random.choice(Cluster_index, 1, p=A[sample_indx, :].flatten()) Chains_list[nc].append(int(sample_indx)) return Chains_list
def ADX(df, n = 14, n_ADX = 14): i = 0 UpI = [] DoI = [] while i + 1 < len(df.index): UpMove = df.get_value(df.index[i + 1], 'High') - df.get_value(df.index[i], 'High') DoMove = df.get_value(df.index[i], 'Low') - df.get_value(df.index[i + 1], 'Low') if UpMove > DoMove and UpMove > 0: UpD = UpMove else: UpD = 0 UpI.append(UpD) if DoMove > UpMove and DoMove > 0: DoD = DoMove else: DoD = 0 DoI.append(DoD) i = i + 1 i = 0 TR_l = [0] while i < len(df.index) -1: TR = max(df.get_value(df.index[i + 1], 'High'), df.get_value(df.index[i], 'Close')) - min(df.get_value(df.index[i + 1], 'Low'), df.get_value(df.index[i], 'Close')) TR_l.append(TR) i = i + 1 TR_s = pd.Series(TR_l) ATR = pd.Series(pd.ewma(TR_s, span = n, min_periods = n)) UpI = pd.Series(UpI) DoI = pd.Series(DoI) PosDI = pd.Series(pd.ewma(UpI, span = n, min_periods = n - 1) / ATR) NegDI = pd.Series(pd.ewma(DoI, span = n, min_periods = n - 1) / ATR) ADX = pd.ewma(abs(PosDI - NegDI) / (PosDI + NegDI), span = n_ADX, min_periods = n_ADX - 1) ADX = ul.fnp(ADX) return ADX
def RSI(df, n=20): i = 0 UpI = [0] DoI = [0] while i + 1 < len(df.index): UpMove = df.get_value(df.index[i + 1], 'High') - df.get_value( df.index[i], 'High') DoMove = df.get_value(df.index[i], 'Low') - df.get_value( df.index[i + 1], 'Low') if UpMove > DoMove and UpMove > 0: UpD = UpMove else: UpD = 0 UpI.append(UpD) if DoMove > UpMove and DoMove > 0: DoD = DoMove else: DoD = 0 DoI.append(DoD) i = i + 1 UpI = pd.Series(UpI) DoI = pd.Series(DoI) PosDI = pd.Series(pd.ewma(UpI, span=n, min_periods=n - 1)) NegDI = pd.Series(pd.ewma(DoI, span=n, min_periods=n - 1)) RSI = PosDI / (PosDI + NegDI) RSI = 100 * ul.fnp(RSI) return RSI
def __init__(self, w=20, h=12, lw=2): self.w = w # X-width self.h = h # Y-height self.lw = lw # line width self.nplots = 0 # Number of plots self.labels = [] # Labels for the plots self.plot_y = [] # Vectors of values to plot x-axis self.plot_x = [] # Vectors of values to plot y-axis # Set of nice colors to iterate over when we do not specify color self.colors = ["b", "g", "k", "r", "c", "m", "y"] self.colorIndex = 0 # Index of the color we are using. self.X = ul.fnp([]) self.legend = [] self.subplotting = 0 # In the beggining we are not subplotting self.ticklabels = [] self.Xticklabels = [] # For 3D self.Yticklabels = [] self.zorder = 1 # Zorder for plotting
def STOD(df, n=14, SK=3, SD=3): SOk = STOK(df, n=n) # SOd = pd.ewma(SOk, span = n, min_periods = n - 1) SOd = pd.Series.rolling(pd.Series(SOk.flatten()), window=SD, min_periods=SD - 1).mean() SOd = 1 * ul.fnp(SOd) return SOd
def empirical_1D_cdf(X): ## Fit a gaussian to the data or use the parameters given. # Gives the gaussian set of points for the std_K sorted_X = np.sort(X.flatten()) y_values = ul.fnp(range(1,X.size + 1))/float(X.size) return sorted_X, y_values
def empirical_1D_cdf(X): ## Fit a gaussian to the data or use the parameters given. # Gives the gaussian set of points for the std_K sorted_X = np.sort(X.flatten()) y_values = ul.fnp(range(1,X.size + 1))/float(X.size) return sorted_X, y_values
def ACCDIST(df, n=14): ad = (2 * df['Close'] - df['High'] - df['Low']) / (df['High'] - df['Low']) * df['Volume'] M = ad.diff(n - 1) N = ad.shift(n - 1) AD = M / N AD = ul.fnp(AD.values) return AD
def preprocess_data(self, X, Y, dataTransform=None): # Preprocess the variables X and Y ### First we transform everything to python format X = ul.fnp(X) Y = ul.fnp(Y) # Whe can plot several Y over same X. So NcY >= 1, NcX = 0,1 NpX, NcX = X.shape NpY, NcY = Y.shape if (X.size == 0): # If the X given is empty if (Y.size == 0): # If we are also given an empty Y Y = ul.fnp([]) else: X = ul.fnp(range(NpY)) # Create X axis with sample values self.X = X self.Y = Y # Get the automatic formatting of the X and Y axes !! # If we are given values to X, these could be of 3 types: # - Numerical: Then we do nothing # - String: Then it is categorical and we set the ticklabels to it # - Datetimes: We set the formatter ? self.formatXaxis = detect_AxisFormat(X) self.formatYaxis = detect_AxisFormat(Y) # print X,Y, self.formatXaxis, self.formatYaxis if (type(dataTransform) != type(None)): if (dataTransform[0] == "intraday"): # In this case we are going to need to transform the dates. openhour = dataTransform[1] closehour = dataTransform[2] self.formatXaxis = "intraday" # Virtual transformation of the dates ! self.Xcategories = self.X transfomedTimes = ul.transformDatesOpenHours( X, openhour, closehour) Mydetransfromdata = ul.deformatter_data(openhour, closehour, None) # Setting the static object of the function ul.detransformer_Formatter.format_data = Mydetransfromdata self.X = ul.fnp(transfomedTimes) if (self.formatXaxis == "categorical" ): # Transform to numbers and later we retransform self.Xcategories = self.X self.X = ul.fnp(range(NpX)) if (self.formatYaxis == "categorical" ): # Transform to numbers and later we retransform self.Ycategories = self.Y self.Y = ul.fnp(range(NpY)) # if (self.formatXaxis == "dates"): # Transform to numbers and later we retransform # self.Xcategories = self.X # self.X = ul.preprocess_dates(range(NpX)) return self.X, self.Y
def TRIX(df, n1 = 12, n2 = 12, n3 = 12): EX1 = df['Close'].ewm( span = n1, min_periods = n1 - 1).mean() EX2 = EX1.ewm( span = n2, min_periods = n2 - 1).mean() EX3 = EX2.ewm( span = n3, min_periods = n3 - 1).mean() # Get the returns Trix = EX3.pct_change(periods = 1) Trix = ul.fnp(Trix.values) return [EX1,EX2,EX3,Trix]
def TRIX(df, n=30): EX1 = pd.ewma(df['Close'], span=n, min_periods=n - 1) EX2 = pd.ewma(EX1, span=n, min_periods=n - 1) EX3 = pd.ewma(EX2, span=n, min_periods=n - 1) # Get the returns Trix = EX3.pct_change(periods=1) Trix = ul.fnp(Trix.values) return Trix
def TRIX(df, n1=12, n2=12, n3=12): EX1 = pd.ewma(df['Close'], span=n1, min_periods=n1 - 1) EX2 = pd.ewma(EX1, span=n2, min_periods=n2 - 1) EX3 = pd.ewma(EX2, span=n3, min_periods=n3 - 1) # Get the returns Trix = EX3.pct_change(periods=1) Trix = ul.fnp(Trix.values) return [EX1, EX2, EX3, Trix]
def TRIX(df, n1=12, n2=12, n3=12): EX1 = df['Close'].ewm(span=n1, min_periods=n1 - 1).mean() EX2 = EX1.ewm(span=n2, min_periods=n2 - 1).mean() EX3 = EX2.ewm(span=n3, min_periods=n3 - 1).mean() # Get the returns Trix = EX3.pct_change(periods=1) Trix = ul.fnp(Trix.values) return [EX1, EX2, EX3, Trix]
def plot_timeRegression( self, Xval, Yval, sigma, Xtr, Ytr, sigma_eps=None, labels=["Gaussian Process Estimation", "Time", "Price"], nf=0): # This is just a general plotting funcion for a timeSeries regression task: # Plot the function, the prediction and the 95% confidence interval based on # the MSE # eps is the estimated noise of the training samples. """ sigma is the std of each of the validation samples sigma_eps is the considered observation noise """ gl = self gl.plot(Xval, Yval, labels=labels, legend=["Estimated Mean"], nf=nf) gl.plot_filled(Xval, np.concatenate( [Yval - 1.9600 * sigma, Yval + 1.9600 * sigma], axis=1), alpha=0.5, legend=["95% CI f(x)"]) # If we are given some observaiton noise we also plot the estimation of it if type(sigma_eps) != type(None): # Ideally we have a func that tells us for each point, what is the observation noise. # We are suposed to know what is those values for training, # TODO: And for testing ? Would that help ? I think we are already assuming so in the computation of K. # The inner gaussian process can also be specified "alpha", we will research more about that later. # I guess it is for hesterodasticity. # We are gonna consider that the observation noise of the predicted samples is homocedastic. sigma = np.sqrt(sigma**2 + ul.fnp(sigma_eps)[0]**2) # TODO: maybe in the future differentiate between sigma_eps and dy gl.plot_filled(Xval, np.concatenate( [Yval - 1.9600 * sigma, Yval + 1.9600 * sigma], axis=1), alpha=0.2, legend=["95% CI y(x)"]) # TODO: what is this ec='None' if type(sigma_eps) != type(None): plt.errorbar(Xtr.ravel(), Ytr, sigma_eps, fmt='k.', markersize=10, label=u'Observations') else: gl.scatter(Xtr, Ytr, legend=["Training Points"], color="k")
def init_variables(self, w=20, h=12, lw=2): self.w = w # X-width self.h = h # Y-height self.lw = lw # line width self.prev_fig = [] # List that contains the previous plot. # When a new figure is done, we put the current one here and # create a new variable. Silly way to do it though self.fig = None self.axes = None self.nplots = 0 # Number of plots self.labels = [] # Labels for the plots self.plot_y = [] # Vectors of values to plot x-axis self.plot_x = [] # Vectors of values to plot y-axis # Set of nice colors to iterate over when we do not specify color # self.colors = ["k","b", "g", "r", "c", "m","y"] self.colors = [ cd["dark navy blue"], cd["golden rod"], cd["blood"], cd["chocolate"], cd["cobalt blue"], cd["cement"], cd["amber"], cd["dark olive green"] ] self.colorIndex = 0 # Index of the color we are using. self.X = ul.fnp([]) self.legend = [] self.subplotting_mode = 1 # Which functions to use for subplotting self.subplotting = 0 # In the beggining we are not subplotting self.ticklabels = [] # To save the ticklabels in case we have str in X self.Xticklabels = [] # For 3D self.Yticklabels = [] self.zorder = 1 # Zorder for plotting ## Store enough data to redraw and reference the plots for the interactivity self.plots_list = [] # List of the plots elements, self.plots_type = [] # Type of plot of every subplot self.axes_list = [] # We store the list of indexes self.Data_list = [] # We need to store a pointer of the data in the graph self.widget_list = [] # We need to store reference to widget so that self.num_hidders = 0
def BBANDS(df, n=20, seriesNames=["Close"]): MA = pd.rolling_mean(df[seriesNames], n) MSD = pd.rolling_std(df[seriesNames], n) BBh = MA + MSD * 2 BBl = MA - MSD * 2 ## Different types of BB bands ? TODO # b1 = 4 * MSD / MA # b2 = (df[seriesNames] - MA + 2 * MSD) / (4 * MSD) BB = ul.fnp([BBh.values, BBl.values]) return BB
def get_meanRange(timeSeries, window = 6): # This function moves a window of samples over a timeSeries and calculates # the mean and range to see if they are correlated and a transformation # of the original signal is needed. timeSeries = ul.fnp(timeSeries) means = [] ranges = [] # print timeSeries for i in range(timeSeries.size - window): samples = timeSeries[i:(i+window),:] rangei = max(samples) - min(samples) meani = np.mean(samples) means.append(meani) ranges.append(rangei) means = ul.fnp(means) ranges = ul.fnp(ranges) return means, ranges
def get_meanRange(timeSeries, window = 6): # This function moves a window of samples over a timeSeries and calculates # the mean and range to see if they are correlated and a transformation # of the original signal is needed. timeSeries = ul.fnp(timeSeries) means = [] ranges = [] # print timeSeries for i in range(timeSeries.size - window): samples = timeSeries[i:(i+window),:] rangei = max(samples) - min(samples) meani = np.mean(samples) means.append(meani) ranges.append(rangei) means = ul.fnp(means) ranges = ul.fnp(ranges) return means, ranges
def FibboSR(df): PP = (df['High'] + df['Low'] + df['Close']) / 3 RangeHL = df['High'] - df['Low'] S1 = PP - 0.382 * RangeHL S2 = PP - 0.618 * RangeHL S3 = PP - 1.0 * RangeHL R1 = PP - 0.382 * RangeHL R2 = PP - 0.618 * RangeHL R3 = PP - 1.0 * RangeHL PPSR = ul.fnp([PP, R1, S1, R2, S2, R3, S3]) return PPSR
def FibboSR(df): PP = (df['High'] + df['Low'] + df['Close']) / 3 RangeHL = df['High'] - df['Low'] S1 = PP - 0.382 * RangeHL S2 = PP - 0.618 * RangeHL S3 = PP - 1.0 * RangeHL R1 = PP - 0.382 * RangeHL R2 = PP - 0.618 * RangeHL R3 = PP - 1.0 * RangeHL PPSR = ul.fnp([PP,R1,S1,R2,S2,R3,S3]) return PPSR
def ACCDIST(df, n=14): MFM = (2 * df['Close'] - df['High'] - df['Low']) / (df['High'] - df['Low']) MFV = MFM * df['Volume'] # TODO: I dont get this # M = ad.diff(n - 1) # N = ad.shift(n - 1) # AD = M / N # # AD = pd.ewma(AD, span = n, min_periods = n - 1) # AD = pd.rolling_sum(AD, window = n) ADL = ul.fnp(MFV.values) ADL = np.cumsum(np.nan_to_num(ADL)) return ADL
def ACCDIST(df, n = 14): MFM = (2 * df['Close'] - df['High'] - df['Low']) / (df['High'] - df['Low']) MFV = MFM* df['Volume'] # TODO: I dont get this # M = ad.diff(n - 1) # N = ad.shift(n - 1) # AD = M / N # # AD = pd.ewma(AD, span = n, min_periods = n - 1) # AD = pd.rolling_sum(AD, window = n) ADL = ul.fnp(MFV.values) ADL = np.cumsum(np.nan_to_num(ADL)) return ADL
def PPSR(df): PP = (df['High'] + df['Low'] + df['Close']) / 3 R1 = 2 * PP - df['Low'] S1 = 2 * PP - df['High'] R2 = PP + df['High'] - df['Low'] S2 = PP - df['High'] + df['Low'] # R3 = df['High'] + 2 * (PP - df['Low']) # S3 = df['Low'] - 2 * (df['High'] - PP) PPSR = ul.fnp([PP, R1, S1, R2, S2]) # R3,S3 return PPSR
def PPSR(df): PP = (df['High'] + df['Low'] + df['Close']) / 3 R1 = 2 * PP - df['Low'] S1 = 2 * PP - df['High'] R2 = PP + df['High'] - df['Low'] S2 = PP - df['High'] + df['Low'] # R3 = df['High'] + 2 * (PP - df['Low']) # S3 = df['Low'] - 2 * (df['High'] - PP) PPSR = ul.fnp([PP,R1,S1,R2,S2]) # R3,S3 return PPSR
def plot_timeRegression(self,Xval, Yval, sigma, Xtr, Ytr,sigma_eps = None, labels = ["Gaussian Process Estimation","Time","Price"], nf = 0): # This is just a general plotting funcion for a timeSeries regression task: # Plot the function, the prediction and the 95% confidence interval based on # the MSE # eps is the estimated noise of the training samples. """ sigma is the std of each of the validation samples sigma_eps is the considered observation noise """ Xval = ul.fnp(Xval) Yval = ul.fnp(Yval) Xtr = ul.fnp(Xtr) Ytr = ul.fnp(Ytr) sigma = ul.fnp(sigma) sigma_eps = ul.fnp(sigma_eps) gl = self gl.plot(Xval,Yval, labels = labels, legend = ["Estimated Mean"], nf = nf) gl.plot_filled(Xval, np.concatenate([Yval - 1.9600 * sigma, Yval + 1.9600 * sigma],axis = 1), alpha = 0.5, legend = ["95% CI f(x)"]) # If we are given some observaiton noise we also plot the estimation of it if type(sigma_eps) != type(None): # Ideally we have a func that tells us for each point, what is the observation noise. # We are suposed to know what is those values for training, # TODO: And for testing ? Would that help ? I think we are already assuming so in the computation of K. # The inner gaussian process can also be specified "alpha", we will research more about that later. # I guess it is for hesterodasticity. # We are gonna consider that the observation noise of the predicted samples is homocedastic. sigma = np.sqrt(sigma**2 + ul.fnp(sigma_eps)[0]**2) # TODO: maybe in the future differentiate between sigma_eps and dy gl.plot_filled(Xval, np.concatenate([Yval - 1.9600 * sigma, Yval + 1.9600 * sigma],axis = 1), alpha = 0.2, legend = ["95% CI y(x)"]) # TODO: what is this ec='None' if type(sigma_eps) != type(None): if (ul.fnp(sigma_eps).size == 1): sigma_eps = np.ones((Xtr.size,1)) * sigma_eps plt.errorbar(Xtr.ravel(), Ytr, sigma_eps, fmt='k.', markersize=10, label=u'Observations') else: gl.scatter(Xtr, Ytr, legend = ["Training Points"], color = "k")
def preprocess_data(self, X, Y): # Preprocess the variables X and Y ### X axis date properties, just to be seen clearly ### First we transform everything to python format X = ul.fnp(X) Y = ul.fnp(Y) # print Y.shape # Each plot can perform several plotting on the same X axis # So there could be more than one NcX and NcY # NpX and NpY must be the same. NpX, NcX = X.shape NpY, NcY = Y.shape # If the type are labels if (X.size > 0): if (type(X[0, 0]).__name__ == "str" or type(X[0, 0]).__name__ == "string_"): self.ticklabels = X.T.tolist()[0] X = ul.fnp([]) # If we have been given an empty X if (X.size == 0): # If we are also given an empty Y if (Y.size == 0): return -1 # We get out. # This could be used to create an empty figure if # fig = 1 # If we wanted to plot something but we did not specify X. # We use the previous X, if it does not exist, we use range() else: X = ul.fnp(range(NpY)) # Create X axis # The given X becomes the new axis self.X = X self.Y = Y return X, Y
def diff(X, lag = 1, n = 1, cval = np.nan): # cval=np.NaN # It computes the lagged difference of the signal X[Nsamples, Nsig] # The output vector has the same length as X, the noncomputable values # are set as cval # n is the number of times we apply the diff. X = ul.fnp(X) Nsa, Nsig = X.shape for i in range(n): X = X[lag:,:] - X[:-lag,:] # print sd # Shift to the right npossitions unk_vec = np.ones((lag*n,Nsig)) * cval X = np.concatenate((unk_vec,X), axis = 0) return X
def diff(X, lag = 1, n = 1, cval = np.nan): # cval=np.NaN # It computes the lagged difference of the signal X[Nsamples, Nsig] # The output vector has the same length as X, the noncomputable values # are set as cval # n is the number of times we apply the diff. X = ul.fnp(X) Nsa, Nsig = X.shape for i in range(n): X = X[lag:,:] - X[:-lag,:] # print sd # Shift to the right npossitions unk_vec = np.ones((lag*n,Nsig)) * cval X = np.concatenate((unk_vec,X), axis = 0) return X
def shift(X, lag = 1, cval = 0): # cval=np.NaN # It shifts the X[Nsam][Ndim] lag positions to the right. or left if negative X = ul.fnp(X) Nsa, Nsig = X.shape if (lag > 0): filling = cval * np.ones((lag,Nsig)) X = np.concatenate((filling,X[:-lag,:]), axis = 0) elif(lag < 0): # print sd # Shift to the right npossitions filling = cval * np.ones((-lag,Nsig)) X = np.concatenate((X[-lag:,:],filling), axis = 0) return X
def fill_between(self, x, y1, y2 = 0, ax = None, where = None, alpha = 1.0 , color = "#888888", legend = [], *args, **kwargs): # This function fills a unique plot. ## We have to f*****g deal with dates !! # The fill function does not work properly with datetime64 x = ul.fnp(x) y1 = ul.fnp(y1) if (type(ax) == type(None)): ax = self.axes x = ul.preprocess_dates(x) x = ul.fnp(x) # print len(X), len(ul.fnp(Yi).T.tolist()[0]) # print type(X), type(ul.fnp(Yi).T.tolist()[0]) # print X.shape # print len(X.T.tolist()), len(ul.fnp(Yi).T.tolist()[0]) x = x.T.tolist()[0] # print x y1 = ul.fnp(y1).T.tolist()[0] if (where is not None): # print len(x), len(y1), len(where) where = ul.fnp(where) # where = np.nan_to_num(where) where = where.T.tolist()[0] y2 = ul.fnp(y2) if (y2.size == 1): y2 = y2[0,0] else: y2 = y2.T.tolist()[0] # print where[0:20] # print y2 # print len(where) # print x[0:5], y1[0:5] if (len(legend) == 0): legend = None else: legend = legend[0] ln = ax.fill_between(x = x, y1 = y1, y2 = y2, where = where, color = color, alpha = alpha, zorder = self.zorder, label = legend) # *args, **kwargs) self.plots_type.append(["fill"]) self.plots_list.append([ln]) # We store the pointers to the plots data_i = [x,y1,y2, where, ax, alpha,color, args, kwargs] self.Data_list.append(data_i) return ln
def fill_between(self, x, y1, y2 = 0, ax = None, where = None, alpha = 1.0 , color = "#888888", legend = [], *args, **kwargs): # This function fills a unique plot. ## We have to f*****g deal with dates !! # The fill function does not work properly with datetime64 x = ul.fnp(x) y1 = ul.fnp(y1) if (type(ax) == type(None)): ax = self.axes x = ul.preprocess_dates(x) x = ul.fnp(x) # print len(X), len(ul.fnp(Yi).T.tolist()[0]) # print type(X), type(ul.fnp(Yi).T.tolist()[0]) # print X.shape # print len(X.T.tolist()), len(ul.fnp(Yi).T.tolist()[0]) x = x.T.tolist()[0] # print x y1 = ul.fnp(y1).T.tolist()[0] if (where is not None): # print len(x), len(y1), len(where) where = ul.fnp(where) # where = np.nan_to_num(where) where = where.T.tolist()[0] y2 = ul.fnp(y2) if (y2.size == 1): y2 = y2[0,0] else: y2 = y2.T.tolist()[0] # print where[0:20] # print y2 # print len(where) # print x[0:5], y1[0:5] if (len(legend) == 0): legend = None else: legend = legend[0] ln = ax.fill_between(x = x, y1 = y1, y2 = y2, where = where, color = color, alpha = alpha, zorder = self.zorder, label = legend) # *args, **kwargs) self.plots_type.append(["fill"]) self.plots_list.append([ln]) # We store the pointers to the plots data_i = [x,y1,y2, where, ax, alpha,color, args, kwargs] self.Data_list.append(data_i) return ln
def STOK(df, n = 14, SK = 1): # TODO: This could be inf SOk = np.zeros((df.shape[0],1)) maxH = df['High'].rolling(window = n, min_periods = n-1).max() minL = df['Low'].rolling( window = n, min_periods = n-1).min() SOk = (df['Close'] - minL) / (maxH - minL) # SOk = SOk.fillna(1) # Instead of computing the min and max of each n-window, # we reuse the previous min and max :) if (SK > 1): SOk = SOk.ewm( span = SK, min_periods = SK - 1).mean() SOk = 100 * ul.fnp(SOk) return SOk
def shift(X, lag = 1, cval = np.nan): # cval=np.NaN # It shifts the X[Nsam][Ndim] lag positions to the right. or left if negative X = ul.fnp(X) if (len (X.shape) == 1): X = np.atleast_2d(X).T Nsa, Nsig = X.shape if (lag > 0): filling = cval * np.ones((lag,Nsig)) X = np.concatenate((filling,X[:-lag,:]), axis = 0) elif(lag < 0): # print sd # Shift to the right npossitions filling = cval * np.ones((-lag,Nsig)) X = np.concatenate((X[-lag:,:],filling), axis = 0) return X
def ATR(df, n = 14): i = 0 TR_l = [np.NaN] while i < len(df.index) -1: CHCL = df.get_value(df.index[i + 1], 'High') - df.get_value(df.index[i + 1], 'Low') CLPC = df.get_value(df.index[i + 1], 'Low') - df.get_value(df.index[i], 'Close') CHPC = df.get_value(df.index[i + 1], 'High') - df.get_value(df.index[i], 'Close') TR = np.max(np.abs([CHCL,CLPC,CHPC])) TR_l.append(TR) i = i + 1 TR_s = pd.Series(TR_l) # ATR = pd.ewma(TR_s, span = n, min_periods = n) ATR = TR_s.rolling( n).mean() ATR = ul.fnp(ATR.values) return ATR
def get_SMA(timeSeries, L, cval = np.NaN): """ Outputs the aritmetic mean of the time series using a rectangular window of size L""" timeSeries = ul.fnp(timeSeries) Nsam, Nsig = timeSeries.shape # Create window window = np.ones((L,1)) window = window/np.sum(window) for si in range(Nsig): ## Convolution for one of the signals signal = timeSeries[:,si] sM = get_convol(signal,window,cval) if (si == 0): total_sM = copy.deepcopy(sM) else: total_sM = np.concatenate((total_sM,sM), axis = 1) return total_sM
def BBANDS(df, n = 20, MA = None, seriesNames = ["Close"]): if (type(MA) == type(None)): MA = df[seriesNames].rolling(n, min_periods = n ).mean() MSD =df[seriesNames].rolling(n, min_periods = n).std() else: varis = (df[seriesNames] - MA)**2 MSD = pd.rolling_mean(varis,n, min_periods = n) MSD = np.sqrt(MSD) # TODO: do this properly BBh = MA + MSD *2 BBl = MA - MSD *2 ## Different types of BB bands ? TODO # b1 = 4 * MSD / MA # b2 = (df[seriesNames] - MA + 2 * MSD) / (4 * MSD) BB = ul.fnp([BBh.values, BBl.values]) return BB
def draw_HMM_indexes(pi, A, Nchains = 10, Nsamples = 30): # If Nsamples is a number then all the chains have the same length # If it is a list, then each one can have different length K = pi.size # Number of clusters Chains_list = [] Cluster_index = range(K) Nsamples = ul.fnp(Nsamples) if(Nsamples.size == 1): # If we only have one sample Nsamples = [int(Nsamples)]*Nchains for nc in range(Nchains): Chains_list.append([]) sample_indx = np.random.choice(Cluster_index, 1, p = pi) Chains_list[nc].append(int(sample_indx)) for isam in range(1,Nsamples[nc]): # Draw a sample according to the previous state sample_indx = np.random.choice(Cluster_index, 1, p = A[sample_indx,:].flatten()) Chains_list[nc].append(int(sample_indx)) return Chains_list
def compute_allocations(self,allocations): # This function performs a fast computing of the Return and STD # of a set of portfolios. No caring about Rf rate. allocations = ul.fnp(allocations) Nalloc = len(allocations) fast_flag = 1 # if (len(allocations) == 1): # fast_flag = 0; if (fast_flag == 0): ## Properway Using Library Function ## P_Ret_s = []; P_std_Return_s = [] for i in range (Nalloc): self.set_allocation(allocations[i]) expRet = self.get_PortfolioMeanReturn() stdRet = self.get_PortfolioStd() P_Ret_s.append(expRet) P_std_Return_s.append(stdRet) # Fast Way !## else: # problems with allocations and function fnp Allocations = ul.fnp(allocations) # print Allocations.shape # Allocations = Allocations.T ## Check the correct way of the matrix if (Allocations.shape[0] == len(self.symbol_names)): Allocations = Allocations.T P_Ret_s = np.dot(Allocations,ul.fnp(self.get_MeanReturns())); P_std_Return_s = np.dot(Allocations,self.get_covMatrix()) * ul.fnp(Allocations) P_std_Return_s = np.sqrt(np.sum(P_std_Return_s, axis = 1)) P_Ret_s = ul.fnp(P_Ret_s) P_std_Return_s = ul.fnp(P_std_Return_s) ## These are the expectd return and std for the differen portfolios return P_Ret_s, P_std_Return_s
def InvTransSampGrid(pdf_Values, pdf_Grid, Nsam): # To this function we give a grid of the pdf and it outputs variables # In this case cdf_Values are the values of the cdf and cdf_Grid the variables # pdf_Grid = [X1, X2...] pdf_Grid = ul.fnp(pdf_Grid) pdf_Values = ul.fnp(pdf_Values) print (pdf_Grid.shape) print (pdf_Grid[[-1],:].shape) pdf_Grid = np.concatenate((pdf_Grid, pdf_Grid[[-1],:]), axis = 0) cum_values = np.zeros(pdf_Grid.shape) print (cum_values.shape) ## Get the dimensions matrix HyperVolume = np.diff(pdf_Grid, axis = 0) print (HyperVolume.shape) NormedCDF = np.cumsum(pdf_Values*HyperVolume) NormedCDF = ul.fnp(NormedCDF) print (NormedCDF.shape) cum_values[1:,:] = NormedCDF print ("----") print ([pdf_Grid[:-1,:].shape, NormedCDF.shape]) inv_cdf = interpolate.interp1d(NormedCDF.flatten(), pdf_Grid[:-1,:].flatten()) r = np.random.rand(Nsam) return inv_cdf(r) # # #def vmfrnd(m, n, kappa, mu): # #RANDVONMISESFISHERM Random number generation from von Mises Fisher # #distribution. # #X = randvonMisesFisherm(m, n, kappa) returns n samples of random unit # #directions in m dimensional space, with concentration parameter kappa, # #and the direction parameter mu = e_m # #X = randvonMisesFisherm(m, n, kappa, mu) with direction parameter mu # #(m-dimensional column unit vector) # # if m < 2: # print('Dimension m must be > 2. Dimension set to 2'); # m = 2 # # if kappa < 0: # disp('appa must be >= 0, Set kappa to be 0'); # kappa = 0 # # # #the following algorithm is following the modified Ulrich's algorithm # # discussed by Andrew T.A. Wood in "SIMULATION OF THE VON MISES FISHER # # DISTRIBUTION", COMMUN. STATIST 23(1), 1994. # # # step 0 : initialize # b = (-2*kappa + np.sqrt(4*kappa**2 + (m-1)**2))/(m-1); # x0 = (1-b)/(1+b); # c = kappa*x0 + (m-1)*log(1-x0**2); # # # step 1 & step 2 # nnow = n; w = []; # # while(True): # ntrial = max(round(nnow*1.2),nnow+10) ; # Z = betarnd((m-1)/2,(m-1)/2,ntrial,1); # U = np.random.randn(ntrial,1); # W = (1-(1+b)*Z)/(1-(1-b)*Z); # # indicator = kappa*W + (m-1)*log(1-x0*W) - c >= log(U); # if sum(indicator) >= nnow # w1 = W(indicator); # w = [w ;w1(1:nnow)]; # break; # else # w = [w ; W(indicator)]; # nnow = nnow-sum(indicator); # %cnt = cnt+1;disp(['retrial' num2str(cnt) '.' num2str(sum(indicator))]); # end # end # # % step 3 # V = UNIFORMdirections(m-1,n); # X = [repmat(sqrt(1-w'.^2),m-1,1).*V ;w']; # # if muflag # mu = mu / norm(mu); # X = rotMat(mu)'*X; # end # end # # # function V = UNIFORMdirections(m,n) # % generate n uniformly distributed m dim'l random directions # % Using the logic: "directions of Normal distribution are uniform on sphere" # # V = zeros(m,n); # nr = randn(m,n); %Normal random # for i=1:n # while 1 # ni=nr(:,i)'*nr(:,i); % length of ith vector # % exclude too small values to avoid numerical discretization # if ni<1e-10 # % so repeat random generation # nr(:,i)=randn(m,1); # else # V(:,i)=nr(:,i)/sqrt(ni); # break; # end # end # end # # # #function rot = rotMat(b,a,alpha) #% ROTMAT returns a rotation matrix that rotates unit vector b to a #% #% rot = rotMat(b) returns a d x d rotation matrix that rotate #% unit vector b to the north pole (0,0,...,0,1) #% #% rot = rotMat(b,a ) returns a d x d rotation matrix that rotate #% unit vector b to a #% #% rot = rotMat(b,a,alpha) returns a d x d rotation matrix that rotate #% unit vector b towards a by alpha (in radian) #% #% See also . # #% Last updated Nov 7, 2009 #% Sungkyu Jung # # #[s1 s2]=size(b); #d = max(s1,s2); #b= b/norm(b); #if min(s1,s2) ~= 1 || nargin==0 , help rotMat, return, end # #if s1<=s2; b = b'; end # #if nargin == 1; # a = [zeros(d-1,1); 1]; # alpha = acos(a'*b); #end # #if nargin == 2; # alpha = acos(a'*b); #end #if abs(a'*b - 1) < 1e-15; rot = eye(d); return, end #if abs(a'*b + 1) < 1e-15; rot = -eye(d); return, end # #c = b - a * (a'*b); c = c / norm(c); #A = a*c' - c*a' ; # #rot = eye(d) + sin(alpha)*A + (cos(alpha) - 1)*(a*a' +c*c'); #end
def scatter_3D (self, X,Y,Z, labels = [], legend = [], # Basic Labelling color = None, lw = 2, alpha = 0.5, # Basic line properties nf = 0, na = 0, # New axis. To plot in a new axis # TODO: shareX option ax = None, position = [], projection = "2d", # Type of plot sharex = None, sharey = None, fontsize = 20,fontsizeL = 10, fontsizeA = 15, # The font for the labels in the axis xlim = None, ylim = None, xlimPad = None, ylimPad = None, # Limits of vision ws = None, Ninit = 0, loc = "best", dataTransform = None, xaxis_mode = None,yaxis_mode = None,AxesStyle = None, # Automatically do some formatting :) marker = [" ", None, None], project = "cart", join_points = "no" ): projection = "3d" X = ul.fnp(X) Y = ul.fnp(Y) Z = ul.fnp(Z) if (project == "spher"): Z = ul.convert_to_matrix(Z) R = Z THETA, PHI = np.meshgrid(X, Y) X = R * np.sin(THETA) * np.cos(PHI) Y = R * np.sin(THETA) * np.sin(PHI) Z = R * np.cos(THETA) # Management of the figure ax = self.figure_management(nf, na, ax = ax, sharex = sharex, sharey = sharey, projection = projection, position = position) # self.ax = self.fig.gca(projection='3d') # if (nf == 1): # color = 'copper' # else: # color = cm.coolwarm # TODO: make this a parameters # ax._axis3don = False colorFinal = self.get_color(color) surf = ax.scatter(X, Y, Z, color = "b", alpha = alpha) if (join_points =="yes"): surf = ax.plot(X[:,0], Y[:,0], Z[:,0], lw = lw, color = colorFinal) # ax.zaxis.set_major_locator(LinearLocator(10)) # ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # # ax.set_zlabel('Z Label') # fig.colorbar(surf, shrink=0.5, aspect=5) mins = np.min([np.min(X.flatten()), np.min(Y.flatten()), np.min(Z.flatten())]) maxs = np.max([np.max(X.flatten()), np.max(Y.flatten()), np.max(Z.flatten())]) # ax.set_xlim(mins, maxs) # ax.set_ylim(mins, maxs) # ax.set_zlim(mins, maxs) # ax.set_xlim(-1, 1) # ax.set_ylim(-1, 1) # ax.set_zlim(-1, 1) plt.show() return ax
return emaslow, emafast, emafast - emaslow def graphData(stockTD,MA1,MA2): ''' Use this to dynamically pull a stock: ''' try: print 'Currently Pulling',stock except Exception, e: print str(e), 'failed to organize pulled data.' ## Now we use the data # try: ## Basic data obtaining date = stockTD.index.values date = ul.fnp(date) date = grba.preprocess_dates(date) print date[0] print np.array(date[0]) # print pd.to_datetime(date[0]) # mdates.strpdate2num('%Y%m%d') date = mdates.date2num(date) # date = range(len(date)) openp = stockTD["Open"] closep = stockTD["Close"] highp = stockTD["High"] lowp = stockTD["Low"] volume = stockTD["Volume"] # Obtain data to plot
logpred += np.log(np.linalg.det(SigmaYYpredList[i])) logpred += (yerr_i.T).dot(np.linalg.inv(SigmaYYpredList[i])).dot(yerr_i) logpred = -logpred/2 logpred -= Ns/np.log(np.pi * 2) return logpred if (KM == 1): # Kalman Filter ! timeSeries = timeData.get_timeSeries(["Close"]); returns = timeData.get_timeSeriesReturn() # Variable to estimate. Nsig x Ndim returns = bMA.diff(timeSeries, cval = 0.000001) dates = timeData.get_dates() dates = ul.fnp(ul.datesToNumbers(dates)) dates = ul.fnp(range(dates.size)) ############################ KALMAN FILTER !! ####################################### Matrix_tr = np.concatenate([timeSeries,returns], axis = 1) # Nsam * 4 Ns,Nd = Matrix_tr.shape Nout = 1 # Number of output # System of equations A # Xt = A*Xt-1 + B*Ut + SystemNoise # Yt = Cx_k + MeasurementNoise # In the normal formulation Xt is a column vector !! Matrix_tr = Matrix_tr.T ########################### AR matrix ###############################
def yieldPriceStudy(self, initial_price = 80): # The initial price is for the aproximation of the # funciton with a cuadratic equation in one point. #### Obtain the yield-price curve from the structure Np = 100 ytm_list = np.linspace(0.001, 0.40, Np) prices = [] mdurations = [] convexities = [] for ytm in ytm_list: price = self.get_price(ytm = ytm) mduration = self.get_mduration(price = price) convexity = self.get_convexity(price = price) prices.append(self.get_price(ytm = ytm)) mdurations.append(mduration) convexities.append(convexity) gl.set_subplots(2,2) gl.plot(ytm_list,prices, labels = ["Yield curve", "Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3) gl.plot(ytm_list,prices, labels = ["Duration and Yield", "Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3) gl.plot(ytm_list,mdurations, na = 1, nf = 0, legend = ["Duration"], loc = 1) gl.plot(ytm_list,prices, labels = ["Convexity and Yield","Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3) gl.plot(ytm_list,convexities, na = 1, nf = 0, legend = ["Convexity"], loc = 1) ### Estimation of the yield courve around a point using the ## Duration and convexity. price = initial_price ytm = self.get_ytm(price) dytmList = np.linspace(-0.10, 0.10, 100) ## Obtain estimations estimations = [] for dytm in dytmList: eprice = self.estimate_price_DC(price = price, dytm = dytm, dy = 0.01) estimations.append(eprice) ## Obtain real values rael_values = [] for dytm in dytmList: rprice = self.get_price(ytm = ytm + dytm) rael_values.append(rprice) # Calculate the error ! rael_values = ul.fnp(rael_values) estimations = ul.fnp(estimations) error = np.abs(np.power(rael_values - estimations, 1)) gl.plot(ytm_list,prices, labels = ["Convexity and Yield","Yield to maturity" ,"Price of Bond"], legend = ["Yield curve"], loc = 3, lw = 4, color = "r") gl.scatter([ytm],[price], nf = 0, legend = ["Initial price"], loc = 3, lw = 4, color = "g") gl.plot(dytmList + ytm,estimations, nf = 0, legend = ["Estimation"], loc = 3, lw = 2, color = "b") gl.plot(dytmList + ytm,error, nf = 0, na = 1, legend = ["Error"], loc = 1, lw = 2, color = "b") ## The limit is the Spot Rate !! When we can do it continuously ## In this case the return is e^(RT)