def calc_step(median_force, element_size): """Calculation of density and ssa from median of force and element size. This is the actual math described in the publication. :param median_force: Median of force. :param element_size: Element size. :return: Tuple containing density and ssa value. """ l = element_size fm = median_force # Equation 9 in publication a1 = 420.47 a2 = 102.47 a3 = -121.15 a4 = -169.96 density = a1 + a2 * np.log(fm) + a3 * np.log(fm) * l + a4 * l # Equation 11 in publication c1 = 0.131 c2 = 0.355 c3 = 0.0291 lc = c1 + c2 * l + c3 * np.log(fm) # Equation 12 in publication ssa = 4 * (1 - (density / DENSITY_ICE)) / lc return density, ssa
def gmean(a, axis=0, dtype=None): # A copy/paste of scipy.stats.mstats.gmean function to # avoid the scipy dependency if not isinstance(a, np.ndarray): # if not an ndarray object attempt to convert it log_a = np.log(np.array(a, dtype=dtype)) elif dtype: # Must change the default dtype allowing array type if isinstance(a, np.ma.MaskedArray): log_a = np.log(np.ma.asarray(a, dtype=dtype)) else: log_a = np.log(np.asarray(a, dtype=dtype)) else: log_a = np.log(a) return np.exp(log_a.mean(axis=axis))
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 populate_df(self, start, end=None): """ :param start: start date of the historical data (ex: '2010-01-01') :param end: end date of the historical data """ self.df = helper.get_historical_data(self.ticker, start, end) self.df['daily_return'] = np.log(self.df['Adj Close'].pct_change() + 1)
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)
def compute_gap(clustering, data, k_max=5, n_references=5): if len(data.shape) == 1: data = data.reshape(-1, 1) reference = np.random.rand(*data.shape) reference_inertia = [] for k in range(1, k_max + 1): local_inertia = [] for _ in range(n_references): clustering.n_clusters = k assignments = clustering.fit_predict(reference) local_inertia.append(compute_inertia(assignments, reference)) reference_inertia.append(np.mean(local_inertia)) ondata_inertia = [] for k in range(1, k_max + 1): clustering.n_clusters = k assignments = clustering.fit_predict(data) ondata_inertia.append(compute_inertia(assignments, data)) gap = np.log(reference_inertia) - np.log(ondata_inertia) return gap, np.log(reference_inertia), np.log(ondata_inertia)
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)
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
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)
def _getLogReturns(self, a_df: DataFrame = DataFrame()) -> DataFrame: new_df: DataFrame = np.log(a_df / a_df.shift(1)) new_df.columns = new_df.columns.str.replace(self._column, 'LogReturn') return new_df
# @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
def volatility(df): quotes = df['close'] logreturns = np.log(quotes / quotes.shift(1)) vol = np.sqrt(252 * logreturns.var()) return vol
from pandas import np import pandas_datareader.data as web import threading def hv(sym, days): """ Return the annualized stddev of daily log returns of picked stock, and auto refresh data loading every 5 seconds. """ try: # past number of 'days' close price data, normally between (30, 60) hv.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(hv.quotes / hv.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 hv('FB', 30) # facebook/ 0.294282265956 threading.Timer(5, hv.quotes).start() # [BUG]Cannot read Time Series data """ Exception in thread Thread-1: Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run()
def test_log(self): y = [0.08, 1, 2, 4, 8, 16] log_y = np.log(y) print(f"y ={y}") print(f"log_y={log_y}")
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
def _getLogDailyReturns(self, a_df: DataFrame = DataFrame()) -> DataFrame: new_df: DataFrame() = np.log(a_df / a_df.shift(1)) new_df.columns = new_df.columns.str.replace(self._portfolio_basics.Column, 'LogDailyRet') return new_df
# facecolor:背景颜色 # edgecolor:边框颜色 # frameon:是否显示边框 # figure = plt.figure(num='gg', figsize=(5, 3), dpi=None, facecolor='red', edgecolor=None, frameon=True) # plt.show() # 创建单个子图 # nrows:subplot的行数 # ncols:subplot的列数 # sharex:所有subplot应该使用相同的X轴刻度(调节xlim将会影响所有的subplot) # sharex:所有subplot应该使用相同的Y轴刻度(调节ylim将会影响所有的subplot) # subpolt_kw:用于创建各subplot的关键字字典 # **fig_kw 创建figure时的其他关键字 # plt.subplot(nrows, ncols, sharex, sharey, subplot_kw, **fig_kw) x = np.arange(0, 100) # 第一行左图 #作图1 plt.subplot(221) plt.plot(x, x) #作图2 plt.subplot(222) plt.plot(x, -x) #作图3 plt.subplot(223) plt.plot(x, x**2) plt.grid(color='r', linestyle='--', linewidth=1, alpha=0.3) #作图4 plt.subplot(224) plt.plot(x, np.log(x)) plt.show()