Exemplo n.º 1
0
    def _update(self, weight: np.ndarray, grad: np.ndarray, cache: Dict[str, Any]) -> Dict[str, Any]:
        g_avg_t_minus_1 = cache.get('g_avg', np.zeros_like(grad))
        update_avg_t_minus_1 = cache.get('u_avg', np.zeros_like(grad))

        g_avg = linear_combination(g_avg_t_minus_1, grad**2, self._delta)

        update = grad * ((np.sqrt(update_avg_t_minus_1 + self._eps)) / np.sqrt(g_avg + self._eps))

        update_avg = linear_combination(update_avg_t_minus_1, update**2, self._delta)
        weight -= update

        return {'g_avg': g_avg, 'u_avg': update_avg}
Exemplo n.º 2
0
    def predict(self, X, Y=None):
        data = self._data_fetcher(X, last=True)
        df_features = create_features(data)
        df_features = df_features.round(2)
        X_train, X_test, y_train, y_test = create_X_Y(df_features)
        self.lr.fit(X_train, y_train)
        y_pred = self.lr.predict(X_test)
        y_pred = y_pred.round(2)
        score = self.lr.score(X_test, y_test)
        mean_absolute_error = metrics.mean_absolute_error(y_test, y_pred)
        mean_squared_error = metrics.mean_squared_error(y_test, y_pred)
        sqrt_mean_squared_error = np.sqrt(
            metrics.mean_squared_error(y_test, y_pred))

        predictions = self.lr.predict(X_train)
        predictions = predictions.round(2)
        prediction_for_y_train = predictions.flatten()[-1]

        #         shape = df_features.shape()
        #         #Classification
        #         for i in range(1,shape[1]+1):
        #             if df_features[i]:
        #
        #         df_features['buysell']

        return {
            "prediction_for_y_train": prediction_for_y_train,
            "r2_score": score,
            "mean_absolute_error": mean_absolute_error,
            "mean_squared_error": mean_squared_error,
            "sqrt_mean_squared_error": sqrt_mean_squared_error,
        }
