Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function win_loss_analysis(rank_at_game, label, win_rate_average_half_length)
- if nargin < 3
- win_rate_average_half_length = 20;
- end
- n_games = length(rank_at_game);
- result_at_game = zeros(1,n_games);
- streaks = zeros(1,n_games);
- streak_count = 1;
- last_result = true;
- %Get rank vs game, streak counts
- for n=2:length(rank_at_game)
- %ignore draws
- if rank_at_game(n) == rank_at_game(n-1)
- result_at_game(n-1) = 0;
- continue;
- end
- if rank_at_game(n) > rank_at_game(n-1)
- win = true;
- result_at_game(n-1) = 1;
- else
- win = false;
- result_at_game(n-1) = -1;
- end
- if (win == last_result)
- streak_count = streak_count+1;
- else
- streaks(streak_count) = streaks(streak_count)+1;
- streak_count = 1;
- end
- last_result = win;
- end
- figure
- plot(rank_at_game);
- xlabel('Game Number');
- ylabel('Skill Rating');
- title(strcat("Skill Rating vs Game, ",label));
- figure
- errorbar(streaks/n_games, sqrt(streaks)/n_games);
- xlabel('Streak Length');
- ylabel('Number of Streaks / Number of Games');
- title(strcat("Streak Frequency, ", label));
- axis([0 25 0 0.5])
- %get win probability by rank, sort by SR, then do a smoothed average, then
- %fit
- [sorted_rank_at_game, indices] = sort(rank_at_game);
- q= result_at_game';
- sorted_result_at_game = q(indices);
- win_rates = zeros(1, n_games - 2*win_rate_average_half_length);
- win_ranks = zeros(1, n_games - 2*win_rate_average_half_length);
- win_rate_index = 1;
- for i = win_rate_average_half_length+1:n_games-win_rate_average_half_length
- win_tot = 0;
- for j = i-win_rate_average_half_length : i+win_rate_average_half_length
- win_tot = win_tot + (sorted_result_at_game(j)+1)/2.0;
- end
- win_rates(win_rate_index) = win_tot/(2*win_rate_average_half_length + 1); % 0 to 100 percent
- win_ranks(win_rate_index) = sorted_rank_at_game(i);
- win_rate_index = win_rate_index + 1;
- end
- ft = fittype( '1-.5*(1+erf((x-a)/(b*sqrt(2))))', 'independent', 'x', 'dependent', 'y' );
- opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
- opts.Display = 'Off';
- opts.Lower = [0 1];
- opts.StartPoint = [2500 400];
- opts.Upper = [5000 5000];
- [fitresult, gof] = fit( win_ranks', win_rates', ft, opts );
- coeffs = coeffvalues(fitresult);
- mu = coeffs(1);
- sigma = coeffs(2);
- pd = makedist('Normal', mu, sigma);
- for i=1:5000
- dist(i) = 1 - cdf(pd, i);
- end
- % Plot fit with data.
- figure
- hold all
- plot( win_ranks', win_rates', 'DisplayName', 'Data' );
- plot(dist, 'DisplayName', 'Fit');
- legend( '-DynamicLegend' );
- xlabel('Skill Rating');
- ylabel('Win Probability');
- axis([0 5000 0 1]);
- title(strcat("Win Probability vs Skill Rating, ", label));
- newline = sprintf('\n');
- txt = ['mu = ', num2str(mu,4), newline, 'sigma = ', num2str(sigma,3)];
- text(500, .1, txt);
- %auto correlation function calculation
- ac_length = 10;
- ac_count = zeros(1, ac_length*2+1);
- ac_error = zeros(1, ac_length*2+1);
- ac_func = zeros(1, ac_length*2+1);
- ac_func_sq = zeros(1, ac_length*2+1);
- for n = 1:n_games
- for m = n-ac_length:n+ac_length
- if m > 0 && m <= n_games
- if(result_at_game(n) == 0)
- continue;
- end
- ac_count(m - n +ac_length+1) = ac_count(m - n +ac_length+1) + 1;
- if(result_at_game(n) == 1)
- ac_func(m - n + ac_length + 1) = ac_func(m-n+ac_length+1) + result_at_game(m);
- else
- ac_func(m - n + ac_length + 1) = ac_func(m-n+ac_length+1) - result_at_game(m);
- end
- ac_func_sq(m-n+ac_length+1) = ac_func_sq(m-n+ac_length+1) + result_at_game(m)*result_at_game(m);
- end
- end
- end
- for n = 1:ac_length*2+1
- ac_error(n) = sqrt(ac_func_sq(n) / (ac_count(n) - 1) - ac_func(n)*ac_func(n)/(ac_count(n) * (ac_count(n)-1))) / sqrt(ac_count(n));
- ac_func(n) = ac_func(n) / ac_count(n);
- end
- figure
- ac_func_x = -ac_length:ac_length;
- errorbar(ac_func_x, ac_func, ac_error);
- xlabel('Game Differential');
- ylabel('Auto Correlation');
- title(strcat("Win/Loss Auto-correlation function, ", label));
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement