Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trying to determine probability of a value x's occurrence given a mean and std.dev of a normal dist.

Hello all, just joined a couple days ago and its my first time doing any form of coding, have an algo almost done, but have run into a bit of an issue.
If I have a stock, AAA, and after doing some testing, have determined that it follows a normal distribution very highly. What I want to do is to take that stock, and given its average and standard deviation from the last 252 days and its current price, return to me a probability.
IE, if the mean is 10, and the standard deviation is 2, then if i plug in a 12, it would return 69.1%. Here is what I have so far:

def initialize(context):
context.stocks = symbols('AAA')
schedule_function(rebalance,
date_rule = date_rules.every_day(),
time_rule = time_rules.market_open())

def rebalance(context, data):
MA = data[symbol('AAA')].mavg(252)
SP = data[symbol('AAA')].price
SD = data[symbol('AAA')].stddev(252)

I would then want to use my average (MA), current price (SP), and std.dev (SD). to give me the probability(P) of that current price occuring on a normal distribution of mean MA and Stddev SD, which i would then turn into an order like so.

if (SP >( MA + .5*SD)):
order_target_percent('AAA', -P)
order_target_percent('Hedge stock',1- P)
context.short=True
if (SP >(MA - .5*SD)):
order_target_percent('AAA', P)
order_target_percent('Hedge stock', P-1)
context.short=False

5 responses

Sam.

I know that this simple Stock-Bond Balance strategy is not what you are trying to implement.
But may be it will be a good benchmark to beat, using normalized distribution adaptive long-short portfolio.

Eyes open, know that it spent $3.2 million on $1M starting capital.

def handle_data(context, data):  
    record(lvg = context.account.leverage)

    if 'cash_low' not in context:  
        context.cash_low = context.portfolio.cash  
    if context.portfolio.cash < context.cash_low:  
        context.cash_low = context.portfolio.cash  
        record(CashLow = context.cash_low)   # Lowest cash level hit  

Gary.

If there is a bug in my code and you know the problem then post a fixed one, but better help Sam to create his dream: normalized distribution adaptive long-short portfolio of two.

Some tools that might help, using the latest Track Orders set to turn on just before the first cash drop. PvR beats SPY and if you can resolve the low cash, it doesn't necessarily mean lower PvR, could wind up far better. Note this is 100K b/c I thought it might have to do with large unfilled orders, maybe compare to 50K and 1M.

http://stackoverflow.com/questions/20626994/how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p

That might help, but I wonder what it is exactly that you are trying to model? If you are assuming that prices follow a random walk (normal or lognormal, usually the latter), then you can't really answer questions about probability without knowing the time at which you want to estimate the probability of the price being at (or above?) a specific value. Or, are you interested in the probability of the price touching a specific value, even if it subsequently retreats? That's a barrier problem. In any case, a lot of these probabilities can be calculating using the mathematics of options pricing, so you might want to investigate that.