Exemplo n.º 3
0
def range_volatility(ticker, components): 
	# Parse and check: find components matching dates, ensure start and end are present, ensure dates are valid
	today, dates = current_date(), []
	for each in components: 
		if PATTERNS['valid_date'].match(each):
			dates.append(each)
	if len(dates)!=2:
		return {"message": Response.vol_required_dates(ticker)}
	for each in dates: 
		if each > today: 
			return {"message": Response.invalid_date(each)}
		try: 
			date = datetime.datetime.strptime(each, '%Y-%m-%d')
		except ValueError: 
			return {"message": Response.invalid_date(each)}

	# Volatility Calculation
	dates = sorted(dates)
	start, end = dates[0], dates[1]
	try:
		quotes = data.DataReader(ticker, 'google')['Close'].loc[start:end]
		if len(quotes) < 10: 
			return {"message": Response.vol_range_size(ticker)}
	except Exception:
		return {"message": Response.data_notfound(ticker)}
	logreturns = np.log(quotes / quotes.shift(1))
	vol = round(np.sqrt(252*logreturns.var()), 5)
	return {"message" : Response.range_vol(ticker, start, end, vol)}
 def _setPortfolioInfo(self):
     port_annual_var: float = round(np.dot(self._weights.T, np.dot(self._dataSimpleCovarianceAnnual, self._weights)), 5)
     port_annual_volatility: float = round(np.sqrt(port_annual_var), 5)
     port_annual_simple_ret: float = round(np.sum(self._stats.SimpleReturnsNan.mean() * self._weights) * 252, 5)
     print('Port Ann Ret', str(round(port_annual_var, 5)*100)+'%')
     print('Port Ann Volatility/ Risk', str(round(port_annual_volatility, 5)*100)+'%')
     print('Port Ann Variance', str(round(port_annual_simple_ret, 5)*100)+'%')
     '''
Exemplo n.º 5
0
def hist_vol(sym, days=10):
    try:
        quotes = DataReader(sym, 'yahoo')['Close'][-days:]
    except Exception:
        print "Problem getting historical volatility!"
        raise SystemExit(code)
        return None, None
    logreturns = np.log(quotes / quotes.shift(1))
    vol = np.sqrt(252*logreturns.var()) #252 trading days in year (annualized volatility)
    return float(vol)
Exemplo n.º 6
0
    def trailing_volatility(ticker, days):
        """
		Returns equally-weighted historical volatility for a stock during the last X trading days
		(annualized std deviation of daily log returns) using market close prices.
		Google API - data available from Jan 4, 2010 to present and is adjusted for stock splits.
		"""

        try:
            ticker_input = "WIKI/" + ticker
            quotes = quandl.get(ticker_input, order='desc')['Close'][:days]
        except Exception:
            return False
        logreturns = np.log(quotes / quotes.shift(1))
        return round(np.sqrt(252 * logreturns.var()), 5)
Exemplo n.º 7
0
def trailing_volatility(ticker, components):
	days=None
	for each in components: 
		if PATTERNS['tvol'].match(each):
			days = int(each)
			break
	if days==None:
		return {"message": Response.trailing_days(ticker)}
	try:
		quotes = data.DataReader(ticker, 'google')['Close'][-days:]
	except Exception:
		return {"message": Response.data_notfound(ticker)}
	logreturns = np.log(quotes / quotes.shift(1))
	vol = round(np.sqrt(252*logreturns.var()), 5)
	return {"message" : Response.trailing_vol(days, ticker, vol)}
 def PlotMatrix(self) -> plt:
     prf_returns = (self._stats.Returns.pct_change() + 1)[1:]
     avg_return = (prf_returns-1).mean()
     daily_pct_change = np.log(self._stats.Returns.pct_change() + 1)
     vols = daily_pct_change.std() * np.sqrt(252)
     plt.style.use('seaborn')
     plt.rcParams['date.epoch'] = '0000-12-31'
     fig, ax = plt.subplots(1, 1, figsize=(self._a_float, self._a_float/2.0))
     #print('AX', type(ax)) AX <class 'matplotlib.axes._subplots.AxesSubplot'>
     fig.suptitle(self._basics.Title)
     ax.set(ylabel='Simple Return Std', xlabel='Simple Return Mean')
     ax.scatter(vols, avg_return)
     for i, txt in enumerate(list(vols.index)):
         ax.annotate(txt, (vols[i], avg_return[i]))
     plt.tight_layout()
     #plt.show()
     return plt
Exemplo n.º 9
0
 def _setMatrices(self, portfolio_data: DataFrame, log_ret: DataFrame,
                  cov_mat: DataFrame):
     for i in range(self._threshold):
         weight_arr: np.ndarray = np.random.uniform(
             size=len(portfolio_data.columns))
         weight_arr = weight_arr / np.sum(weight_arr)
         # saving weights in the array
         self._weight_matrix[i, :] = weight_arr
         # Portfolio Returns
         annual_weighted_log_ret: float = (
             (np.sum(log_ret.mean() * weight_arr)) + 1)**252 - 1
         # Saving Portfolio returns
         self._annual_weighted_log_return_matrix[
             i] = annual_weighted_log_ret
         # Saving Portfolio Risk
         portfolio_sd: float = np.sqrt(
             np.dot(weight_arr.T, np.dot(cov_mat, weight_arr)))
         self._risk_matrix[i] = portfolio_sd
         # Portfolio Sharpe Ratio
         # Assuming 0% Risk Free Rate
         sr: float = annual_weighted_log_ret / portfolio_sd
         self._sharpe_ratio_matrix[i] = sr
Exemplo n.º 10
0
 def _getAnnualReturnStd(self, a_series: Series = Series()) -> float:
     return self.__roundFloat(np.std(a_series) * np.sqrt(252))
Exemplo n.º 11
0
from pandas import np
import pandas_datareader.data as web

def historical_volatility(sym, days): # stock symbol, number of days
    "Return the annualized stddev of daily log returns of picked stock"
    try:
        # past number of 'days' close price data, normally between (30, 60)
        quotes = web.DataReader(sym, 'yahoo')['Close'][-days:] 
    except Exception, e:
        print "Error getting data for symbol '{}'.\n".format(sym), e
        return None, None
    logreturns = np.log(quotes / quotes.shift(1))
    # return square root * trading days * logreturns variance
    # NYSE = 252 trading days; Shanghai Stock Exchange = 242; Tokyo Stock Exchange = 246 days?
    return np.sqrt(252*logreturns.var()) 
    
    
if __name__ == "__main__":
    print historical_volatility('FB', 30) # facebook
Exemplo n.º 12
0
def wgn(X, snr):
    P_signal = np.sum(abs(X)**2) / (len(X))
    P_noise = P_signal / 10**(snr / 10.0)
    #return np.random.randn(X.shape[1])*np.sqrt(P_noise)
    return np.random.randn(len(X)) * np.sqrt(P_noise)
 def __init__(self, y_stocks: list):
     self._a_float = 3 * math.log(y_stocks[0].TimeSpan.MonthCount)
     self._a_suffix = y_stocks[0].Column
     self._a_ts = y_stocks[0].TimeSpan
     self._a_length = len(y_stocks)
     iso_weight: float = round(1.0 / len(y_stocks), 3)
     self._stocks = y_stocks
     self._weights = np.array(len(y_stocks) * [iso_weight], dtype=float)
     self._basics = PortfolioBasics(y_stocks, self._a_float, self._legend_place)
     self._stats = PortfolioStats(self._weights, self._basics)
     self._final = PortfolioFinal(y_stocks, self._a_float, self._legend_place)
     print('Volatility\t\t\t\t\t', self._final.Volatility)
     print('Annual Expected Return\t\t', self._final.AnnualExpectedReturn)
     print('Risk Free Rate\t\t\t\t', self._final.RiskFreeRate)
     print('Free 0.005 Sharpe Ratio\t\t', self._final.Free005SharpeRatio)
     print('Kurtosis\n', self._final.KurtosisSeries)
     print('Skewness\n', self._final.SkewnessSeries)
     print('Frequency\n', self._final.Frequency)
     self._final.Plot().show()
     exit(1234)
     self._dataSimpleCorrelation = self._stats.SimpleReturnsNan.corr()
     self._dataSimpleCovariance = self._stats.SimpleReturnsNan.cov()
     self._dataSimpleCovarianceAnnual = self._dataSimpleCovariance * 252
     self._dataSimpleSummary = self._stats.SimpleReturnsNanSummary
     self._dataWeightedReturns = self._stats.SimpleWeightedReturns
     # axis =1 tells pandas we want to add the rows
     self._portfolio_weighted_returns = round(self._dataWeightedReturns.sum(axis=1), 5)
     print('7', self._portfolio_weighted_returns.head())
     print('7', self._stats.SimpleWeightedReturnsSum.head())
     #self._dataWeightedReturns['PORTFOLIOWeighted'] = portfolio_weighted_returns
     portfolio_weighted_returns_mean = round(self._portfolio_weighted_returns.mean(), 5)
     print('port_ret mean', portfolio_weighted_returns_mean)
     print(round(self._stats.SimpleWeightedReturnsSum.mean(), 5))
     portfolio_weighted_returns_std = round(self._portfolio_weighted_returns.std(), 5)
     print('port_ret std', portfolio_weighted_returns_std)
     self._portfolio_weighted_returns_cum: Series = round((self._portfolio_weighted_returns + 1).cumprod(), 5)
     #self._dataWeightedReturns['PORTFOLIOCumulative'] = self._portfolio_weighted_returns_cum
     print('$', self._dataWeightedReturns.head())
     self._portfolio_weighted_returns_geom = round(np.prod(self._portfolio_weighted_returns + 1) ** (252 / self._portfolio_weighted_returns.shape[0]) - 1, 5)
     print('geometric_port_return', self._portfolio_weighted_returns_geom)
     self._portfolio_weighted_annual_std = round(np.std(self._portfolio_weighted_returns) * np.sqrt(252), 5)
     print('port_ret annual', self._portfolio_weighted_annual_std)
     self._portfolio_weighted_sharpe_ratio = round(self._portfolio_weighted_returns_geom / self._portfolio_weighted_annual_std, 5)
     print('port_sharpe_ratio', self._portfolio_weighted_sharpe_ratio)
     print('%', self._stats.Returns.head())
     self._data_returns_avg = self._getDataReturnsAverage(self._stats.Returns)
     print('^', self._data_returns_avg.head())
     daily_log_pct_changes: DataFrame = np.log(self._stats.Returns.pct_change() + 1) #avant portfolio
     daily_log_pct_changes.columns = daily_log_pct_changes.columns + 'LogReturn'
     print('&', daily_log_pct_changes.head())
     daily_log_volatilities: DataFrame = (daily_log_pct_changes.std() * np.sqrt(252)).to_frame()
     daily_log_volatilities.columns = ['Volatility']
     print('*', daily_log_volatilities)
     port_daily_simple_ret: float = round(np.sum(self._stats.SimpleReturnsNan.mean()*self._weights), 5)
     port_weekly_simple_ret: float = round(4.856 * port_daily_simple_ret, 5)
     port_monthly_simple_ret: float = round(21 * port_daily_simple_ret, 5)
     port_quarterly_simple_ret: float = round(63 * port_daily_simple_ret, 5)
     port_yearly_simple_ret: float = round(252 * port_daily_simple_ret, 5)
     print('port_daily_simple_ret', str(100*port_daily_simple_ret) + '%')
     print('port_weekly_simple_ret', str(100*port_weekly_simple_ret) + '%')
     print('port_monthly_simple_ret', str(100*port_monthly_simple_ret) + '%')
     print('port_quarterly_simple_ret', str(100*port_quarterly_simple_ret) + '%')
     print('port_yearly_simple_ret', str(100*port_yearly_simple_ret) + '%')
     self._setPortfolioInfo()
     self._optimizer = PortfolioOptimizer(self._legend_place, self._a_float, self._stats, self._basics.Data)
     self._stock_market_index = SnP500Index('yahoo', "^GSPC", self._a_ts)
     self._linear_reg = PortfolioLinearReg(self._stock_market_index, self._stats.Returns)
     print(f'The portfolio beta is {self._linear_reg.Beta}, for each 1% of index portfolio will move {self._linear_reg.Beta}%')
     print('The portfolio alpha is ', self._linear_reg.Alpha)
     print('_', self._basics.DataLogReturns.head())
     cov_mat_annual = self._basics.DataLogReturns.cov() * 252
     print('-', cov_mat_annual)
Exemplo n.º 14
0
def calculate(param, param1, param2, param3):
    xDis = abs(param - param2)
    yDis = abs(param1 - param3)
    from pandas import np
    distance = np.sqrt((xDis ** 2) + (yDis ** 2))
    return int(distance)
Exemplo n.º 15
0
def order_path(gray_img, locs, xy_to_pixel):
    # starting location
    # TODO: pass this in dynamically
    start_loc = locs[0]

    # construct 2d cost map where each entry is cost to move from neighboring pixel
    h, w = np.shape(gray_img)

    # iterate over the entire image and build graph for dijkstra calculations
    print('Converting map into graph.')
    graph = Graph()
    # NOTE: this isn't using the values in the map yaml (0.196) for free_thresh but instead assumes
    # 0 = occupied,  205 = unknown, 254 = free
    free_thresh = 220
    diag_cost = np.sqrt(2)
    lin_cost = 1
    for py in range(1, h - 1):
        for px in range(1, w - 1):
            if gray_img[py][px] < free_thresh:
                # obstacle -- skip it
                continue
            i = py * w + px
            for py2 in range(py - 1, py + 2):
                for px2 in range(px - 1, px + 2):
                    if gray_img[py2][px2] < free_thresh:
                        # obstacle -- skip it
                        continue
                    if px == px2 and py == py2:
                        # same as "center" pixel -- skip it
                        continue

                    i2 = py2 * w + px2
                    if px == px2 or py == py2:
                        # straight up or down pixel
                        graph.add_edge(i, i2, lin_cost)
                    else:
                        # diag pixel
                        graph.add_edge(i, i2, diag_cost)

    # perform dijkstra cost calculation for each disinfection location pair
    # to build cost array for traveling salesman problem
    print('Building cost matrix for TSP problem.')
    num_locs = len(locs)
    costs = [[0] * num_locs for i in range(num_locs)]
    for loc_i in range(num_locs):
        costs[loc_i][loc_i] = 0
        (px, py) = xy_to_pixel(locs[loc_i][0], locs[loc_i][1])
        src_i = py * w + px
        for loc_j in range(loc_i + 1, num_locs):
            (px, py) = xy_to_pixel(locs[loc_j][0], locs[loc_j][1])
            dst_i = py * w + px
            path_info = find_path(graph, src_i, dst_i)
            cost = int(round(path_info.total_cost))
            costs[loc_i][loc_j] = cost
            costs[loc_j][loc_i] = cost

    # find disinfection location closest to and furthest from starting location (i.e., where robot currently is)
    # this will serve as the start and end locations for the tsp problem
    print('Finding starting and ending disinfection locations.')
    (px, py) = xy_to_pixel(start_loc[0], start_loc[1])
    src_i = py * w + px
    start_i = -1
    end_i = -1
    min_cost = sys.maxsize
    max_cost = 0
    for loc_i in range(num_locs):
        (px, py) = xy_to_pixel(locs[loc_i][0], locs[loc_i][1])
        dst_i = py * w + px
        path_info = find_path(graph, src_i, dst_i)
        cost = int(round(path_info.total_cost))
        if cost < min_cost:
            min_cost = cost
            start_i = loc_i
        if cost > max_cost:
            max_cost = cost
            end_i = loc_i

    # feed cost array into tsp algorithm
    print('Solving TSP problem.')
    data = create_data_model(costs, start_i, end_i)
    tour = solve_tsp(data)

    return tour
Exemplo n.º 16
0
from pandas import np
from pandas_datareader.data import DataReader


def ewma_volatility(sym, days):
    try:
        quotes = DataReader(sym, 'google')['Close'][-days:]
    except Exception, e:
        print "Error getting data for symbol '{}'.\n".format(sym), e
        return None, None

    logreturns = np.log(quotes / quotes.shift(1))

    l = 0.94  #lambda
    temp = 1 - l

    weight = []

    for logreturn in logreturns:
        logreturn = logreturn * logreturn * temp
        weight.append(logreturn)
        temp = temp * l

    ewma = pandas.Series(weight, logreturns.index)
    return np.sqrt(252 * ewma.sum())


if __name__ == "__main__":
    print ewma_volatility('GOOG', int(sys.argv[1])) * 100
Exemplo n.º 17
0
# @Last Modified by:   boyac
# @Last Modified time: 2016-05-02 19:09:29

from pandas import np
import pandas_datareader.data as web


def gk_vol(sym, days):
    """"
    Return the annualized stddev of daily log returns of picked stock
    Historical Open-High-Low-Close Volatility: Garman Klass
    sigma**2 = ((h-l)**2)/2 - (2ln(2) - 1)(c-o)**2
    ref: http://www.wilmottwiki.com/wiki/index.php?title=Volatility
    """

    try:
        o = web.DataReader(sym, 'yahoo')['Open'][-days:]
        h = web.DataReader(sym, 'yahoo')['High'][-days:]
        l = web.DataReader(sym, 'yahoo')['Low'][-days:]
        c = web.DataReader(sym, 'yahoo')['Close'][-days:]
    except Exception, e:
        print "Error getting data for symbol '{}'.\n".format(sym), e
        return None, None
    sigma = np.sqrt(252 * sum((np.log(h / l))**2 / 2 - (2 * np.log(2) - 1) *
                              (np.log(c / o)**2)) / days)
    return sigma


if __name__ == "__main__":
    print gk_vol('FB', 30)  # facebook
Exemplo n.º 18
0
    data = data_fetcher(ticker, last=True)
    data = data.drop('ticker', axis=1)
    data = data.round(2)
    X = data.drop('close', axis=1)
    Y = data[['close']]
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        Y,
                                                        test_size=test_size)

    regressor = LinearRegression()
    regressor.fit(X_train, y_train)

    y_pred = regressor.predict(X_test)
    y_pred = y_pred.round(2)
    mean_absolute_error = metrics.mean_absolute_error(y_test, y_pred)
    mean_squared_error = metrics.mean_squared_error(y_test, y_pred)
    sqrt_mean_squared_error = np.sqrt(
        metrics.mean_squared_error(y_test, y_pred))

    y_pred_from_train = regressor.predict(X_train)
    y_pred_from_train = y_pred_from_train.round(2)

    # last_close_price = get_last_close_price(ticker)

    print(f'Prediction for ticker {ticker}:   {y_pred_from_train}'
          f'\n\nScore given with test_size {test_size} is:'
          f'\n\tmean_absolute_error": {mean_absolute_error}'
          f'\n\tmean_squared_error: {mean_squared_error}'
          f'\n\tsqrt_mean_squared_error": {sqrt_mean_squared_error}'
          # f'\n\nLast close price was: {last_close_price.get("close")}'
          )
Exemplo n.º 19
0
def volatility(df):
    quotes = df['close']
    logreturns = np.log(quotes / quotes.shift(1))
    vol = np.sqrt(252 * logreturns.var())
    return vol