Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Tag Info

9

You seem to have two distinct problems: How to generate random portfolios How optimal portfolios are structured Ad 1) A straightforward way to simulate the weights of random portfolios is to use the Dirichlet distribution $Dir(\alpha_1,\ldots,\alpha_n)$. This is a distribution on the Simplex (i.e. on $S=\{x\in\mathbb{R}^n | \sum x_i =1, x_i\geq 0\}$, ...

6

Let $R$ be a random vector of risky returns and let $r_f$ denote the risk free rate. Let vector of expected returns $\boldsymbol{\mu} = \operatorname{E}[R]$ and covariance matrix $\Sigma = \operatorname{Cov}(R)$. The maximum Sharpe ratio portfolio among risky assets is called the tangency portfolio. Quick method to tangency portfolio Let's find the ...

6

To complement @skoestimeier's answer on the shortselling-allowed case, I provide a vectorised version. Using the original notation in my post (you may change $r$ to something like $r-r_f$, but this doesn't affect the algebraic structure). Our goal is to find the maximiser for the problem $$\max_{w}f(w):=\frac{w^T r}{(w^T\Sigma w)^{1/2}}.$$ Let $$\phi: w\... 5 The Kelly Criterion aims to maximise the expected value of the logarithm of terminal wealth. The derivation starts off by assuming that there is a risky asset that is following a Geometric Brownian Motion:$$ \frac{\,dS}{S} = \mu \,dt + \sigma \,dZ_t $$This is combined with a riskless asset that is continuously compounding:$$ \frac{dB}{B} = r \,dt $$... 5 There are two cases, where short sales are allowed: With riskless lending and borrowing and without. As mentioned in the comments, you just have to solve a linear system. With riskless lending and borrowing The existence of a riskless lending and borrowing rate r_f implies that there is a single portfolio of risky assets, that is preferred to all other ... 4 Simulation for timeseries data is not a trivial matter and there are a number of methods to ensure you retain some of the relevant properties (mostly called dependent bootstrap methods): Block bootstrap - contiguous blocks of data chosen so that they are large enough to retain significant autocorrelations. Stationary bootstrap - randomised block size ... 4 Having read your comments I'm not sure where the implementation issue is: scipy.integrate.dblquad solves the following iterated integral problem:$$I(\alpha,\beta) = \int_{\alpha}^{\beta} \int_{g(x)}^{h(x)} f(x,y)\; dy \;dx $$If I re-express your variables to be consistent with the scipy documentation, and suppose I changed your integral to be finite:$$...

3

It would have been helpful had you provided links to those papers. But in general, you need to distinguish between the optimisation model, and the numerical technique used to solve the model. Suppose you wanted to estimate a linear regression, with the mean squared residual as the criterion of fit, and without further constraints. This is a model. Now you ...

3

This is a bit more complex than adding additional constraints. This is a well known problem in markowitz optimization - if you don't treat your covariance matrix and expected return vector with great care, markowitz will often spray your weights against the edges and result in a very non-diversified portfolio. I suggest robustly landscaping the literature - ...

3

If you are willing to switch to CVXPY, it comes with a pretty example of exactly this exercise: http://nbviewer.jupyter.org/github/cvxgrp/cvx_short_course/blob/master/applications/portfolio_optimization.ipynb

2

There are two transformations of the input data to be made to go from the first problem to the second: the $\hat{\mu}$ are found by subtracting the scalar $r_f$ from all the $\mu$ vector components: $$\hat{\mu}=\mu-r_f=(\mu_1-r_f,\mu_2-r_f,\cdots,\mu_N-r_f)^T$$ in other words the $\mu$ are returns and the $\hat\mu$ are "excess returns". the $\hat{A}$ ...

2

