liveBook · Manning

Numerical differentiation

As explained in Build AI Drug Discovery Pipelines

The finite difference method numerically approximates derivatives from function values at nearby input points.

Numerical differentiation estimates how a model prediction changes when one descriptor is slightly increased and decreased, supporting local sensitivity analysis for RNA-binder interpretation. section 6.4.3

f'(x)≈f(x+h)-f(x-h)2h

Apply the central difference equation by evaluating the model at x+h and x−h, then dividing their prediction difference by 2h. section 6.4.3

Descriptor sensitivity for an RNA binder

Perturb each descriptor in a single ROBIN RNA-binder representation, call model.predict_proba for both perturbed inputs, and retain derivatives above 0.01 for visualization. section 6.4.3

Listing 6.9 Partial Derivative Analysis
def partial_derivative(model, X, feature_idx):
    delta = 5e-2
    X_plus = X.copy()
    X_plus[:, feature_idx] += delta
    y_pred_plus = model.predict_proba(X_plus)[:, 1]
    X_minus = X.copy()
    X_minus[:, feature_idx] -= delta
    y_pred_minus = model.predict_proba(X_minus)[:, 1]
    partial_derivative = (y_pred_plus - y_pred_minus) / (2 * delta)
    return partial_derivative
 
partial_derivatives = np.zeros(X_all_2.shape[1])
for i in range(X_all_2.shape[1]):
    partial_derivatives[i] = partial_derivative(
        best_model, X_all_2[:1], i)[0]
feature_names = df_all_2.drop(
    columns=['Library']
).columns[partial_derivatives > 1e-2]
partial_derivatives = partial_derivatives[partial_derivatives > 1e-2]
 
plt.bar(
    range(len(partial_derivatives)),
    partial_derivatives,
    tick_label=feature_names
)

The implementation copies feature arrays, applies delta=5e-2 in opposite directions, computes prediction differences, and repeats across descriptors. section 6.4.3

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Explains central differences, perturbation size, and competing approximation and calculation errors, but stops short of deriving the formula.
Examples
Uses descriptor perturbations, a single ROBIN RNA binder, prediction probabilities, and a thresholded derivative bar chart.
What it assumes
Assumes descriptor-based model predictions and introduces perturbation-based derivative estimation through code.
What you can do after
Diagnose/debug descriptor influence by implementing central differences and visualizing meaningful local derivatives.
How it connects
Connects finite differences with partial derivative analysis, local sensitivity analysis, model interpretation, and SHAP.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.