clear;
close all;
clc;

% Set the seed for random number generation
rng(123);

%Specify the file path and name
filename= 'C:\Users\MatinRayane\Desktop\payanname\First year\Machine Learning_Programming\MachineLearning_FirstYear.xlsx';
%Specify the sheet name or index
sheet= 'First Scenario';
%Read the data from the second sheet of the Excel file
[num_data]= xlsread(filename, sheet);

normalized= @(x) (x - min(x)) / (max(x) - min(x));

%Extract independent variables (features)
X= num_data(:, 1:end-1); %Select all rows and all columns except the last one

% Normalize each column of X using arrayfun
X_normalized = arrayfun(@(col) normalized(X(:, col)), 1:size(X, 2), 'UniformOutput', false);

% Convert X_normalized from cell array to matrix
X_normalize = cell2mat(X_normalized);

%Extract dependent variable (lable)
y= num_data(:, end); %Select all rows and the last column

y_normalize = normalized(y);

%Set the size of each subset
train_ratio= 0.8;
test_ratio= 0.2;

%Calculate the number of data points in each subset
num_samples= size(X_normalize,1);
num_train= round(train_ratio * num_samples);
num_test= num_samples - num_train;

%Generate random indices for the data points
idx= randperm(num_samples);

%Split the indices into train, cross_validation and test sets
train_idx= idx(1:num_train);
test_idx= idx(num_train+1:end);

%Use the indices to extract the corresponding rows from the data matrix
X_train= X_normalize(train_idx, :);
X_test= X_normalize(test_idx, :);

%Use the same indices for y as for X
y_train= y_normalize(train_idx, :);
y_test= y_normalize(test_idx, :);

%%CV Partition
c= cvpartition(num_train, "KFold", 10);

% Define parameter grid
%param_grid = struct('BoxConstraint', [0.1, 1, 10, 100],'KernelScale', [1, 0.1, 0.01, 0.001, 0.0001],'KernelFunction', {'rbf'});

rng(1234)
% Perform grid search and cross-validation
Md1 = fitrsvm(X_train, y_train, 'kernelFunction', 'rbf', 'OptimizeHyperparameters', 'auto', ...
    'HyperparameterOptimizationOptions', struct('AcquisitionFunctionName', ...
    'expected-improvement-plus', 'Showplots', true, 'MaxObjectiveEvaluations', 30, 'CVPartition', c));

y_pred_train= predict(Md1, X_train);
MSE_train= mean((y_pred_train - y_train).^2);

% Evaluate the model using cross-validation to estimate MSE for training data
%cv_MSE_train = crossval('mse', X_train_w_best_features, y_train, 'Predfun', @(Xtr, ytr, Xte) predict(Md1, Xte));

% Get the best model
bestModel = Md1;

% Use the trained model to predict on the test set
y_pred_test = predict(bestModel, X_test);

% Evaluate the model performance
MSE_test = mean((y_pred_test - y_test).^2); % Mean Squared Error
RMSE_test = sqrt(MSE_test); % Root Mean Squared Error
NRMSE_test = sqrt(MSE_test) / std(y_test); % Normalized Root Mean Squared Error
MAE_test = mean(abs(y_pred_test - y_test)); % Mean Absolute Error
r_squared_test = 1 - (sum((y_test - y_pred_test).^2) / sum((y_test - mean(y_test)).^2)); % R^2

% Calculate Index of agreement
y_mean = mean(y_test);
numerator = sum((y_test - y_pred_test).^2);
denominator = sum((abs(y_test - y_mean) + abs(y_pred_test - y_mean)).^2);
d_test = 1 - (numerator / denominator); %d

% Display the results
fprintf('Train MSE: %.4f\n', MSE_train);
fprintf('Test MSE: %.4f\n', MSE_test);
fprintf('Test RMSE: %.4f\n', RMSE_test);
fprintf('Test NRMSE: %.4f\n', NRMSE_test);
fprintf('Test MAE: %.4f\n', MAE_test);
fprintf('Test R^2: %.4f\n', r_squared_test);
fprintf('Test d: %4f\n', d_test);
%%hyperplane
figure;
scatter(y_test, y_pred_test, 'filled', 'MarkerFaceColor', 'b');
hold on;
xlabel('Actual Values');
ylabel('Predicted Values');
title('Actual vs. Predicted Values');
grid on;
axis equal;

% Plot the line of perfect prediction (y = x)
hold on;
plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], '--r');

% Add legend
legend('Predictions', 'Perfect Prediction Line', 'Location', 'northwest');

% Show the plot
hold off;


