1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> #include <vector> #include <numeric> #include <cmath> // Function to calculate the mean of a vector double mean(const std::vector<double>& data) { return std::accumulate(data.begin(), data.end(), 0.0) / data.size(); } // Function to calculate the slope (m) and intercept (b) of the linear regression line void linearRegression(const std::vector<double>& x, const std::vector<double>& y, double& slope, double& intercept) { size_t n = x.size(); double x_mean = mean(x); double y_mean = mean(y); double numerator = 0.0; double denominator = 0.0; for (size_t i = 0; i < n; ++i) { numerator += (x[i] - x_mean) * (y[i] - y_mean); denominator += (x[i] - x_mean) * (x[i] - x_mean); } slope = numerator / denominator; intercept = y_mean - slope * x_mean; } // Function to predict a value based on the linear regression model double predict(double x, double slope, double intercept) { return slope * x + intercept; } int main() { // Example data: x values (e.g., months) and y values (e.g., sales) std::vector<double> x = {1, 2, 3, 4, 5}; // Example x values std::vector<double> y = {2, 3, 5, 7, 11}; // Example y values (corresponding to x values) double slope, intercept; linearRegression(x, y, slope, intercept); std::cout << "Linear Regression Model:\n"; std::cout << "Slope (m): " << slope << std::endl; std::cout << "Intercept (b): " << intercept << std::endl; double x_predict; std::cout << "Enter a value for x to predict its corresponding y: "; std::cin >> x_predict; double y_predict = predict(x_predict, slope, intercept); std::cout << "Predicted y for x = " << x_predict << " is " << y_predict << std::endl; return 0; } |
Explanation
- Function
mean(const std::vector<double>& data)
:- Purpose: Computes the mean of a dataset.
- Implementation: Uses
std::accumulate
to sum the data and divides by the number of elements.
Advertisement - Function
linearRegression(const std::vector<double>& x, const std::vector<double>& y, double& slope, double& intercept)
:- Purpose: Calculates the slope and intercept of the linear regression line.
- Parameters:
x
: Vector of independent variable values.y
: Vector of dependent variable values.slope
: Reference to store the calculated slope of the line.intercept
: Reference to store the calculated intercept of the line.
- Implementation:
- Computes the mean of
x
andy
. - Calculates the numerator and denominator for the slope formula.
- Computes the slope and intercept of the regression line.
- Computes the mean of
- Function
predict(double x, double slope, double intercept)
:- Purpose: Predicts the value of
y
for a givenx
based on the linear regression model. - Implementation: Applies the linear regression formula y=mx+by = mx + b.
- Purpose: Predicts the value of
- Main Function:
- Setup: Initializes example data for
x
andy
values. - Calculation:
- Calls
linearRegression
to compute the model parameters. - Displays the slope and intercept.
- Calls
- Prediction:
- Prompts the user for a value of
x
Advertisement - Uses
predict
to estimate the correspondingy
value and displays the result.
- Prompts the user for a value of
- Setup: Initializes example data for
Usage
- Predictive Analytics: Demonstrates a basic predictive analytics approach using linear regression.
- Forecasting: Allows users to input future values and see predictions based on past data trends.