clear all;

% Number of possible outcomes
N = 7;

% Decimal odds
odds = NaN(N,1);
pi = NaN(N,1);

odds(1) = 0.8 / 0.025;
odds(2) = 0.8 / 0.0375;
odds(3) = 0.8 / 0.0625;
odds(4) = 0.8 / 0.125;
odds(5) = 0.8 / 0.25;
odds(6) = 0.8 / 0.3125;
odds(7) = 0.8 / 0.1875;

p = NaN(N,1);
for i = 1:N
    p(i) = 1/odds(i);
end

% Beliefs
pi(1) = .003247 ;
pi(2) = .003247 ;
pi(3) = .003247 ;
pi(4) = .01623 ;
pi(5) = .2273 ;
pi(6) = .1623 ;
pi(7) = .5844 ;


revenue_rate  = pi./ p ;

% Kelly criterion calculator

for i=1:N
    kelly(i) = max(0, (pi(i)-p(i)) / (1-p(i)) );
end 

[xopt, fopt] = maximize_function(pi,p,N);

disp("Optimal Fraction of Wealth to Bet")
disp((p.*xopt)')

disp("Kelly Criterion Fraction of Wealth to Bet")
disp(kelly)

disp("Revenue Rates")
disp((revenue_rate)')


function [xopt, fopt] = maximize_function(pi, p , N)
% Define the function to be minimized

fun = @(x)  sum( -pi.*log(1+x-sum(p.*x)) ) 
% Define initial points for simplex algorithm

x0 = NaN(N,1);

for i=1:N
    x0(i) = 0.5;
end 

% Set options for the optimization algorithm
opts = optimset('MaxIter', 1000, 'TolX', 1e-10);

A = [ ];
b = [ ];
Aeq = [ ];
beq = [ ];
nonlcon = [];
lb = zeros(N,1);
ub = [ ];

% Run the Nelder-Mead simplex algorithm
%[xopt, fopt, exitflag, output, lambda] = fmincon(fun, x0, A, b , Aeq, beq, lb, ub, nonlcon, opts);
%disp(lambda.lower)

[xopt, fopt] = fmincon(fun, x0, A, b , Aeq, beq, lb, ub, nonlcon, opts);
end

