How to create a new numpy array from a calculation of elements within an existing numpyarray
<p>I'm a Python and Numpy newbie...and I'm stuck.  I'm trying to create a new numpy array from the log returns of elements in an existing numpy array (i.e. new array = old array(with ln(x/x-1)).   I was not using Pandas dataframe because I plan to incorporate the correlations of the returns (i.e. "new array) into a large monte carlo simulation.  Open to suggestions if this is not the right path.</p>

<p>This is the closest result I found in stackflow search, but it is not working:
<a href="http://stackoverflow.com/questions/30087636/what-is-the-most-efficient-way-to-get-log-returns-in-numpy">What is the most efficient way to get log returns in numpy</a></p>

<p>My guess is that I need to pass in the elements of the existing array but I thought using arrays and functions within Numpy was the whole benefit of moving away from Pandas series and Python base code.   Appreciate help and feedback!</p>

<p>code link(I'm new so stackflow won't let me embed images): <a href="http://i.stack.imgur.com/wkf56.png" rel="nofollow">http://i.stack.imgur.com/wkf56.png</a></p>

<p>Numpy as <code>log</code> function, you can apply it directly to an array. The return value will be a new array of the same shape. Keep in mind that the input should be an array of positive values with <code>dtype == float</code>.</p>

<pre><code>import numpy
old_array = numpy.random.random(5.) * 10.

new_array = numpy.log(old_array / (old_array - 1.))

print type(old_array)
# &lt;type 'numpy.ndarray'&gt;
print old_array.dtype
# float64
print old_array
# [ 8.56610175  6.40508542  2.00956942  3.33666968  8.90183905]
print new_array
# [ 0.12413478  0.16975202  0.68839656  0.35624651  0.11916237]
</code></pre>

python arrays numpy