We can call C functions from Cython. In this example, we call the C
log() function. This function works on a single number only. Remember that the NumPy
log() function can also work with arrays. We will compute the so-called log returns of stock prices.
We start by writing some Cython code:
log()function from the
libcnamespace. Then, apply this function to numbers in a
forloop. Finally, use the NumPy
diff()function to get the first-order difference between the log values in the second step:
from libc.math cimport log import numpy as np def logrets(numbers): logs = [log(x) for x in numbers] return np.diff(logs)
Building has been covered in the previous recipes. We only need to change some values ...