Equivalence and Nonequivalence of the Microcanonical and Canonical Ensembles: A Large Deviations Study