So, based on this, if it is 4th and 5 (or less), go for it if you are on your own 45 or better. This would assume an average offense and an average defense. Since the 49ers have a poor short yardage offense, and a great defense (and a great punter), the numbers would shift more towards punting the ball. I imagine if you did the same analysis for college football, it would more strongly indicate going for it on 4th down.
As for the Bills, last I heard they had an above average offense, and they have an awful defense as we saw ourselves.