In multi-instance learning only bags of instances are labeled: a bag is positive if any of its instances is positive, , and negative if all are negative. Instance labels are never observed.
Instances are the rows of X (shape (M, D)); bag_ids[m] in 0..n_bags-1 says which bag row m belongs to. An instance is positive with probability (no separate bias; include a constant column in X if needed).
bag_probs(instance_probs, bag_ids, n_bags, mode): the probability that each bag is positive, (noisy-OR) when mode == "noisy_or", or when mode == "max". Return an array of length n_bags.
mil_nll_and_grad(w, X, bag_ids, y_bag): the mean over bags of the negative log-likelihood under the noisy-OR model, , and its exact gradient with respect to w. Compute as . Return (nll, grad) with nll a Python float.
fit_mil(X, bag_ids, y_bag, lr, n_steps): start from and take n_steps gradient-descent steps of size lr on the mean NLL; return w.