Nonlinear response theory for inhomogeneous systems in various ensembles