Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R translation to Python

Tags:

python

r

numpy

I have some code that I wrote in R that I would like to have translated into Python, but am new to python so need a bit of help

The R code basically simulates 250 random normals, and then calculated a geometric mean return of sorts and then a max drawdown, it does this 10000 times and then combines the results, as shown below.

mu <- 0.06
sigma <- 0.20
days <- 250
n <- 10000
v <- do.call(rbind,lapply(seq(n),function(y){
  rtns <- rnorm(days,mu/days,sqrt(1/days)*sigma)
  p.rtns <- cumprod(rtns+1)
  p.rtns.md <- min((p.rtns/cummax(c(1,p.rtns))[-1])-1)
  tot.rtn <- p.rtns[days]-1
  c(tot.rtn,p.rtns.md)
}))

This is my attempt in Python, (if you can make it shorter/more eloquent/more efficient please suggest as answer)

import numpy as np
import pandas as pd
mu = float(0.06)
sigma = float(0.2)
days = float(250)
n = 10000
rtns = np.random.normal(loc=mu/days,scale=(((1/days)**0.5)*sigma),size=days)
rtns1 = rtns+1
prtns = rtns1.cumprod()
totrtn = prtns[len(prtns)-1] -1
h = prtns.tolist()
h.insert(0,float(1))
hdf = pd.DataFrame(prtns)/(pd.DataFrame(h).cummax()[1:len(h)]-1))[1:len(h)]]

and that was as far as I got... wasn't too sure if hdf was correct to get p.rtns.md, and wasnt sure how I would go about simulating this 10000 times.

All suggestions would be greatly appreciated...

like image 540
h.l.m Avatar asked Dec 20 '13 23:12

h.l.m


1 Answers

I'm unfamiliar with R, but I see some general improvements that could be made to your Python code:

  • Use 0.06 without float() around, since Python will infer that a numeric value with a decimal point is a float
    • The last line, h.insert(0,float(1)) can be replaced with h.insert(0,1.0)
  • You can reference the last item in an iterable using [-1], the second-last using [-2], etc.:
    • totrtn = prtns[-1] -1

Python developers usually choose underscores between words or camelcase. In addition, it is normally preferable to use the full words in your variable names for readability over economy on-screen. For example, some variables here could be renamed to returns and total_returns or totalReturns.

To run your simulation 10000 times, you should use a for loop:

for i in range(10000):
    # code to be repeated 10000 goes in an indented block here
    # more lines in the loop should be indented at same level as previous line
# to mark what code runs after the for loop finishes, just un-indent again
h - prtns.tolist()
...
like image 90
SimonT Avatar answered Sep 19 '22 10:09

SimonT