Training Chain-of-Thought via Latent-Variable Inference