I know Python, but I don't know C++. I'm trying to maximize a function that takes a long time to evaluate. I believe a good workflow would be to write the function that evaluates the function in C++ and use this function with scipy.optim.minimize to find the optimum. As an example, suppose I am maximizing a likelihood.
import pandas as pd
import numpy as np
from scipy.optimize import minimize
from scipy.stats import norm
# simulating data
means = np.array([10, 20, 30])
cov = np.diag([1, 4, 10])
N = 1000
df = pd.DataFrame(np.random.multivariate_normal(mean=means, cov=cov, size=N),
columns=['a', 'b', 'c'])
df[np.random.choice([True, False], size=(N, 3), p=[0.3, 0.7])] = np.nan
# a function to print parameters used in likelihood function
def print_params(params):
print('Means: {}'.format(params[:3]))
print('Variances: {}'.format(np.exp(params[3:])**2))
# defining likelihood
def llf(params):
logll = 0
for i in df.index:
for j,col in enumerate(['a', 'b', 'c']):
if not np.isnan(df.loc[i, col]):
m = params[j]
sd = np.exp(params[j+3])
logll += np.log(norm.pdf(df.loc[i, col], loc=m, scale=sd))
print_params(params)
return -logll
opt = minimize(llf, x0=np.array([0, 0, 0, 1, 1, 1]), options={'maxiter':30})
print_params(opt.x)
There may be more efficient ways to write the llf
function in pure Python, and there are definitely ways to speed up the optimization routine (e.g., by choosing a specific optimizer suited to the problem, or by supply derivatives), but this is not the focus of this question. I chose this particular example because I have a loop (I'm using all of the data, including rows where some columns have missing values) to evaluate the likelihood, which takes a lot of time in pure python, especially if my sample size grows.
How can I write the likelihood function in C++ and combine that with the Python minimize routine? Keep in mind, I have no experience with C++ but am willing to learn. However, a lot of the resources available for this seem to presume C++ knowledge, see Extending Python for example. I'm looking for resources for someone who knows Python, but is completely ignorant of C++ and methods for combining Python with C++. EDIT: Perhaps an example of how to do this using my example or information about the likely gains from combining Python and C++ would be useful.