Optimal simultaneous maximum a posterioriestimation of states, noise statistics and parameters I. Algorithm