1

I currently have a dataframe as below:

enter image description here

and wish to add a column, E, that is calculated based on the following function.

def geometric_brownian_motion(T = 1, N = 100, mu = 0.1, sigma = 0.01, S0 = 20):
    dt = float(T)/N
    t = np.linspace(0, T, N)
    W = np.random.standard_normal(size = N)
    W = np.cumsum(W)*np.sqrt(dt) ### standard brownian motion ###
    X = (mu-0.5*sigma**2)*t + sigma*W
    S = S0*np.exp(X) ### geometric brownian motion ###
    return S

(originating from here)

How to i create a time-series for all of the dates contained within the data-frame and append it?

The function input parameters are as follows:

T = (#days between df row 1 and df last)/365

N = # rows in data frame

S0 = 100

S.Peters
  • 185
  • 1
  • 1
  • 9

1 Answers1

2

As i understand the essense of question is how to apply some method to every column, taking into account, the fact that to calculate a new value you need an index from dataframe:

I suggest you to extract index as separate column and use apply as usually.

from functools import partial
df['index'] = df.index
T = # precalculate T here
N = df.shape[0]
applying_method = partial(geometric_brownian_motion,T=T,N=N, S0=100)
df['E'] = df.apply(lambda row: applying_method(*row),axis=1)

Or if you rename columns of dataframe accroding to you function arguments:

df['E'] = df.apply(lambda row: applying_method(**row),axis=1)

Hope that helps.

Grigory
  • 679
  • 1
  • 4
  • 22
  • Thanks for your response. I'm not actually looking to apply the function to each column, just to create a time series for column [E] based on the dates in the index and the function above. – S.Peters Sep 05 '17 at 14:05
  • I updated answer, @S.Peters . I could improve it, and describe how to apply your function, if you rename columns in the original dataframe, so that they match the arguments of the function. – Grigory Sep 05 '17 at 14:16
  • Updated question with definition of function inputs. – S.Peters Sep 05 '17 at 14:36
  • Sorry, but I still confused about how the dataframe columns relate to the arguments of the function. What means 'A','B','C','D', of dataframe? Please, name them identically to arguments of the function. – Grigory Sep 05 '17 at 14:43
  • they are simply existing columns, the only relevance is that i need to use the existing dates to create the time-series for column E. Basically trying to replicate the example in the link above, but rather than apply it to a range of dates, apply it to the dates in an existing dataframe and add the results as a column. – S.Peters Sep 05 '17 at 14:45