This calculator attempts to formalize the opportunity cost of buying vs renting, in the context of potential alternative investments.
Disclaimer: these calculations are not intended as financial advice. Please cross-check results here with another source.
Note that this calculator assumes you will reinvest all payments not put toward buying/renting into external investments, and that you do not need to sell external investments to make payments.