Multifractal formalism from large deviations