Variational calculation of vibrational linear and nonlinear optical properties