Inferring latent factors and states underlying behavior and neural dynamics