Search code examples
pythonpython-3.xfinancecomplex-numbers

Annual return conversion function returns complex value


I am writing a Python function that converts a compound return rate into an annualised one:

annual_rate = ((1 + ((1 + compound_rate) ** (1 / nb_days) - 1)) ** 365 - 1)

This works fine

compound_rate = 0.1
nb_days = 100
>>0.41606536550485806

But in some cases, a complex number is returned

compound_rate = -1.8536
nb_days = 362
>>(-1.852192062046392-0.022192088919279443j)

Here is a real-life example:

If I have a payment deposit table as per the following:

03/03/2018  £1000
03/04/2018  £1000
03/05/2018  £1000
03/06/2018  £1000
03/07/2018  £1000
03/08/2018  £1000
03/09/2018  £1000
03/10/2018  £1000
03/11/2018  £1000
03/12/2018  £1000
03/01/2019  £1000
03/02/2019  £1000

.. and if I lose all the money and end up with £100 at the end of February 2019, I would have put in £12000, with a loss of £11900, over 362 days.

If I apply my formula from there, a complex number is returned (see second example above).

Is there something that I am not seeing here? Quite literally, this result does not feel real to me.


Solution

  • This might be what you are looking for:

    import numpy as np
    l = [-1000]*12
    l.append(100)
    print(l)
    #[-1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 100]
    print(np.irr(l))
    #-0.9090909090908827