refactoring marginal probability function