0

I want to know how can I increase the number of significant digits beyond the decimal. The original "rf" numpy array contains floating point numbers.

import numpy as np
rf=daily_rets(df)

[ 7.11441183  7.12383509  7.13325787  7.16152716  7.17094994  7.17094994  7.18979692  7.18979692  7.19921923  7.19921923  7.19921923  7.19921923  7.19921923  7.19921923  7.19921923  7.20864296  7.20864296  7.20864296  7.20864296  7.20864296]

But when I perform the operation I get an undesired output

rf[0:]=(1+rf[0:]/100)**(1/252)

I get the following output [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

np.around() also does not help giving me the same output as above

rf[0:]=np.around((1+rf[0:]/100)**(1/252), decimals=6)

I realize the above operation would make the numbers very small, still I want the numbers beyond decimals to appear

Gabriel
  • 40,504
  • 73
  • 230
  • 404
Nagesh Joshi
  • 51
  • 1
  • 1
  • 6
  • 1
    Possible duplicate of [Python division](http://stackoverflow.com/questions/2958684/python-division) – ayhan Sep 03 '16 at 14:04
  • 1
    In Python 3 this is the result I get with `(1+rf[0:]/100)**(1/252)` applied on the first four elements: `array([ 1.00027276, 1.00027311, 1.00027346, 1.00027451])` Your problem is because of integer division. You can fix it by using floats `(1+rf[0:]/100.0)**(1/252.0)` or with other methods in the linked question. – ayhan Sep 03 '16 at 14:06
  • The problem still persists. I tried what you suggested , I am getting the same output – Nagesh Joshi Sep 03 '16 at 14:19
  • Can you run this and tell me the result you get? `import numpy as np; rf = np.array([7.11441183, 7.12383509, 7.13325787, 7.16152716]); print((1+rf[0:]/100.0)**(1/252.0))` – ayhan Sep 03 '16 at 14:22
  • 2
    Nagesh, did you change `100` to `100.0` *and* change `252` to `252.0`? Actually, the values in `rf` are floating point, so changing `100` to `100.0` shouldn't make a difference. (When using Python 2, you can save yourself from future issues like this by always putting `from __future__ import division` at the top of your python files.) – Warren Weckesser Sep 03 '16 at 14:27
  • 1
    @ayhan the problem got rectified. I made an error of not changing 250 to 250.0, sorry for the inconvenience. – Nagesh Joshi Sep 03 '16 at 15:32
  • Warren. Yup you are right. The mistake was on my part – Nagesh Joshi Sep 03 '16 at 15:32

2 Answers2

0

In python 2.7 dividing a numpy float by an integer will return an integer, at least that is my experience. As the answers say:

In [1]: import numpy as np

In [2]: rf = np.array([ 7.11441183,  7.12383509,  7.13325787,  7.16152716,  7.17
   ...: 094994,  7.17094994,  7.18979692,  7.18979692,  7.19921923,  7.19921923,
   ...:   7.19921923,  7.19921923,  7.19921923,  7.19921923,  7.19921923,  7.208
   ...: 64296,  7.20864296,  7.20864296,  7.20864296,  7.20864296])

In [3]: print (1+rf[0:]/100)**(1/252)
[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.]

In [4]: print (1+rf[0:]/100.0)**(1/252.0)
[ 1.00027276  1.00027311  1.00027346  1.00027451  1.00027486  1.00027486
  1.00027556  1.00027556  1.00027591  1.00027591  1.00027591  1.00027591
  1.00027591  1.00027591  1.00027591  1.00027626  1.00027626  1.00027626
  1.00027626  1.00027626]

Dividing by a float solves this problem, i.e change both 100 and 252 to 100.0 and 252.0. Hope that helps.

Llewyn S
  • 16
  • 6
0

You could just use numpy ufuncs:

from __future__ import division
import numpy as np
rf = np.array([7.11441183, 7.12383509, 7.13325787, 7.16152716])
np.divide(rf[0:], 100, rf[0:])
np.add(rf[0:], 1, rf[0:])
np.power(rf[0:], 1 / 252, rf[0:])

>>> rf
array([ 1.00027276,  1.00027311,  1.00027346,  1.00027451])
>>> 

Initially I thought numpy would handle the issue but as others have stated, it is the v2.7 integer division that is causing the problem. Hopefully the ufuncs above are not too much of a distractor.

wwii
  • 23,232
  • 7
  • 37
  • 77
  • This still contains the expression `1/252`, which is 0 in Python 2. Are you using Python 3? – Warren Weckesser Sep 03 '16 at 15:57
  • @WarrenWeckesser: I fooled myself, I had ```from __future__ import division``` at the top of my temp file. Corrected - but then the ufuncs are moot. – wwii Sep 03 '16 at 16:31