Nagesh Joshi Nagesh Joshi - 2 months ago 11
Python Question

Increase the significant digits in Numpy

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

Answer

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.

Comments