diff --git a/alphalens/sharpe_test.py b/alphalens/sharpe_test.py new file mode 100644 index 00000000..1d399c48 --- /dev/null +++ b/alphalens/sharpe_test.py @@ -0,0 +1,141 @@ +import numpy as np +import scipy.integrate as integrate + + +def _compute_vhat(ret1, ret2): + nu = [np.mean(ret1), np.mean(ret2), np.mean(ret1 ** 2), np.mean(ret2 ** 2)] + nux = np.vstack([ret1 - nu[0], ret2 - nu[1], ret1 ** 2 - nu[2], ret2 ** 2 - nu[3]]).T + return nux + + +def _ar2(y, nlag, const=1): + # ols estimates for the AR(k) model + results = {} + n = y.shape[0] + results['y'] = y + results['negs'] = 1 + cols = [] + if const == 1: + cols.append(np.ones(n)) + for i in range(nlag): + cols.append(np.roll(y, i + 1)) + + x = np.vstack(cols).T + x = x[nlag:, :] + y = y[nlag:] + n_adj = len(y) + + b0 = np.linalg.lstsq(x.T.dot(x), x.T.dot(y), rcond=None)[0] + p = nlag + const + sige = ((y - x.dot(b0)).T.dot(y - x.dot(b0))) / (n - p + 1) + + results['meth'] = 'ar' + results['beta'] = b0 + results['sige'] = sige + results['yhat'] = x.dot(b0) + results['nobs'] = n + results['nadj'] = n_adj + results['nvar'] = nlag * const + results['x'] = x + return results + + +def _compute_alpha(v_hat): + t = v_hat.shape[0] + p = v_hat.shape[1] + numerator = 0 + denominator = 0 + for i in range(p): + results = _ar2(v_hat[:, i], 1) + rho_hat = results['beta'][1] + sig_hat = np.sqrt(results['sige']) + numerator = numerator + 4 * rho_hat ** 2 * sig_hat ** 4 / (1 - rho_hat) ** 8 + denominator = denominator + sig_hat ** 4 / (1 - rho_hat) ** 4 + + alpha_hat = numerator / denominator + return alpha_hat + + +def _gamma_hat(v_hat, j): + t = v_hat.shape[0] + p = v_hat.shape[1] + gamma_hat = np.zeros((p, p)) + if j >= t: + raise Exception('j must be smaller than the row dimension!') + else: + for i in range(j, t): + gamma_hat = gamma_hat + v_hat[i, :].reshape(-1, 1).dot(v_hat[i - j, :].reshape(1, -1)) + gamma_hat = gamma_hat / t + return gamma_hat + + +def _kernel_type(x, type): + if type == 'G': + if x < 0.5: + wt = 1 - 6 * x ** 2 + 6 * x ** 3 + elif x < 1: + wt = 2 * (1 - x) ** 3 + else: + wt = 0 + return wt + elif type == 'QS': + term = 6 * np.pi * x / 5 + wt = 25 * (np.sin(term) / term - np.cos(term)) / (12 * np.pi ** 2 * x ** 2) + return wt + else: + raise Exception('wrong type') + + +def _compute_psi(v_hat): + t = v_hat.shape[0] + alpha_hat = _compute_alpha(v_hat) + ss_star = 2.6614 * (alpha_hat * t) ** 0.2 + psi_hat = _gamma_hat(v_hat, 0) + j = 1 + while j < ss_star: + gamma = _gamma_hat(v_hat, j) + psi_hat = psi_hat + _kernel_type(j / ss_star, "G") * (gamma + gamma.T) + j += 1 + psi_hat = (t / (t - 4)) * psi_hat + return psi_hat + + +def _compute_se(ret1, ret2): + t = len(ret1) + mu1_hat = np.mean(ret1) + mu2_hat = np.mean(ret2) + gamma1_hat = np.mean(ret1 ** 2) + gamma2_hat = np.mean(ret2 ** 2) + gradient = np.zeros((4, 1)) + gradient[0] = gamma1_hat / (gamma1_hat - mu1_hat ** 2) ** 1.5 + gradient[1] = -gamma2_hat / (gamma2_hat - mu2_hat ** 2) ** 1.5 + gradient[2] = -0.5 * mu1_hat / (gamma1_hat - mu1_hat ** 2) ** 1.5 + gradient[3] = 0.5 * mu2_hat / (gamma2_hat - mu2_hat ** 2) ** 1.5 + v_hat = _compute_vhat(ret1, ret2) + psi_hat = _compute_psi(v_hat) + se = np.sqrt(gradient.T.dot(psi_hat).dot(gradient) / t) + return se + + +def sharpe_hac(ret1, ret2): + """ + This method performs a sharpe test on two return sequences, + returns: + diff: the difference in sharpe (sharpe1 - sharpe2) + pval: P-value under the assumption that H0 holds, H0: there is no difference between these two sharpes + + + For mathematical principles, please refer to the paper: http://www.ledoit.net/jef_2008pdf.pdf + """ + mu1_hat = np.mean(ret1) + mu2_hat = np.mean(ret2) + sig1_hat = np.std(ret1) + sig2_hat = np.std(ret2) + sr1_hat = mu1_hat / sig1_hat + sr2_hat = mu2_hat / sig2_hat + diff = sr1_hat - sr2_hat + se = _compute_se(ret1, ret2)[0, 0] + func = lambda x: (1 / np.sqrt(np.pi * 2)) * np.exp(-0.5 * x ** 2) + pval, err = integrate.quad(func, -1000, -abs(diff) / se) + pval = 2 * pval + return diff, pval diff --git a/alphalens/tests/test_sharpe_test.py b/alphalens/tests/test_sharpe_test.py new file mode 100644 index 00000000..9c8f0048 --- /dev/null +++ b/alphalens/tests/test_sharpe_test.py @@ -0,0 +1,407 @@ +from unittest import TestCase +import random +from ..sharpe_test import sharpe_hac +import numpy as np + + +class SharpeTestTestCase(TestCase): + + def choose_random(self, total_data, size): + res = [] + for i in range(size): + res.append(total_data[random.randint(0, len(total_data) - 1)]) + return res + + def test_false_negative(self): + """ + This method checks the probability of making false-negative errors. If the method is effective, + the probability of making a mistake should be close to 1-a in the case of a certain confidence level a. + The two return series used for testing are from SPY’s daily return data for the last 5 years, + with a sample size of 400 + + """ + spy_daily_rets = [-0.013919958838530699, 0.005853027750837958, 0.0044375278402715157, -0.0039632195264346359, + 0.012759817845710009, 0.0086145490161160243, -0.0021462779457527281, -0.01659340355533423, + 0.0052233854734780039, -0.012982218667322964, -0.0016490802552390216, -0.0034625449137485331, + -0.00041580988944023289, -0.025511715900802368, 0.001327399991813305, 0.018710800172858866, + 0.002396211921440905, 0.014731785428342636, 0.018102301674970178, -0.0035760152340462703, + 0.0082443528405238276, 0.0088712076964614361, 0.00089570498536284404, 0.0010913456291608004, + -0.0067916712089829412, -0.0046977729482915453, 0.01525711575998856, 0.0040515953835709162, + 0.00088710506244882303, -0.0013294782068258293, -0.0060555552549123037, 0.016796428376980588, + 0.011255388075285921, -0.0026475057848001082, -0.0018847189114288287, 0.011521154030276204, + -0.00038387110344539899, -0.004881769641495004, 0.012026410526148945, 0.0028990503651236121, + -0.0028437944342881716, -0.0011438958271307031, -0.00056998828647925048, + -0.0092348906201764613, 0.0020648718300784186, -0.0038366473604604101, -0.013575211219917227, + -0.011375398889812649, 0.015108444454088765, -0.0011169362811900285, 0.016162859130062657, + -0.00086347147121856871, 0.0039838328054930816, -0.0014328978653496138, 0.0015768725361366975, + -0.00024140645499881597, 0.00095536051736444527, -0.0041009827674817201, + 0.0098681439042884733, -0.010579941390566172, -0.013907846681668889, 0.01954999732777507, + -0.0063427828566636757, -0.00633579168381182, -0.00783087435030283, 0.00238653267837452, + -0.01933507003758117, 0.0056013412735567147, 0.010344547595203757, 0.013994406523579528, + -0.014667807965845658, -0.018253772482062613, 0.0084991096691027224, 0.0090720019064745028, + 0.01233434777708009, -0.0021367181493793241, -0.0018968778559435018, 0.010918460236255978, + -0.007230197263794147, -0.0097122397560006624, -0.014365753586081942, 0.0020868204247548316, + -0.013010043601813348, -0.02393995505420432, -0.011027921639270244, 0.00093351016899934081, + 0.0081264750206146985, -0.024792129697530418, 0.016150024005229024, -0.021363799374967907, + 0.0010644609639478553, -0.011702430549502929, 0.0048445795387066504, 0.020408282673438194, + -0.014908342364348348, 0.013480870086554742, -0.010831921431237901, 0.0053707563766844935, + 0.02431939198632449, -0.00036083782031415268, -0.01827964873294563, 0.0056302424451337707, + 0.0018281535648996261, -0.018795620437956351, -0.013779756369722729, 0.0, + -0.00048322558974156493, -0.011980355047726765, 0.019554956169925752, 0.0164465983050055, + 0.016767818686444169, -0.0044569538674127385, 0.0, 0.014420533474409858, + -0.012522010250888838, 0.0044692293276848982, 0.011957259158751787, -0.0019944691192491648, + -0.0084080654731916171, 0.024241260952036825, 0.0041393374855316356, 0.00381488739220881, + 0.0031059297009994324, 0.0010957022306536945, -0.01116284135152068, 0.0053415567609240711, + 0.00055322761756082528, 0.016084000306570534, -0.0012284218012543757, -0.0016345157949249067, + 0.0060894890124436607, 0.0061439312567133086, 0.0041741395507730594, 0.0010737484451910362, + -0.00033983263242853834, -0.0065493137296562809, -0.00059348450256924146, + 0.00059383693558756612, 0.0092979238735824143, 0.0045346188483339489, -0.0024785630662461644, + 0.0063756899067435313, -0.0029522698857964436, -0.010031612223393216, 0.010867714054583422, + -0.011961798060419637, 0.0028401521852654543, -0.0027364651246819527, 0.0097078067389868572, + 0.010052452165102865, 0.0, -0.00091426302563601958, 0.0069286117676587988, + 0.0029133625188901835, 0.00095276560930401821, -0.0053283395238636899, 0.00019243171274618831, + -0.0018199497693863753, 0.0017711746534487816, 0.0018200444090834544, -0.0089331596185888706, + -0.0055412398131271523, 0.0079473756280481478, -0.0087991096387871615, -0.0056036141466218048, + -6.8915795500390509e-05, 0.0035573604491498934, 0.00082939765655520858, 0.012288137829834733, + -0.0088800129315513843, 5.2610534733599224e-05, -0.0088118009742959025, 0.0099144954381644723, + -0.0091392113685693221, 0.00029171528588101125, -0.0035260743922160831, 0.0060341616559356215, + -0.0015074260567849995, 0.013163540436171406, 0.0066348084323237799, 0.00047784016246565919, + 0.0042050616482802727, -0.001473363145242601, 0.0018120631633444795, 0.002945736434108559, + -0.0028907095377956882, 0.0050385253399067853, 0.001470557989346144, 0.003255121425270735, + -0.0015966919817402836, -0.0091546552395792569, -0.0077079863015115935, + -0.0017777453392835429, -0.0015824520172346146, 0.0031751511727657888, -0.0035770339818228525, + 0.006195936010214842, 0.0024547917847688172, -0.0015875568744843083, 0.01336499638854094, + -0.036048509089044334, -0.017863127377184318, 0.017987618279016626, 0.017127077702343119, + 0.013694832267075352, 0.001573909229300563, -0.0066721623988872381, 0.0063331517933555848, + -0.001097847600197932, 0.014937849259299529, 0.0035282514311278934, 0.0072191375537002944, + -5.0297509770369508e-05, 0.005261384156493687, -0.0011108164500908124, 0.0027300369181137984, + -0.0011989449284628417, 0.0043463908450704913, -0.0038694255621125606, 0.0045343651888736236, + -0.0026227386965934629, 0.0002744401421101994, -0.00087796750522539657, 0.0011583377852781851, + 0.0016108199223017206, -0.0011053464912718081, -0.0061309939188516349, 0.0030192085861879825, + 0.00092003680147212386, 0.0081328430980738275, -0.0006392341083426567, 0.00059501673484563256, + -0.0023836071260436853, 0.0046891920085041505, -0.001008612761917993, 0.0031130291902163876, + -0.0052002387965444097, 0.0017854574490774322, 0.0023367609127229461, -0.0014175569615877404, + -0.00031655900363047884, 0.0019642768789271958, -0.0048837817951972173, + -0.0011959229448487152, -0.0016097219255055695, 0.0051106477634073055, -0.0017873056738290094, + -0.0027031182577038626, 0.0001342789221878693, 0.0044654178746001971, 0.0031584314773835942, + 0.0, -0.0024181171256977274, -0.024071591465616549, 0.014299400347727254, + -0.014327692876632825, -0.00042081780617042597, 0.010083590326245728, -0.0040524254293461404, + 0.00023697518819365548, 0.00018651073696940301, 0.011057520272960364, 0.006395461864005636, + -0.0054781221829276472, -0.0081478972846984465, 0.0062063147746445058, 0.0052398347206419338, + -0.0091839672752905832, 0.0077810289195743287, -0.003097329733868226, -0.004498349275376734, + 0.0043333199743507134, 0.00083299664307334709, -0.0035684382600374986, 0.0048866614648981344, + -0.012309055343463826, 0.0012648914511479425, -0.0032312129127725076, 0.0002827640182787583, + -0.0032357231917051754, 0.0060265370201559865, 0.002617669267555911, -0.0016317718531906333, + 0.0002313359651990865, 0.0043038649310427335, -0.0031690087961271374, -0.0020038771758892349, + -0.0026671363510555324, -0.003098115397229928, -9.1106488300463084e-05, + -0.0070158387875658423, -0.0062089953967792155, -0.0040985078148644272, + -0.0015348956992016216, 0.021965210573014593, 0.0045530260966128999, 0.010838542399453255, + 0.0022170413930575616, -0.0018451008109516831, 0.00037268377036725475, 0.0078035744444113764, + -0.0013751361581948451, 0.004634519520260616, -0.0020093441873947349, 0.007320074825243772, + 0.0019107937857663071, 0.00090723741330034358, 0.0036207871191593632, -0.00451086930132516, + 0.0016291252475393314, -0.0025322373290740385, -0.0036322449617247621, 0.00063698012622004363, + 0.0057830357754948825, 0.0033933952940374823, 0.012989092461765273, 0.0024955334262573015, + 0.0060393513430867518, -0.0011920705933756715, 0.006633160094528634, -0.0079498540401129469, + 0.0040282069717501034, -0.0018684940601807476, 0.0019147531013774532, 0.0038553828352476671, + -0.0025178683622518738, -0.0017712191102228214, 0.0012857007306195101, 0.0024401684900807741, + -0.008044770898915643, -0.00026683947699468469, -0.0036128269656066081, 0.0072470700789284415, + 0.0059933987129865418, -0.00070340419304437685, 0.0038407392360095827, -0.0032095476984908, + -0.00022189803077299342, 0.0027389238862496335, -0.0022039916738093579, 0.0019870300272804009, + -0.0032172213193903954, 0.0019894901990435976, -0.0034853065324735866, 0.0032750575028159812, + -0.002250148594718504, 0.006236141251672489, 0.0083024789971244406, -0.00080616976164404885, + -0.0013291546576377211, -0.0059401224449767698, -0.00030535785591800924, + 0.00030545112781954487, 0.00061541352500404933, 0.0070705553155927525, -0.0017855311372388938, + 1.8681200640635609e-05, 0.0012469468483067292, 0.0057558654788003594, 0.0038585686194485547, + 0.0054006356950142553, 0.0042550258472142666, 0.0049187603808722447, -0.00050995783741447642, + 0.0013620999116228383, 0.005914117909313843, -0.00067386359011001318, 0.00063358737169871837, + 0.0014382372096388707, 0.0014768180216961913, -0.0026561562848085307, 0.01372309640079572, + -0.0059055732732664623, 0.00063265296965497875, -0.0030222588918982218, + -0.0029864440626432565, -0.0018946741611556295, 0.00097173384436000099, 0.0034180856011452043, + 0.00050399143214563402, -0.0034452050481698082, 0.0082681927319336879, -0.0015890494351040729, + -0.0017126281669050947, -0.001392213484710414, -0.012502417283917322, 0.0020084070736006154, + -0.0010635493459626444, -0.00047319186109995304, -0.0013656227239621233, + 0.0071929984501777344, 0.0014029815621068309, 0.0028833812390405278, -0.0023253089145854888, + -0.0014860653146030467, 0.00055188386915827614, -0.0031015041843178714, 0.0026440269028602525, + -0.00097702189252757421, 0.00067915097073312225, -0.001189969866162377, -0.003999981880036807, + -0.0066221551112485288, 0.0086442136878925879, -0.002982296867907408, -0.0016253647965106532, + 0.0077524328958529232, -0.0029323124541825729, 0.010697201572130055, 0.0060306697501066875, + -0.00041957167980444243, 0.00071446433036848056, -0.0020972405668797656, + 0.0022670971319209698, 0.0005487617169550596, -0.0010924624549638606, 0.00092402876541708423, + 0.0041030032199655952, -0.0002087534311069339, -0.00083074558305451518, 0.0015872874724336405, + -0.0017934114325539596, -0.0016320903653304075, 0.0050869050058353071, + -0.00060716185073572682, -0.017698212440632632, 0.0034761096464299701, 0.0068876472228969199, + 0.0051605581445224313, 0.0020447352512358297, 0.0023776993097575083, 0.0046511833743427822, + 0.00012774486267441709, -0.00091171755254493281, -0.00041439447709146204, + 0.0079120413860627181, 0.0032861199718208223, -0.0006760057220613902, -0.002928430213020472, + 0.0016851817807777003, 0.00045008236944332758, -0.0012710198733347777, + -0.00049418566511705286, 0.0048917942121060598, -0.0011059634944963204, + -0.0019223224794037153, 5.24088413715873e-05, 0.0083282019032147048, -0.0065399674300961053, + -0.00037056736042684335, -0.0004928192312891877, 0.0013177415132210601, + 0.00049241334832350603, -0.0078921579302685396, 0.0087407752114951887, -0.0084996561839024709, + 0.0015714091326084478, 0.0023577980445177449, 0.0017751194707869011, -0.0090213553418905246, + 0.0063200267775349595, 0.0010328634388225844, -0.00074324412946447094, 0.0074729717313404276, + 0.0017631858945128531, 0.0046213004668986812, -0.00016397899342779354, 0.00077686999080683883, + 0.0052484269813135587, 0.00040755740124231998, -0.00081049440158498243, + -0.00028325937116424615, 0.0023482845073323766, 0.00024412912289117905, + -0.0009291770146441225, -0.001294343035191603, -0.00064801026516914106, 0.0023103018851720059, + 0.00052268988209491773, -0.0018969725517065106, 0.0017418388841934362, 0.0018159002625346421, + -0.0022187357053997658, -0.00032133951447732567, -0.013873471539453841, 0.0013125526976869306, + 0.009870305745090846, 0.0, 0.001663357144699118, -0.015430231410561768, + -0.0017476345918336911, 0.00092555402266736309, 0.010210935864330972, -0.0035448284795233098, + -0.0020452024403770919, 0.0020884712911182213, 0.00020797857820653576, 0.0010613365909868389, + 0.0048596626364210138, 0.0056113242811433039, 0.0018585768611463216, -0.007223889273593076, + 0.0033325296224855361, -8.1534566364838135e-05, -0.0013776173657037427, 0.010709527697795318, + 0.0035334336811492228, 0.00043641663806659459, -0.00031764048179716564, 0.0015039760041686279, + 0.0018062911385980929, 0.0010809760876266949, 0.00051881440363765741, -0.0027571437003061083, + 8.0321964252449973e-05, -0.0019656165061061603, 0.00048284216143867908, 0.0039328411284587084, + 0.0012819161272639157, 0.0034365274227305953, 0.0042557656390993515, 0.0022609589642217376, + 0.0012676173797014911, 0.0058428145458180314, -0.00098125675390325906, -0.0017696547722657607, + 0.0025616221669579886, 0.0015694881563690366, -0.0015298169594681887, 0.0012174467574090908, + 0.001493074368338565, 0.00062772565085222887, 0.00093687060454983317, 0.00047005850991443943, + 0.0049621450974088877, -0.0036909297452027978, 0.0015970955911106532, -0.0048699497388309254, + 0.0010943901149729207, 0.0082587692701179272, -0.0034531997332362119, 0.0012070600697138278, + 0.0015541640524725508, 0.00038896167703894591, 0.0032209875785293995, 0.0014686564241479516, + -0.00053771763306476039, 0.0016996127980435372, -0.0035521449177472464, + -0.0004655075257050667, 0.0010049840673258537, -0.0023181213422249769, -0.0050806273470288543, + 0.0084986185119400037, -0.0028212309862484242, 0.0015904101132093729, 0.0064617519797534051, + -0.00073003654238457116, 0.0022363555927154, -0.00034827119797187223, 0.0099534936479128699, + -0.00045727121397487469, 0.0081824785004274148, -0.0016598336185965623, + -0.0011363047369953305, -0.0035564886958743269, 0.0, 0.0032286880550560504, + 0.0054543349185243173, 0.0031611260916011297, 0.0017299736743137117, -0.00022525904790504647, + -0.0039765203470562005, 0.0083697450978057475, 0.0061514121091905682, -0.0039898142389429569, + -0.00026312585663179622, 0.0020230668908409744, -0.00029794691056506117, + -0.0012352795849460696, 0.00067533609749981238, 0.0019422427999684633, -0.0036184919034284446, + 0.0071178433445611144, 0.0062519024984193372, 0.0042855369913592511, 0.0064800404712896942, + 0.0019760040210721197, 0.0021559151725880632, -0.0015322596052808057, 0.0072597434405905492, + 0.0066641084190408595, -0.0034534166179160941, 0.0094947735191637239, -0.0015006734272006161, + 0.0042983764611286279, 0.0081334575425284772, 0.002122724658118802, -0.0003184748756650535, + 0.00042229886164513175, 0.01157869401810685, -0.0065594892969048635, -0.01069638909358861, + 0.00090503804511787855, -0.0011349259507330567, -0.021558291578966937, -0.041123480856507633, + 0.018963894745619214, -0.0057204537395322275, -0.037099018243812565, 0.015405655116834671, + 0.013716860274016529, 0.0027877071997974845, 0.013836696227538692, 0.012271012407140791, + 0.00029201006666301765, -0.0060420755854823094, -0.0053406912755825653, 0.0012588097254704333, + 0.016130534321524337, 0.011647133850389624, -0.012664343926588217, -0.011045182404171849, + -0.01330250934577637, 0.0052814694082246039, 0.011388260971237241, 0.002535625929665386, + -0.00036515990159913247, 0.0047295868340608838, 0.017256357756558538, -0.0011850837459180807, + -0.0064635446289674769, -0.0051294105158602443, -0.00098315677158766768, + 0.0010832996391545446, -0.013458006599452954, 0.0015912619634319558, -0.0019203627866085649, + -0.024885251982011081, -0.021506654463185448, 0.027599723030948375, -0.017393360260072988, + -0.0026868676336716524, 0.014053584568802346, -0.022546850340621649, 0.012580121703853919, + 0.0116986710790421, 0.0068588355027898729, -0.021985974600102987, 0.0037319585141335487, + 0.016683293868769322, -0.005166400662055981, 0.0081721094579392251, -0.0028407863045024628, + 0.0081841250827474621, 0.010587780083717302, 0.00081215918319976765, -0.0055838936548419094, + -0.0083665261235353894, -0.0001136443793057218, -0.013317499696260615, 0.0020535838414330598, + 0.010393543476882261, 0.0010161086569318023, -0.007991283661175741, 0.0020425578988123583, + -0.0067184216440420164, -0.0022784378014265583, 0.012910130374413686, 0.0034210795057305621, + -7.4373015904072304e-05, 0.0096848698375415765, 0.0094058714970302049, 0.0023161129249085199, + 0.00095419481669112294, -0.0068146491987043367, 0.0043172899754067018, + -0.00073692533257574411, -0.0024620702899943447, 0.0074429457585221126, + -0.0029238425023504488, 0.0028979174093537452, -0.0018652081350594152, -0.0023473549177659425, + -0.011388236377872896, 0.012967600411546432, -0.0067495831816176688, 0.010673602321489284, + 0.0046046351800235197, 0.0010907736511653887, 0.0084319464850437509, -0.00068517905008591562, + 0.0031004889957126913, 0.0013670591328189818, 0.0014702076668329234, -0.0037974541328210654, + 0.0029134571646609597, -0.0010233074195409309, -0.002236322225515841, -0.003835841045755739, + 0.0016723733733998714, -0.0063052130129345185, 0.0017143095542415665, -0.013471404501826822, + 0.0020302659302955384, -0.0084684739894135275, 0.0062385321100917324, 0.0009597297401051641, + 0.0026923371941398599, -0.0040467866677885711, 0.008345366842816837, 0.0083486633378657693, + 0.0091520647858915893, 0.0033835881005495061, -0.0069234097793162697, 0.0086695214484260319, + 0.0010017577310372783, -0.00085938459134604628, 0.0041889144567814895, 0.0019615040917200233, + -0.0037006883280290559, -0.0009991828244558798, 0.0018218932073126304, 0.0046726370522676142, + 0.0091281861839676015, -0.0029541935271314435, -0.0064252250664551536, -0.0056131611816090388, + 0.0047529682825768749, -0.0010688897601102143, 0.0050539644926597127, 0.0046417388100938872, + 0.0036302299145611894, 0.0029850200949945727, -0.00038248997329859424, -0.0012973193592116239, + -0.0068051785036634671, -0.0037436946849820352, 0.0064165916835070558, -0.0073979890953860261, + 0.0083021709678730016, 0.0033096338168425721, 0.0023499704429184032, 0.0019258072736538789, + -0.00024344249488594816, -0.0015046228993428334, 0.0060894379372347096, 0.0077240051951621425, + 0.00051697026702957061, 0.0055186766658295028, -0.0042536791469803825, 0.0, + -0.001275821041012648, -0.0027917324529928012, -0.0029758691912976643, -0.0022232247586393417, + 0.0019134844588166722, 0.0035416696752206267, 0.00013670589166414615, 0.0057408626401493912, + 0.00027539046433688874, -0.0053632723112128078, 0.0053921921058308619, 0.0012049485125857906, + 0.0078959491745145272, -0.00070864690959082655, -0.0032549959578481724, + -0.0013055387570702903, -0.0029243827985024895, 0.0027614835561333706, + -0.00010331498601678923, 0.0036484647231940226, -0.00034435017217504527, + 0.00072089860188273569, -0.007917074763303944, -0.0054584801387871362, + -0.00034887192084553931, -0.001284440638694484, -0.032044238701658845, -0.021385239495329111, + 0.013736765242789106, -0.0055785651045182627, 0.021277478694226692, 0.00035462433831168205, + -0.014050700688687412, -0.00086891385767795537, -0.0041984046062494285, + -0.0055976329578992789, -0.03038298613345658, 0.018552716340992204, -0.017954133398751293, + -0.00554247998657309, 0.015161922106263104, 0.010790810606001955, 0.010419336207193375, + -0.0062878320418230382, 0.0059961904761904883, 0.0061081363557184964, 0.021156473594869052, + -0.0017470918660710266, -0.0092344388075351613, -0.019185117000145469, -0.0016490301498947035, + -0.0070599160421842555, 0.010736078744020494, 0.0023435989032867255, -0.016661937460983989, + -0.018356283497043324, 0.0034098926079799696, -0.0065075582985039127, 0.015585191708866741, + 0.0032906452761820582, 0.022955108466649721, -0.0019652442221442801, 0.0065309577982206601, + 0.012620440532604249, -0.031300639658848772, -0.0020671122441947576, -0.023268659808050929, + 0.0016337559105834565, 3.528802873220549e-05, 0.0053792741116551746, -0.00052646972799064162, + -0.01838932459323428, -0.020001828466036264, -0.00054756738120831372, -0.015445801712592799, + -0.015762312245470089, -0.020768071027724266, -0.025921538270728517, 0.048981383912890752, + 0.0090492055785296177, -0.0015306249429645202, 0.0093806214142388988, 0.00058032572324639986, + -0.024495286045708164, 0.034079265284436833, 0.0072909658083062379, 0.0094363682733997756, + 0.0046359793706938213, 0.004187457586523502, -0.00019081010816546229, -0.0056021406618398384, + 0.011343371904263044, 0.0021902514044889188, 0.0077793075153949065, 0.013078112461589519, + -0.013098614406157449, 0.0014447019763836888, 0.0011415837519792582, 0.008380291865337508, + -0.0076716636395672833, -0.0013268706925485541, 0.015791201319254933, 0.0085941918035878917, + 0.00073995811989591154, 0.0070739525328638031, 0.0043750260192940704, -0.0016127634400093793, + -0.0093864995942706253, 0.0011125165734489073, 0.00062794945958288828, 0.012783113055053752, + 0.0031394816099983291, -0.0022199594192916505, 0.011008182974460601, 0.0016588548335021436, + 0.0021229298655105477, -0.0035196296994635112, 0.0060883534732554256, 0.0015045801188910968, + -0.0008211208483686061, -0.00039431450082372255, -0.0024700460829493176, + 0.0069628206075837973, -0.0038133478184273484, -0.0012894906511928816, -0.0062713271234897672, + -0.0080037122969838226, -0.0020357833836665007, 0.014497046967282268, 0.0033747195434334909, + 0.0068004155400178679, -0.0005342114892060712, 0.0049752154462316955, 0.0036974569510370703, + 0.00031938677738718724, -0.0033633751306163528, 0.011154432831310901, -0.019042098555191123, + -0.00074872276704451846, 0.007129214721222521, -0.004660814511982192, 0.0037886143710863518, + 0.006519271272499072, 0.011605009012014778, 0.00031547663859643471, 0.0018886790356626992, + 0.0024431423890569626, 0.0045603605468151898, 0.00096973916502962609, -0.005227259822847885, + 0.0034460739371930948, -0.00020975167534720285, 0.0067667996557929655, + -0.00065694436105112608, 0.00065737622065231704, -0.0024123279494505656, + 0.0018658433824518816, 0.0010000954154634201, 0.0087482701160788157, -0.0020823563174140469, + -0.00054710158904958117, 0.0046564181021324647, 0.0012608710837902493, 0.00054417782056792596, + -0.0072447851841004329, -0.0019561087132878852, 0.0094443193334083908, -0.0039145833042847356, + -0.016489765636756193, -0.0016652374564566363, -0.0028192306458568739, 0.0044641899764816806, + -0.024545334856297063, 0.0084010316160403331, 0.006138853116091969, 0.0090172687167651144, + -0.0060148018286709792, -0.0067502410800385215, 0.0088750135344859427, -0.0030372519488138794, + -0.011730299985646675, 0.0018771944475710622, -0.0091944275318197599, -0.0066351855035462481, + 0.0025480802866222341, -0.013185489350605595, -0.0028695953163438981, 0.021634292369022257, + 0.0084836173384386093, 0.0065030105859593679, 0.010150420594555509, 0.0045538606262092163, + -0.00017380878904926789, -0.0018341788767871625, 0.0044748056711676476, -0.001588750654607729, + 0.00086474839011474458, 0.009847489616050531, 0.0026333506318640953, 0.0094845404089682894, + -0.0011155339839460376, -0.0014948201533664385, -0.0097048260147692966, + -0.0011013524889161852, 0.0036834158502756509, 0.0052826941040235997, 0.0086306199081953583, + 0.0028775588364173821, 0.007758125389196513, -0.0013382584887238202, -0.0050286128412517783, + 0.0013468244833449994, 0.0045428499864470506, 0.0023089718585516117, 0.0046107133485775442, + 0.00025101763907731112, -0.0034082353818919442, -0.0065403291943009201, 0.003627370721397849, + -0.0057882773731594561, 0.0026912910645016108, 0.00684711086157197, 0.0047365808210981797, + -0.0046804089520013914, 0.0067357125370617332, -0.0016887042572234501, -0.0021888796792800536, + -0.011436301866832976, -0.0084063587879203983, -0.007190935063677184, -0.029783824272301418, + 0.013131719368298489, 0.00086835399070683827, 0.019200532600535558, -0.0066780167471596208, + -0.011969722408775363, 0.015031897644318226, -0.029102374508217288, 0.0027122370387400174, + 0.014681699465826981, 0.012290755275915277, -0.0080710982134760689, 0.0081719341181765159, + -0.00041156282262333477, -0.025656503625337668, 0.011094359352673999, -0.003924360431962981, + 0.0069373575459321657, 0.01291395639553361, -0.00034165507479810842, -0.0057787341189434738, + 0.011007282064233515, 0.013094073318483668, 0.00083905258976146868, 0.00027032483463984569, + 0.00016762452107288439, 0.0070082669503264139, 0.0032640667350503083, -0.0007312639016314737, + -0.0029576877860707507, 0.0025994767066499502, 0.00053210418396565373, + -0.00013210845087596734, -0.0051596860146423396, 0.00036778477779675356, + -0.0081461611258245581, 0.0060508297153054613, -0.0021185284244424896, -0.0054220924627444989, + 0.004977296241935214, -0.01219349520640034, -0.017634797301976257, 0.0080892175488436191, + 0.013775934770599463, -0.0042801073303305737, -0.015521033580990995, 0.0092497466786760629, + 0.0069373511868449089, 0.01026509210886184, -0.00087728917644469551, 0.0097958511689166805, + -0.0016371837721802507, 0.0027796111946545299, -0.0042104477460024325, 0.0069471417571260741, + -0.0034005434102205578, 0.0027432979330199725, 0.0018994799284901553, 0.0040621145300012707, + 0.0057016495740589423, -0.00052543331514953717, 0.0030973332797579189, -0.0028908198111288197, + 0.00962829298667911, 0.0037867358138332641, -0.001073595819252704, 0.00042328602230190526, + 0.0035170763311340725, 0.0024012806830309152, -0.0018434724317077933, 0.0017810347779141367, + 0.00042064167573130362, 0.001488051244148636, 0.0074324811891970377, 0.0005111608887036212, + -0.00031890556815639126, -0.0035286113742376557, -0.0016072181079906933, + 0.0022871015976992926, 0.0075573082271827019, 0.001947246153297888, 0.0047147541407699389, + -0.0036787898294173837, -0.0085283162713168936, -0.0068357243163459369, 0.0064628517251219542, + 0.0018318779356496506, 0.008908381622314554, -0.0030184882404727942, -0.0010189129654248896, + 0.0027425381591643649, 0.0086830166136557807, 0.00044177812493995106, 0.0070269301658816286, + 0.00012710194847276313, 6.3542897810187071e-05, 0.0041903878412037354, 0.0055744146390075411, + 0.00031146865335429652, 0.00012580673569262402, 0.0051354139149903943, + -0.00015330657247170087, -0.0054541699523111831, 0.0024604425650269768, 0.0091647808769943495, + -0.0073865350463874213, 0.0038131822682323513, -0.002781133241565148, 0.0051145291440517493, + 0.0067794815580664025, -0.0028147841122450057, 0.0066752899501525409, -0.0012941593356649017, + 0.0022276043996729555, 0.0080440343926337921, 0.0035974958008855307, -0.0021665834925812533, + 0.00030190658583295615, 0.0010548298096124675, -0.0092550576956459407, -0.015393978888608717, + 0.010133806202663687, -0.00082519470886388557, 0.0034303248137137476, -0.018276706062231307, + 0.0072565021210588831, 0.015474587261210093, 0.011333914559721103, 0.0033663186012626856, + -0.0052427930503495812, 0.0074813877670187878, 0.0011199120974654253, 0.0070134812013979975, + -0.00097612681264658274, 0.0015405503421281885, -0.0026364536854990295, 0.0047527604416706115, + -0.0039598165225284143, -0.010238713429954083, -0.032651466271662377, -0.030158700335688127, + -0.0037066574538363595, -0.045581968142854867, -0.012302207130730047, 0.050292735417375667, + -0.027701583602285917, 0.042852381641715231, -0.033611817724003346, -0.016632148583645257, + -0.077301184519381283, 0.051125632765761653, -0.048845582672532251, -0.095788869004219901, + 0.092106388437526832, -0.11622851176492532, 0.061603115598668268, -0.051851940175118516, + 0.00062459390916136392, -0.045064179904819368, -0.02812934585671234, 0.09473446633714766, + 0.013054485778609681, 0.059327276871479429, -0.030680386069529497, 0.031572187934786555, + -0.013963818570264852, -0.045045009937259017, 0.023240060232359916, -0.014412991632967276, + 0.071229140702134819, -0.0029727841048596826, 0.033911982663251905, 0.01543061718804406, + -0.0099527263541555611, 0.029360092144398386, -0.020799767711140227, 0.0068382435369376271, + 0.025317501364825024, -0.017924637356247386, -0.030436302415500327, 0.022529366536312212, + -0.00032380273937104853, 0.014190752797302197, 0.014378992193044615, -0.0046667506261981462, + 0.025646883501219797, -0.0092798519610025609, -0.028290563701328653, 0.0053714830846338657, + 0.0088833970053499023, -0.0066749489376936033, 0.010835771116858073, 0.017363890238404123, + 0.00064930826339226755, -0.020304324539599183, -0.017234732998310909, 0.011999172716769646, + 0.0042424092938362357, 0.030497710566481206, -0.01010224823542627, 0.016953874729130014, + -0.0073429187002593821, 0.0021704464918497202, 0.012695282617934511, 0.01440614518950234, + -0.0017143802746979464, 0.0039617812433694333, 0.0046693326552784686, 0.008345357792013619, + 0.013240758849990319, -0.0028824933567535016, 0.025601233763300479, 0.012432325304123415, + -0.0075473925918101425, -0.0056104494621230794, -0.057756004181202414, 0.012256572704064661, + 0.0090712229645868447, 0.019219193677130653, -0.0043458863329258746, 0.00054480101868126241, + -0.0057801233350732728, 0.0067405535031435093, 0.0044099658790959229, -0.025318078389898302, + 0.010390293624437108, -0.02365843340167284, 0.014732351176588221, 0.013631585862567297, + 0.0063838750445575076, 0.0052163833075733468, 0.015407777564225844, -0.010378876305246254, + 0.0077462543831685604, -0.0054091671147944442, 0.01024107881178038, -0.0089409394282835564, + 0.012992376111816917, 0.0093449151745117476, -0.0032621865970733355, 0.0026650458200860871, + 0.0082847594621899834, 0.001942406117037887, 0.0057236052558695683, -0.011932809105651199, + -0.0064410243706065629, 0.0072931276297334424, -0.006188310281877496, 0.012173479871727011, + -0.0034450938172869705, 0.0078708562256928349, 0.006890637919946041, 0.0037715189488412015, + 0.006272347130476863, 0.0066849348068296344, 0.00071789656307030825, 0.0029890898221491824, + -0.0081060944717628036, 0.013820869512964684, -0.0017188750259312657, 5.9373608430890457e-05, + 0.0029685041707483428, 0.0021605943114215709, -0.0041051388068518158, 0.0031137866611310461, + 0.0035180039023237075, 0.010222418618353268, 0.0034410358101015781, 0.010055216506829279, + 0.0027477270111635477, 0.0058390600117643743, -0.0022250748823278155, 0.0080338508162507427, + 0.014606614101764226, -0.034020071003270647, -0.0088262530385461169] + + total_test_count = 5000 + reject_count = 0 + alpha = 0.05 + for i in range(total_test_count): + ret1 = np.array(self.choose_random(spy_daily_rets, 400)) + ret2 = np.array(self.choose_random(spy_daily_rets, 400)) + diff, pval = sharpe_hac(ret1, ret2) + if pval <= alpha: + reject_count += 1 + + percent = reject_count / total_test_count + print("the reject percent:" + str(percent)) + assert abs(percent - alpha) < 0.02 + + def test_definite_value(self): + """ + This sample value and result is taken from the paper: http://www.ledoit.net/jef_2008pdf.pdf + """ + ret1 = np.array( + [3.92670000, -2.3305000, -4.8583000, 1.96000000, -0.4397300, -2.7056000, 2.92570000, 3.87810000, -2.5249000, + 1.74590000, -4.0699000, 0.84013000, -0.5617100, 2.84340000, 3.11080000, 1.91650000, 1.24070000, 2.70060000, + 4.01650000, 1.19310000, 2.44220000, -1.6150000, 3.69480000, 2.07020000, 2.02090000, 0.83481000, 1.44590000, + 1.20520000, 1.51500000, 0.06133600, -4.8483000, 2.44980000, 4.55040000, 1.21710000, 5.40700000, -2.5487000, + 3.72540000, 0.54106000, -5.4384000, 5.18970000, 4.95600000, 4.63930000, 7.65870000, -5.7242000, 4.80640000, + -3.2255000, 3.90520000, 1.86220000, 0.10583000, 6.23280000, 4.70720000, 0.34273000, -1.0273000, 4.33750000, + 0.03111600, -16.625000, 3.97890000, 6.78550000, 5.69550000, 7.71210000, 3.20710000, -2.4622000, 3.89690000, + 1.76310000, -3.5363000, 5.06140000, -3.5352000, -1.9028000, -2.5466000, 3.85620000, 3.63410000, 9.75480000, + -4.8312000, 2.90380000, 3.82400000, -6.4514000, -5.0871000, 5.19070000, -2.5309000, 6.15040000, -5.8471000, + -0.8204800, -11.711000, 2.15650000, 2.76750000, -10.242000, -8.8416000, 12.0670000, 0.23470000, -0.6506600, + -5.3993000, -5.2209000, -10.378000, 2.62880000, 5.75680000, 2.07200000, -1.6054000, -2.5013000, 2.24750000, + -6.0060000, -0.4965000, -6.5602000, -8.6304000, -0.0425940, -10.261000, 8.24590000, 4.83680000, -5.7832000, + -3.3307000, -0.8245600, 1.73950000, 7.23180000, 3.48930000, 0.94715000, 2.39770000, 1.60400000, -1.8021000, + 5.55900000, 0.58602000, 5.52010000]) + ret2 = np.array([2.64760000000000, -0.816280000000000, -5.72400000000000, 0.0474760000000000, -4.24160000000000, + -7.59120000000000, 3.33910000000000, 6.75050000000000, -0.843120000000000, 4.28130000000000, + -4.41420000000000, 2.29520000000000, -3.21210000000000, 4.18450000000000, 3.66220000000000, + 2.78660000000000, 3.57730000000000, 10.0630000000000, 10.7530000000000, 0.906150000000000, + 1.90050000000000, -1.66430000000000, -1.68610000000000, -5.91620000000000, 0.996660000000000, + 5.41830000000000, -0.403410000000000, 5.81890000000000, 3.37120000000000, -4.26580000000000, + -10.7530000000000, 3.69750000000000, 7.75710000000000, -3.00710000000000, 4.93610000000000, + -3.78720000000000, 6.32270000000000, -5.64270000000000, -7.06980000000000, 1.74900000000000, + 8.63520000000000, 2.33160000000000, 9.29360000000000, -1.61820000000000, 5.63140000000000, + -8.20500000000000, 0.500030000000000, 1.09180000000000, -1.07180000000000, 8.75430000000000, + 5.11080000000000, 1.20070000000000, -4.22400000000000, 9.33900000000000, 0.463230000000000, + -21.9250000000000, 11.4950000000000, 2.99960000000000, 6.78450000000000, 12.2250000000000, + 10.2200000000000, -5.98690000000000, 12.7810000000000, 6.38120000000000, -4.78080000000000, + 8.73820000000000, -1.53230000000000, 5.41220000000000, -2.88540000000000, 10.4790000000000, + 10.8260000000000, 16.9360000000000, -0.699960000000000, 15.9100000000000, -2.98170000000000, + -17.0610000000000, -12.1860000000000, 16.9480000000000, -7.93020000000000, 14.7800000000000, + -10.2540000000000, -12.1910000000000, -26.5170000000000, 6.52520000000000, 1.67180000000000, + -22.6160000000000, -23.9400000000000, 19.0280000000000, -3.57650000000000, -11.1930000000000, + -13.3670000000000, -15.1100000000000, -23.9460000000000, 15.6860000000000, 10.0070000000000, + 0.0186130000000000, -7.06580000000000, -10.8050000000000, 4.75810000000000, -10.2730000000000, + -7.13690000000000, -16.3760000000000, -7.77480000000000, -3.71390000000000, -14.6970000000000, + 13.9630000000000, 13.4860000000000, -9.00860000000000, -0.636020000000000, -1.54890000000000, + 1.80770000000000, 4.97740000000000, 7.06600000000000, 1.26390000000000, 2.70540000000000, + 3.73190000000000, -1.55510000000000, 5.91610000000000, 2.27750000000000, 1.82370000000000]) + expected_p_val = 0.0646 + diff, pval = sharpe_hac(ret1, ret2) + assert abs(pval - expected_p_val) < 0.001