This project simulates the evolution of personal net worth under two alternative housing strategies:
- Buying a home using a mortgage.
- Renting a home and investing the available capital.
The simulator generates 10,000 economic scenarios using a block-bootstrap approach based on historical data. Each scenario includes monthly paths for:
- Inflation
- Real estate returns
- Bond returns
- Equity returns
Historical data are sourced from the Federal Reserve Economic Data (FRED) database:
The simulator is written in Odin:
- Odin language: https://odin-lang.org/
src/buy-vs-rent/– Source code of the simulator.src/buy-vs-rent/history_generation/– Generation of historical bootstrap scenarios.src/buy-vs-rent/portfolio/– Portfolio, mortgage, buyer and tenant simulations.data/– Historical datasets and example configuration files.output/– Generated binary simulation results..devcontainer/– Development container configuration for VS Code.
This project is provided for educational and informational purposes only. It is not financial, investment, legal, or tax advice.
Nothing in this repository should be interpreted as a recommendation to buy, sell, or hold any security, nor as a substitute for individualized professional advice.
By using this code and any outputs derived from it, you acknowledge and accept that:
- The code is provided "as is", without warranties of any kind, express or implied.
- Results obtained from historical simulations are not predictive of future performance.
- Data sources may contain inaccuracies, omissions, revisions, or delays.
- You are responsible for validating all inputs, assumptions, and outputs before relying on them.
- Any investment, housing, tax, or legal decision should be discussed with qualified professionals.
The simulator generates future economic paths by randomly sampling blocks of historical monthly returns.
For each scenario:
- Random historical starting dates are selected.
- Consecutive blocks of historical observations are extracted.
- The blocks are concatenated to create a synthetic future path.
- Buyer and tenant portfolios are simulated on the same economic scenario.
This approach preserves much of the short-term correlation structure observed in historical data while allowing many alternative future paths to be explored.
Install the Odin compiler:
https://odin-lang.org/docs/install/
Create a JSON configuration file describing the simulation parameters. An example is provided in data/defaultConfig.json
odin run src/buy-vs-rent/ -- --config path/to/your/config.jsonSimulation results are written to the output/ directory as binary files containing float32 values.
Examples:
output/buyNetWorth.bin
output/rentNetWorth.bin
output/buyRealEstate.bin
output/buyDebt.bin
output/buyMarket.bin
The arrays are stored in row-major form:
(month, scenario)
and can be reshaped as:
(n_months, n_scenarios)Python: https://www.python.org/
Example:
import numpy as np
import matplotlib.pyplot as plt
buyNetWorth = np.fromfile(
"output/buyNetWorth.bin",
dtype=np.float32,
).reshape((601, 10000))
rentNetWorth = np.fromfile(
"output/rentNetWorth.bin",
dtype=np.float32,
).reshape((601, 10000))
horizon = 12 * 30
plt.plot(
np.mean(buyNetWorth, axis=1)[:horizon],
label="Buy",
)
plt.plot(
np.mean(rentNetWorth, axis=1)[:horizon],
label="Rent",
)
plt.legend()
plt.xlabel("Months")
plt.ylabel("Net Worth")
plt.show()Potential extensions include:
- Specific tax systems.
- Transaction cost modelling.
- Interactive visualizations.