
import numpy as np
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt
import timeit
# import seaborn as sns
from scipy import stats
from pandas.io.pytables import HDFStore
from datetime import datetime
from Data import Data
from Portfolio import Portfolio
from Selector import Selector
from Simulator import Simulator
import scipy



# "nct",
# "johnsonsu",
# "genlogistic",
# "t",
# "hypsecant",
# "logistic",
# "dweibull",
# "dgamma",
# "laplace",
# "powerlognorm",


cdfs = [

"johnsonsu",
"nct",
"norm",
"dgamma",
"dweibull",
"laplace",
"hypsecant",
"genlogistic",
"lognorm",
"powernorm",
    
#     "norm",            #Normal (Gaussian)
#     "alpha",           #Alpha
#     "anglit",          #Anglit
#     "arcsine",         #Arcsine
#     "beta",            #Beta
#     "betaprime",       #Beta Prime
#     "bradford",        #Bradford
#     "burr",            #Burr
#     "cauchy",          #Cauchy
#     "chi",             #Chi
#     "chi2",            #Chi-squared
#     "cosine",          #Cosine
#     "dgamma",          #Double Gamma
#     "dweibull",        #Double Weibull
#     "erlang",          #Erlang
#     "expon",           #Exponential
#     "exponweib",       #Exponentiated Weibull
#     "exponpow",        #Exponential Power
#     "fatiguelife",     #Fatigue Life (Birnbaum-Sanders)
#     "foldcauchy",      #Folded Cauchy
#     "f",               #F (Snecdor F)
#     "fisk",            #Fisk
#     "foldnorm",        #Folded Normal
#     "frechet_r",       #Frechet Right Sided, Extreme Value Type II
#     "frechet_l",       #Frechet Left Sided, Weibull_max
#     "gamma",           #Gamma
#     "gausshyper",      #Gauss Hypergeometric
#     "genexpon",        #Generalized Exponential
#     "genextreme",      #Generalized Extreme Value
#     "gengamma",        #Generalized gamma
#     "genlogistic",     #Generalized Logistic
#     "genpareto",       #Generalized Pareto
#     "genhalflogistic", #Generalized Half Logistic
#     "gilbrat",         #Gilbrat
#     "gompertz",        #Gompertz (Truncated Gumbel)
#     "gumbel_l",        #Left Sided Gumbel, etc.
#     "gumbel_r",        #Right Sided Gumbel
#     "halfcauchy",      #Half Cauchy
#     "halflogistic",    #Half Logistic
#     "halfnorm",        #Half Normal
#     "hypsecant",       #Hyperbolic Secant
#     "invgamma",        #Inverse Gamma
#     # "invnorm",         #Inverse Normal
#     "invweibull",      #Inverse Weibull
#     "johnsonsb",       #Johnson SB
#     "johnsonsu",       #Johnson SU
#     "laplace",         #Laplace
#     "logistic",        #Logistic
#     "loggamma",        #Log-Gamma
#     "loglaplace",      #Log-Laplace (Log Double Exponential)
#     "lognorm",         #Log-Normal
#     "lomax",           #Lomax (Pareto of the second kind)
#     "maxwell",         #Maxwell
#     "mielke",          #Mielke's Beta-Kappa
#     "nakagami",        #Nakagami
#     "ncx2",            #Non-central chi-squared
# #    "ncf",             #Non-central F
#     "nct",             #Non-central Student's T
#     "pareto",          #Pareto
#     "powerlaw",        #Power-function
#     "powerlognorm",    #Power log normal
#     "powernorm",       #Power normal
#     "rdist",           #R distribution
#     "reciprocal",      #Reciprocal
#     "rayleigh",        #Rayleigh
#     "rice",            #Rice
#     "recipinvgauss",   #Reciprocal Inverse Gaussian
#     "semicircular",    #Semicircular
#     "t",               #Student's T
#     "triang",          #Triangular
#     "truncexpon",      #Truncated Exponential
#     "truncnorm",       #Truncated Normal
#     "tukeylambda",     #Tukey-Lambda
#     "uniform",         #Uniform
#     "vonmises",        #Von-Mises (Circular)
#     "wald",            #Wald
#     "weibull_min",     #Minimum Weibull (see Frechet)
#     "weibull_max",     #Maximum Weibull (see Frechet)
#     "wrapcauchy",      #Wrapped Cauchy
#     "ksone",           #Kolmogorov-Smirnov one-sided (no stats)
#     "kstwobign"
    ]       #Kolmogorov-Smirnov two-sided test for Large N


def fitdist(r):
    dist = pd.Series()    
    for cdf in cdfs:
        #fit our data set against every probability distribution
        parameters = eval("scipy.stats."+cdf+".fit(r)")
     
        #Applying the Kolmogorov-Smirnof one sided test
        D, p = scipy.stats.kstest(r, cdf, args=parameters)
     
        #pretty-print the results    
        dist[cdf] = p
    return dist

def fitassets(data, maxAssets=10):
    """ Tries to fit asset returns to distributions """
    assets = data.assetList[:maxAssets]
    fitStore = pd.HDFStore('distributions.h5')
    if not 'df' in fitStore.keys():
         fitStore['df'] = pd.DataFrame()

    pValues = pd.DataFrame()
    i = 0
    keys = fitStore['df'].keys()
    for symbol in assets:
        i = i+1
        if ("".join(["/", symbol]) not in keys):
            print 100.00*i/assets.count() , "%", symbol
            fitx = fitdist(data.assets[symbol]["excess_daily_ret"][:].dropna())
            pValues[symbol] = fitx
            fitStore['df'][symbol] = fitx

    pValues['AVG'] = pValues.mean(axis=1)        
    pValues = pValues.sort('AVG', ascending=False)    
    fitStore.close()
    return pValues

def show(r):
    sns.distplot(r, kde=False, fit=stats.johnsonsu);
    plt.show()

