def calc_kelly_leverages(securities: Set[str],
                         start_date: date,
                         end_date: date,
                         risk_free_rate: float = 0.04) -> Dict[str, float]:
    """Calculates the optimal leverages for the given securities and
    time frame. Returns a list of (security, leverage) tuple with the
    calculate optimal leverages.

    Note: risk_free_rate is annualized
    """
    f = {}
    ret = {}
    excess_return = {}

    # Download the historical prices from Yahoo Finance and calculate the
    # excess return (return of security - risk free rate) for each security.
    for symbol in securities:
        try:
            hist_prices = get_historical_data(
                symbol, start=start_date, end=end_date,
                output_format='pandas')
        except IOError as e:
            raise ValueError(f'Unable to download data for {symbol}. '
                             f'Reason: {str(e)}')

        f[symbol] = hist_prices

        ret[symbol] = hist_prices['close'].pct_change()
        # risk_free_rate is annualized
        excess_return[symbol] = (ret[symbol] - (risk_free_rate / 252))

    # Create a new DataFrame based on the Excess Returns.
    df = DataFrame(excess_return).dropna()

    # Calculate the CoVariance and Mean of the DataFrame
    C = 252 * df.cov()
    M = 252 * df.mean()

    # Calculate the Kelly-Optimal Leverages using Matrix Multiplication
    F = inv(C).dot(M)

    # Return a list of (security, leverage) tuple
    return {security: leverage
            for security, leverage in zip(df.columns.values.tolist(), F)}
Example #2
0
def get_mode(arr):  
    mode = [];  
    arr_appear = dict((a, arr.count(a)) for a in arr);  # 统计各个元素出现的次数  
    if max(arr_appear.values()) == 1:  # 如果最大的出现为1  
        return;  # 则没有众数  
    else:  
        for k, v in arr_appear.items():  # 否则,出现次数最大的数字,就是众数  
            if v == max(arr_appear.values()):  
                mode.append(k);  
    return mode;  

get_mode(a)

var(a)
std(a)

a=Series(a)
a.skew()
a.kurt()
a.describe()

df = DataFrame({'data1' : np.random.randn(5),
                'data2' : np.random.randn(5)})
df.cov()
df.corr()

###假设检验
from scipy import stats as ss
df=DataFrame({'data':[10.1,10,9.8,10.5,9.7,10.1,9.9,10.2,10.3,9.9]})
ss.ttest_1samp(a = df, popmean = 10)
print "*"*15

print "Definimos de nuevo el dataframe"
df = pd.DataFrame(data={"A":[1,2], "B":[2.6,1.3]})
print df
print "añadimos columnas combinando las actuales"
df["C"] = df["A"]+df["B"]
df["D"] = df["A"]*3
df["E"] = np.sqrt(df["A"])
print df
print "*"*15
print "Datos disponibles de un dataframe"
print " descripcion del dataframe"
print df.describe()
print " covarianza "
print df.cov()
print " correlación "
print df.corr()
print "*"*15

print " Creamos otro dataframe con valores aleatorios (1000 filas y 2 columnas "
print " DataFrame(np.random.randn(1000,2),columns=['x','y'])"
plot_df = DataFrame(np.random.randn(1000,2),columns=['x','y'])
print plot_df
print "Mostramos las graficas"
plot_df.plot()
plot_df.hist()



Example #4
0
print(df5)

import math


def int_float_squares(series):
    return pd.Series({"int_sq": series["int_col"] ** 2, "flt_sq": series["float_col"] ** 2})


print(df.apply(int_float_squares, axis=1))

### 7. Basic Stats ###

print(df.describe())
print(df.cov())
print(df.corr())

### 8. Merge and Join ###

print(df)
other = DataFrame({"str_col": ["a", "b"], "some_val": [1, 2]})
print(other)
print(pd.merge(df, other, on="str_col", how="inner"))
print(pd.merge(df, other, on="str_col", how="outer"))
print(pd.merge(df, other, on="str_col", how="left"))
print(pd.merge(df, other, on="str_col", how="right"))

### 9. Plot ###

plot_df = DataFrame(np.random.randn(1000, 2), columns=["x", "y"])
obj.reindex()

data = DataFrame([[1,2,3],[4,5,6]])
data.drop()

np.argsort()

obj.rank()

obj.sort_values()


data.tail()

data.cov()

data.cov()

data.corr()

data.dropna()

data.loc


data.fillna()

data.unstack()

for i in range(len(exchange_rates)-1):
    row = {}
    for tp, cur in zip(metadata['Time Period'], metadata['Currency:']):
        x1 = float(exchange_rates[tp][i])
        x2 = float(exchange_rates[tp][i+1])

        if cur == 'USD':
            x1 = 1.0 / x1
            x2 = 1.0 / x2

        # Returns are in units of %.
        row[tp] = 100 * (x1 - x2) / x2
    rows.append(row)

returns = DataFrame(data=rows, columns=list(metadata['Time Period']))
returns_cov = returns.cov()

# Means are the expected returns for each currency.
exp_returns =  concat({'mean': returns.mean(), 'variance': returns.var()}, axis = 1)

class CurrencyPortfolio(YhatModel):
    @preprocess(in_type=dict, out_type=dict)
    def execute(self, data):
        P = matrix(data['risk_aversion'] * returns_cov.as_matrix())
        q = matrix(-exp_returns['mean'].as_matrix())
        G = matrix(0.0, (len(q),len(q)))
        G[::len(q)+1] = -1.0
        h = matrix(0.0, (len(q),1))
        A = matrix(1.0, (1,len(q)))
        b = matrix(1.0)
        try:
            hist_prices = web.DataReader(symbol, 'yahoo', start_date, end_date)
        except IOError, e:
            print 'Unable to download data for %s. Reason: %s' % (symbol, str(e))
            return None

        f[symbol] = hist_prices

        ret[symbol] = hist_prices['Adj Close'].pct_change()
        excess_return[symbol] = (ret[symbol] - (risk_free_rate / 252))  # risk_free_rate is annualized

    # Create a new DataFrame based on the Excess Returns.
    df = DataFrame(excess_return).dropna()

    # Calculate the CoVariance and Mean of the DataFrame
    C = 252 * df.cov()
    M = 252 * df.mean()

    # Calculate the Kelly-Optimal Leverages using Matrix Multiplication
    F = inv(C).dot(M)

    # Return a list of (security, leverage) tuple
    return zip(df.columns.values.tolist(), F)


def main():
    """Entry point of Kelly Criterion calculation."""

    print "Kelly Criterion calculation"
    args = docopt(__doc__, sys.argv[1:])
Example #8
0
            3   3.5 0.5
        ]

    calculate var, cov, corr, cov-matrix, corr-matrix
"""

data = {'x1': [1, 2], 'x2': [2, 3], 'x3': [2, 1]}
A = DataFrame(data)
print A

data1 = Series([3, 3.5, 0.5], index=['x1', 'x2', 'x3'])
A = A.append(data1, ignore_index=True)
print '\n', A, '\n'

var_x1 = A['x1'].var()
var_x2 = A['x2'].var()
var_x3 = A['x3'].var()

print 'var_x1: %f' % var_x1
print 'var_x2: %f' % var_x2
print 'var_x3: %f' % var_x3

cov_matrix = A.cov()
print '\ncov_matrix:\n', cov_matrix

corr_matrix = A.corr()
print '\ncorr_matrix:\n', corr_matrix

print "\nA['x1'].corr(A['x2']): %f" % A['x1'].corr(A['x2'])
print "A['x1'].corr(A['x3']): %f" % A['x1'].corr(A['x3'])