# Time in years; one update per month
start_time <- 0
end_time <- 5
delta_t <- 1 / 12
time <- seq(start_time, end_time, by = delta_t)
# Parameters and initial conditions
annual_inflation_rate <- 0.05
annual_interest_rate <- 0.04
annual_depreciation_rate <- 0.08
monthly_contribution <- 600
new_car_initial <- 30000
trade_in_initial <- 13000
savings_initial <- 6000
# Create and initialize three stock vectors
# TODO
# Update all three stocks each month
for (i in 2:length(time)) {
# TODO
}
# Organize derived quantities
# TODO: create a data frame and calculate cash_neededThe situation
A young professional wants to buy a new car without borrowing money. While they save, three things happen:
- the new car’s price rises with inflation;
- the current car loses trade-in value; and
- savings earn interest while the buyer adds money each month.
Build a monthly simulation that determines the first month the purchase becomes possible. Then change the monthly contribution once to see how that decision affects the result.
Base-case information
Use these values for your first simulation:
| Quantity | Initial value | Rate or contribution |
|---|---|---|
| New-car price | $30,000 | 5% growth per year |
| Current-car trade-in value | $13,000 | 8% decay per year |
| Savings | $6,000 | 4% growth per year |
| Monthly contribution | — | $600 per month |
Simulate at least 5 years using a time step of one month. Because the proportional rates are annual, use \(\Delta t=1/12\) year. The $600 contribution is already a per-month amount, so do not multiply it by \(\Delta t\).
Your model
Your simulation needs three stocks:
- new-car price;
- trade-in value; and
- savings balance.
Before coding, describe how each stock changes during one month. You may use equations or clear sentences. Make sure the signs and units are clear.
The buyer can afford the purchase when
\[ \text{savings} + \text{trade-in value} \geq \text{new-car price}. \]
Equivalently, savings must cover the new car’s price minus the trade-in value. Call this difference the cash needed.
Build the simulation
Use the same structure as Module 2.2:
The downloadable template already contains these parameters and headings. Your job is to complete and explain the model. Use meaningful names, indentation, and a few useful comments.
Required evidence
Your report must include all of the following.
1. Model
- Briefly explain how the new-car price, trade-in value, and savings change each month.
- State two assumptions made by the model.
2. Verification
Calculate the first month’s values by hand and compare them with your simulation’s first two rows. Explain whether they agree.
3. Base-case result
- Report the first month when the buyer can afford the car.
- Give the savings and cash needed in that month.
- Inspect the previous month to confirm that you found the first affordable month.
4. Visualization
Create one clearly labeled graph showing:
- savings; and
- cash needed for the purchase.
Describe where the two trajectories cross. Your graph needs a title, axis labels with units, distinguishable lines, and a legend.
5. One experiment
Change the monthly contribution from $600 to either $400 or $800. Before running the modified model, predict whether the purchase will become possible earlier or later.
Report the new purchase month and explain the difference from the base case. You may duplicate and modify your original code; writing a function is not required.
6. Conclusion
Write a short recommendation to the buyer. Include the base-case result, what your experiment revealed, and two important things the model leaves out.
A useful R pattern
After creating a data frame named results, this expression finds positions where the purchase is affordable:
which(results$savings >= results$cash_needed)The first value returned is a vector position, not automatically a month number. Use the corresponding value in your time or month column. Also decide what your program should report if the condition is never met during the simulated interval.
Evaluation
This project is graded holistically on a 10-point scale. Your score reflects how well the completed report demonstrates a correct model, working and readable R code, appropriate verification, a clear graph and result, a meaningful experiment, and an understanding of the model’s limitations.
Before submitting
- Render the document from a fresh R session.
- Make sure every graph and table appears in the HTML file.
- Check that units are consistent.
- Remove abandoned code and debugging output.
- Proofread the narrative, captions, and conclusion.
- Submit both the
.qmdand.htmlfiles to Moodle.