We can integrate Cython and NumPy code in the same way we can integrate Cython and Python code. Let's go through an example that analyzes the ratio of up days (days on which a stock closes higher than the previous day) for a stock. We will apply the formula for binomial proportion confidence. You can refer to http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval for more information. The following formula indicates how significant the ratio is:
In the formula, p is the probability and n is the number of observations.
This section describes how to use Cython with NumPy, with the following steps:
Write a
.pyx
file that contains a function that calculates the ratio of up days and the associated confidence. First, this function computes the differences between the prices. Then, it counts the number of positive differences, giving us a ratio for the proportion of up days. Finally, apply the formula for confidence from the Wikipedia page in the...