How Linear Regression Actually Works for Stock Prices
Learn the math behind the simplest supervised learning model. Spoiler: it's just fitting a line.
A practical walkthrough to gather TSX data, prepare it for training, and build your first supervised learning model without needing a data science degree.
Editorial Team
Written by the MarketPulse Toronto editorial team, focused on clear, practical guidance for learning supervised learning in stock analysis.
You don't need to pay for expensive financial data. We're talking free, reliable sources that most people don't realize exist. Yahoo Finance gives you daily TSX data going back years. You'll get opening prices, closing prices, volume — everything you need. Download it as CSV and you're done.
Alpha Vantage offers a free API tier with 5 requests per minute. Not blazing fast, but perfect for learning. The Canadian government actually publishes historical market data too. It's not as convenient as Yahoo, but it's there if you want to diversify your sources.
Start small. Pick 3-5 TSX stocks you recognize — something like BCE, TD, or Shopify. Download 2-3 years of data for each. That's enough to build something real without overwhelming yourself with processing time.
Raw data is messy. You'll find missing values, weird spikes that don't make sense, formatting issues. Don't skip this step. Your model is only as good as what you feed it. Spend time actually looking at your data — plot it, scan through the numbers, understand what you're working with.
Remove rows with missing prices. Check for stock splits or dividends that might cause artificial jumps. If you see a 20% drop in one day for no reason, investigate it. Sometimes it's real news. Sometimes it's a data error. You need to know the difference.
Normalize your features. Stock prices for different companies are on different scales. A $300 stock and a $50 stock need to be treated fairly when your model learns from them. Most people use min-max scaling or standardization. Pick one, apply it consistently, and move on.
Individual learning outcomes vary from person to person. This guide is for educational purposes to help you understand how supervised learning models work with real market data. Always test thoroughly with historical data before applying any model to real trading decisions.
Linear regression gets a lot of hate in finance, but it's the perfect starting point. It's simple, it's interpretable, and it teaches you the fundamentals without drowning you in complexity. You're essentially drawing a line through historical data and saying "if the pattern continues, here's where the price goes."
Split your data: 70% for training, 30% for testing. Train your model on the historical data, then see how it performs on data it's never seen. This is where you'll discover whether you actually built something useful or just memorized historical prices.
Measure your results with metrics that matter. Mean Absolute Error (MAE) tells you how far off you are on average. Root Mean Squared Error (RMSE) punishes bigger mistakes harder. Pick one, track it, and use it to compare different approaches later.
Your model performed great on training data? Congratulations, that probably means nothing. You need to test on completely separate data. Walk forward validation is your friend here. Train on 2021-2022, test on 2023. That's closer to real-world conditions than random train-test splits.
Look for overfitting. If your model nails historical data but tanks on new data, you've memorized patterns that don't actually exist in the future. Add regularization, simplify your model, or collect more data. There's no magic fix — just trade-offs.
Backtest across different market conditions. Test on 2020 (the crash), 2021 (recovery), and 2022 (interest rates rising). If your model only works in one type of market, you haven't actually built something reliable. You've built something that got lucky once.
You've got the roadmap now. Download some data. Build a linear regression model. Test it. It probably won't be perfect, and that's fine. You'll learn more from what fails than what succeeds.
Once you're comfortable with the basics, move to decision trees or random forests. But don't rush it. Understanding why your first model works or doesn't work is worth way more than jumping to fancy algorithms you don't fully grasp. That's where the real learning happens.
Learn the math behind the simplest supervised learning model. Spoiler: it's just fitting a line.
A step-by-step walkthrough of how decision trees split data into buy/sell decisions.
Your model performs perfectly on historical data but tanks on live markets? That's overfitting.