Kelly DOES reflect the odds! The simple binary bet form of Kelly is: Kelly Fraction = (p(win) * (odds + 1) - 1) / odds So for a 60% chance of a 50% risk, ie 1:1 equals odds 1, that’s 20% of your capital at risk. More formally, Kelly seeks to maximise log-wealth (LW) LW = sum ( Pi * ln(1 + Stake * Payoffi) Maximise LW, then dLW/dStake = 0 For each ...

2

Assuming the $\epsilon_i$ are zero mean, you should find that $$\mu - r_f = \beta \left(E[r_m] - r_f\right).$$ Further assuming the $\epsilon_i$ are independent of each other, though possibly with different variances, let $\Gamma$ be the diagonal matrix with the variances of $\epsilon_i$ on the diagonal. Then you are to find (under the more usual MVO ...

2

2

Try Quantopian. I believe they offer all that you are looking for. ps.: I have no affiliation with the company or anyone working there.

1

The problem you describe can be handled as an optimization problem: evolve a program such that it maximizes some performance measure. The technique you may want to look into is called "Genetic Programming". For a financial application see for example Single versus Multiple Tree Genetic Programming for Dynamic Decision Making.

1

Looking at this a second time, your implementation is probably not very inefficient since for; $$\quad \hat{f}(u) := \sum_{j}a_j \phi(u - z_j), \quad \int_{-\infty}^y \hat{f}(u) du = \sum_j a_j \Phi(y)\;,$$ where $\Phi(y)$ is the transformed cumulative normal distribution function of $y$. This function already exists as scipy.stats.norm.cdf and is ...

1

Do you have correctly formulated the problem for the solver ? If you want to maximise a function (the sharpe ratio) $f$, it is equivalent to minimise $-f$. This kind of confusion (minimising instead of maximising) would basically lead to a similar outcome as yours.

1

For those experiencing a similar problem, here is the solution that worked for me: ## OPTIMIZE PORTFOLIO WEIGHTS UNDER THE OBJECTIVE OF MAXIMIZING THE SHARPE RATIO ## WHILE CONSTRAINING THE WEIGHTS TO SECTOR BOUNDS ## PAPER: # ACCORDING TO: http://people.stat.sc.edu/sshen/events/backtesting/reference/maximizing%20the%20sharpe%20ratio.pdf np.random.seed(101)...

1

This is very simply done. It involves ensuring the constraints are presented as part of the matrix standard form. You will typically have the constraint that all assets sum to one, i.e. the matrix-vector equation: $$\delta^T x = 1$$ If you want to create an inequality constraint for assets in a sector just isolate them: $$\begin{bmatrix} 1 & 1 &... 1 I get the maximization problem$$ \max\limits_{w} \mathbb{E}\left[W_1\right] - \frac{\gamma}{2} Var(W_1)  st. W_1 = W_0(1 + r_f + w^Tr)So we have \begin{align*} L(w) &= \mathbb{E}\left[W_1\right] - \frac{\gamma}{2} Var(W_1)\\ & = \mathbb{E}\left[W_0(1 + r_f + w^Tr)\right] - \frac{\gamma}{2} Var(W_0(1 + r_f + w^Tr))\\ & = W_0 + ... 1 Please let me know if you have any questions. 1 To optimize:z = f(g(x))$$using traditional calculus with chain rule:$$ \frac{dz}{dx} = \frac{df}{dg} \frac{dg}{dx} $$Set \frac{dz}{dx} = 0 and that will determine either minimum, maximum or saddle points. 1 The simplest is to get the admissible return range using the cvxopt optimizer with q = \alpha \mu and q = -\alpha \mu for a large \alpha instead of q=0 and then run the function compute_ep iteratively to find the portfolio with the highest Sharpe ratio in this range. Using code similar to the one posted above, one can get the bounds and optimal ... 1 If you're happy with equal stock weightings, then this can certainly be done iteratively. I don't know of any closed-form equation. It works for a universe of 100 stocks, but the calculations obviously grow exponentially if you want to increase your selection universe. Pick five stocks at random. Calculate the portfolio variance. There are then 4 * 95 =... 1 Consider the Lagrangian:$$L = \sum_{t=0}^{\infty} \beta ^t \{ U(c_t) + \lambda _t [(1-\delta)k_t+f(k_t) - k_{t+1}-c_t]\}$$The FOC of the Lagrangian with respect to c_t gives:$$\frac{\partial L}{\partial c_t} = 0 \Rightarrow U'(c_t) = \lambda_t$$The FOC with respect to k_{t+1} gives:$$\frac{\partial L}{\partial k_{t+1}} = 0 \Rightarrow \lambda_t ...

1

For Java, combining @vjond answer (as the initial starting estimate for implied Volatility), with a basic Cumulative Density Function for Normal distribution (CDN), and the Black Scholes Model applied to Newton Raphson, below is a basic Implied Volatility calculation for Java : For the NR code (C), plz refer http://finance.bi.no/~bernt/gcc_prog/recipes/...

Only top voted, non community-wiki answers of a minimum length are eligible