1

I am trying to modify the function below so that it gives the expected output below. For the first calculation it go like 100+ 100*-87/100 = 13 with the equation NonC_Amount + Int_amount * np.cumprod(PnL / 100). Since -87 is the first element in the PnL, for the second calculation it will go as 13 + 100*-4/100 = 9. the NonC_Amounts value is updated.

PnL = np.array([-87., -4., -34.1, 8.5])
Int_amount = 100
NonC_Amount = 100
PnL.prod(initial=Int_amount)
NonCompounding =NonC_Amount  + Int_amount  * np.cumprod(PnL / 100)

Current Output:

[ 13, 103.48 , 98.81332,  99.8991322]

Expected Output:

[ 13,  9,  -25.1,  -16.6]

1 Answer 1

3

You do the wrong calculations. From your description it seems you want to do

NonC_Amount = 100
NonCompounding = np.zeros_like(PnL)
for i in range(PnL.shape[0]):
    NonCompounding[i] = NonC_Amount + Int_amount * PnL[i] / 100
    NonC_Amount = NonCompounding[i]

Edit: If you want this operation vectorised, you can do

NonCompounding = NonC_Amount + np.cumsum(Int_amount * PnL / 100)

np.cumprod() gives you the cumulative product up to the ith element. For np.cumprod(PnL) that'd be

[-87, -87*(-4), -87*(-4)*(-34.1), etc...]

and it's only by pure chance it gives you the correct result for the first element.

Sign up to request clarification or add additional context in comments.

1 Comment

I have updated the issue sorry about the mistake it was supposed to be -16. instead of 16. if possible I am trying to avoid for loops as they are slow and inefficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.