close all
clear all
% compute wins and losses from superbowl bets
%possible wagers
poss(1,:)=[105,210,105,210,0];
poss(2,:)=[105,105,210,210,0];
poss(3,:)=[105,105,210,210,0];
poss(4,:)=[200,150,0,0,0];
poss(5,:)=[140,100,280,200,0];
poss(6,:)=[105,125,210,250,0];
poss(7,:)=[130,110,260,220,0];
poss(8,:)=[150,170,300,340,0];
poss(9,:)=[150,130,300,260,0];
poss(10,:)=[72,45,15,10,0];
%winning bets 100 or 200, losing bets -100 or -200; 0- no bet
% ne wins
win(1,:)= [100,200,-100,-100,0];
%pts over
win(2,:)= [100,-100,200,-200,0];
%tails
win(3,:)= [-100,100,-200,200,0];
%pants jeans
win(4,:)= [100,-100,0,0,0];
%gaga no trump
win(5,:)= [-100,100,-200,200,0];
%first penalty atl
win(6,:)= [-100,100,-200,200,0];
%first score atl
win(7,:)= [-100,100,-200,200,0];
%brady inter yes
win(8,:)= [100,-100,200,-200,0];
%fg both
win(9,:)= [-100,100,-200,200,0];
%mvp tom brady
win(10,:)= [100,-100,200,-200,0];

bid = fopen('superbowl_bets_2017.csv');
bets_in = textscan(bid,...
    '%s %d %d %d %d %d %d %d %d %d %d','Delimiter',',');
names = char(bets_in{1});
no_bets = size(names,1);

%cycle over the 10 bets
pay = zeros(10,no_bets);
loss = zeros(10,no_bets);
fees = zeros(no_bets);
for w = 1:10
    bets(w,:) = bets_in{w+1};
    wagers(w,:)= sign(win(w,bets(w,:))).*poss(w,bets(w,:));
    %cycle thru everyone
    for n=1:no_bets
        if(win(w,bets(w,n))>=100)
            %winning bet
            pay(w,n)=win(w,bets(w,n));
            fees(n)=fees(n)+5;
        elseif(win(w,bets(w,n))<=-100)
            %losing bet
            loss(w,n)=poss(w,bets(w,n));
            fees(n)=fees(n)+5;
        end
    end
end
for n = 1:no_bets
    %total wagers
    total_bets(n)= sum(abs(wagers(:,n)));
    % limit to max of 1000
    if(total_bets(n)>1000)
        net(n) = 950;
        prof(n) = 50;
    else
    %net for bettors 
    net(n) = 1000 - fees(n) - sum(loss(:,n)) + sum(pay(:,n));
    %net for bookie
    prof(n) = 1000 - net(n);
    end
end

figure(1)
x = 0:100:1100;
hist(total_bets,x)
title('Total Amount Wagered')
xlabel('$');
ylabel('Count')
axis('tight')
figure(2)
x = -1000:100:3000;
hist(net,x)
title('Bettors Final')
xlabel('$');
ylabel('Count')
axis('tight')
figure(3)
x = -1000:100:3000;
hist(prof,x)
title('Bookies Profit')
xlabel('$');
ylabel('Count')
axis('tight')
figure(4)
bar(net)
set(gca,'Xtick',1:1:no_bets);
set(gca,'XTickLabel',names(1:1:no_bets,:))

titles=['WIN ';'PTS ';'COIN';'PANT';'GAGA';...
    'FPEN';'FSCO';'INT ';'FGS ';'MVP ']
%loop over bets
for w=1:10
    %loop over type of bets
    for cat=1:5
         no_win(cat)=0;
        if(isempty(find(bets(w,:)==cat)))
            no_cat(cat) = 0;
        else
            val = find(bets(w,:)==cat);
            no_cat(cat) = length(val);
            if(win(w,cat)>=100)
                no_win(cat)=no_cat(cat);
            end
        end
    end
    figure(w+10)
    x=1:5;
    %red are losing bets
    bar(x,no_cat,'r')
    hold on
    %blue are winning bets
    bar(x,no_win,'b')
    title(titles(w,:))
    xlabel('Bet Type');
    ylabel('Count')
    axis([1 5 0 no_bets]);
end
%summary stats
class_med = median(net)
class_mean = mean(net)
class_prof = sum(prof)
valn=find(net>1000);
%number of bets with net winning
total_net_pos=length(valn)



% now repeat for 10,000 random bettors
no_bets = 10000;
%cycle over the 10 bets
payr = zeros(10,no_bets);
lossr = zeros(10,no_bets);
feesr = zeros(no_bets);
for w = 1:10
    r = randi([1 5],1,no_bets);
    betsr(w,:) = r;
    wagersr(w,:)= sign(win(w,betsr(w,:))).*poss(w,betsr(w,:));
    for n=1:no_bets
        if(win(w,betsr(w,n))>=100)
            %winning bet
            payr(w,n)=win(w,betsr(w,n));
            feesr(n)=feesr(n)+5;
        elseif(win(w,betsr(w,n))<=-100)
            %losing bet
            lossr(w,n)=poss(w,betsr(w,n));
            feesr(n)=feesr(n)+5;
        end
    
    end
end

for n = 1:no_bets
    total_betsr(n)= sum(abs(wagersr(:,n)));
    %if(total_betsr(n)>1000)
    %    netr(n)= 950;
    %    profr(n) = 50;
    %else
        netr(n) = 1000 - feesr(n) - sum(lossr(:,n)) + sum(payr(:,n));
        profr(n) = 1000 - netr(n);
    %end
end


figure(101)
x = 0:100:2500;
hist(total_betsr,x)
title('Total Amount Wagered')
xlabel('$');
ylabel('Count')
axis('tight')
figure(102)
x = -1000:100:3000;
hist(netr,x)
title('Bettors Final')
xlabel('$');
ylabel('Count')
axis('tight')
figure(103)
x = -2000:100:3000;
hist(profr,x)
title('Profit')
xlabel('$');
ylabel('Count')
axis('tight')

for w=1:10
    for cat=1:5
        no_winr(cat)=0;
        if(isempty(find(betsr(w,:)==cat)))
            no_cat(cat) = 0;
        else
            val = find(betsr(w,:)==cat);
            no_cat(cat) = length(val);
            if(win(w,cat)>=100)
                no_winr(cat)=no_cat(cat);
            end
        end
    end
    figure(w+110)
    x=1:5;
    bar(x,no_cat,'r')
    hold on
    bar(x,no_winr,'b')

    title(titles(w,:));
    xlabel('Bet Type');
    ylabel('Count')
    axis([1 5 0 no_bets]);
    
end
ran_mean= mean(netr)
ran_med= median(netr)
ran_prof = sum(profr)/no_bets
valnr=find(netr>1000);
%fraction of bets with net winning
total_net_pos=length(valnr)/no_bets

