My model gives the same prediction value when I enter very differing input values
3 views (last 30 days)
Show older comments
When I enter my code and change the values between the min and max amounts the prediction amount that is given is the same, how do I change my code so that it gives a proper prediction for the stock price crash risk of shares.
%% Logistic Regression
%
% Instructions
% ------------
%
% This file contains code that helps you get started on the logistic
% regression exercise. You will need to complete the following functions
% in this exericse:
%
% sigmoid.m
% costFunction.m
% predict.m
%
% For this exercise, you will not need to change any code in this file,
% or any other files other than those mentioned above.
%
%% Initialization
clear all; close all; clc
%% Load Data
% The first 4 columns contains the crash risk variables and the five column
% contains the label.
data = load('All variables.txt');
X = data(:, [1, 2]); y = data(:, 3);
%% ==================== Part 1: Plotting ====================
% We start the exercise by first plotting the data to understand the
% the problem we are working with.
fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
'indicating (y = 0) examples.\n']);
plotData(X, y);
% Put some labels
hold on;
% Labels and Legend
xlabel('Crash Risk factors combined')
ylabel('Crash Risk')
% Specified in plot order
legend('Crashed', 'Not Crashed')
hold off;
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ============ Part 2: Compute Cost and Gradient ============
% In this part of the exercise, you will implement the cost and gradient
% for logistic regression. You neeed to complete the code in
% costFunction.m
% Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);
% Add intercept term to x and X_test
X = [ones(m, 1) X];
% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);
% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);
fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ============= Part 3: Optimizing using fminunc =============
% In this exercise, you will use a built-in function (fminunc) to find the
% optimal parameters theta.
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);
% Plot Boundary
plotDecisionBoundary(theta, X, y);
% Put some labels
hold on;
% Labels and Legend
xlabel('Crash Risk Factors')
ylabel('Crash Risk')
% Specified in plot order
legend('Crashed', 'Not Crashed')
hold off;
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
%% ============== Part 4: Predict and Accuracies ==============
% After learning the parameters, you'll like to use it to predict the outcomes
% on unseen data. In this part, you will use the logistic regression model
% to predict the probability that a stock will have a stock price crash
% risk
%
% Furthermore, you will compute the training and test set accuracies of
% our model.
%
% Your task is to complete the code in predict.m
% Predict probability for a score of 13,12,0.02,0.02 and 0.08
prob = sigmoid([1 45 85] * theta);
fprintf(['For a firm with scores 13.3, 12.9, 0.025, 0.024 and 0.08, we predict a crash ' ...
'probability of %f\n\n'], prob);
% Compute accuracy on our training set
p = predict(theta, X);
fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
2 Comments
Matt J
on 5 Jan 2024
You have a number of previous posts that have seemingly been abandoned.
Please go back and address the Answers you were given, so that we know you are not a bot, either by Accept-clicking those answers or replying to the poster why they are insufficient.
Answers (1)
Gagan Agarwal
on 14 Jan 2024
Hi Danny
Upon reviewing your code, I noticed an issue with the data loading process for prediction. Although you are currently loading data from a file named 'All variables.txt' and assigning the first two columns to X and the third column to y, your comments indicate that the first four columns should contain crash risk variables and the fifth column should contain the label. It's crucial to update the data loading section to align with this requirement.
Additionally, if the range of the input features varies widely, leveraging the feature scaling technique can significantly enhance accuracy. This approach also ensures that the optimization algorithm converges more smoothly.
I hope it helps!
0 Comments
See Also
Categories
Find more on Financial Data Analytics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!