def test_tool_fecon236_std(): '''Test standard deviation on population arg and data formats.''' # tool.std() should work for both numpy array and pandas DataFrame. darr = np.array([0, 1]) data = tool.todf(darr) assert round(tool.std(darr, population=True), 3) == 0.500 assert round(tool.std(data, population=True), 3) == 0.500 # When sample size is small, the difference is huge! assert round(tool.std(darr, population=False), 3) == 0.707 assert round(tool.std(data, population=False), 3) == 0.707
def gemrat(data, yearly=256, pc=True): '''Compute annualized geometric mean rate for given data. Output will be more accurate than the method implicit in georet() since the kurtosis of differenced log data matters. Argument pc will present appropriate output in percentage form. ''' rat = tool.diflog(data, lags=1) # ^First difference of log(data). arr = tool.df2a(rat) # # Routine stat calculations on our array: N = len(arr) mu = np.mean(arr) sigma = tool.std(arr) k_Pearson = (sum((arr - mu)**4)/N) / sigma**4 # For kurtosis details, see our kurtfun(). # # Annualize... muy = mu * yearly sigmay = sigma * np.sqrt(yearly) grate = gemrate(muy, sigmay, k_Pearson, yearly=1) # Using gemrate(muy, sigmay, k_Pearson, yearly=256) instead could # result in: geometric mean > arithmetic mean, which is a contradiction. if pc: return [grate*100, muy*100, sigmay*100, k_Pearson, yearly, N] else: return [grate, muy, sigmay, k_Pearson, yearly, N]
def gm2_vols_fit(data, b=2.5): '''Estimate GM(2) VOLATILITY parameters including mu, given b.''' # Our data is presumed to be prices of some financial asset. rat = tool.diflog(data, lags=1) # ^First difference of log(data). arr = tool.df2a(rat) # # Routine stat calculations on our array: N = len(arr) mu = np.mean(arr) sigma = tool.std(arr) k_Pearson = (sum((arr - mu)**4)/N) / sigma**4 # For kurtosis details, see our kurtfun(). # specs = gm2_main(k_Pearson, sigma, b) # [[a, b], [p, a*sigma], [1-p, b*sigma]] sigma1 = specs[1][1] sigma2 = specs[2][1] q = specs[2][0] return [mu, sigma1, sigma2, q, k_Pearson, sigma, b, N]