Advertisement
Kaawumba

win_loss_analysis.m

Oct 21st, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.09 KB | None | 0 0
  1. function win_loss_analysis(rank_at_game, label, win_rate_average_half_length)
  2.  
  3. if nargin < 3
  4.     win_rate_average_half_length = 20;
  5. end
  6.  
  7. n_games = length(rank_at_game);
  8. result_at_game = zeros(1,n_games);
  9. streaks = zeros(1,n_games);
  10. streak_count = 1;
  11. last_result = true;
  12.  
  13. %Get rank vs game, streak counts
  14. for n=2:length(rank_at_game)
  15.     %ignore draws
  16.     if rank_at_game(n) == rank_at_game(n-1)
  17.         result_at_game(n-1) = 0;
  18.         continue;
  19.     end
  20.     if rank_at_game(n) > rank_at_game(n-1)
  21.         win = true;
  22.         result_at_game(n-1) = 1;
  23.     else
  24.         win = false;
  25.         result_at_game(n-1) = -1;
  26.     end
  27.    
  28.     if (win == last_result)
  29.         streak_count = streak_count+1;
  30.     else
  31.         streaks(streak_count) = streaks(streak_count)+1;
  32.         streak_count = 1;      
  33.     end
  34.     last_result = win;
  35. end
  36.  
  37. figure
  38. plot(rank_at_game);
  39. xlabel('Game Number');
  40. ylabel('Skill Rating');
  41. title(strcat("Skill Rating vs Game, ",label));
  42. figure
  43. errorbar(streaks/n_games, sqrt(streaks)/n_games);
  44. xlabel('Streak Length');
  45. ylabel('Number of Streaks / Number of Games');
  46. title(strcat("Streak Frequency, ", label));
  47. axis([0 25 0 0.5])
  48.  
  49. %get win probability by rank, sort by SR, then do a smoothed average, then
  50. %fit
  51. [sorted_rank_at_game, indices] = sort(rank_at_game);
  52. q= result_at_game';
  53. sorted_result_at_game = q(indices);
  54.  
  55. win_rates = zeros(1, n_games - 2*win_rate_average_half_length);
  56. win_ranks = zeros(1, n_games - 2*win_rate_average_half_length);
  57. win_rate_index = 1;
  58. for i = win_rate_average_half_length+1:n_games-win_rate_average_half_length
  59.    
  60.     win_tot = 0;
  61.     for j = i-win_rate_average_half_length : i+win_rate_average_half_length
  62.         win_tot = win_tot + (sorted_result_at_game(j)+1)/2.0;
  63.     end
  64.     win_rates(win_rate_index) = win_tot/(2*win_rate_average_half_length + 1); % 0 to 100 percent
  65.     win_ranks(win_rate_index) = sorted_rank_at_game(i);
  66.     win_rate_index = win_rate_index + 1;
  67. end
  68.  
  69. ft = fittype( '1-.5*(1+erf((x-a)/(b*sqrt(2))))', 'independent', 'x', 'dependent', 'y' );
  70. opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
  71. opts.Display = 'Off';
  72. opts.Lower = [0 1];
  73. opts.StartPoint = [2500 400];
  74. opts.Upper = [5000 5000];
  75.  
  76. [fitresult, gof] = fit( win_ranks', win_rates', ft, opts );
  77. coeffs = coeffvalues(fitresult);
  78. mu = coeffs(1);
  79. sigma = coeffs(2);
  80. pd = makedist('Normal', mu, sigma);
  81.  
  82. for i=1:5000
  83.         dist(i) = 1 - cdf(pd, i);
  84. end
  85.  
  86. % Plot fit with data.
  87. figure
  88. hold all
  89. plot( win_ranks', win_rates', 'DisplayName', 'Data' );
  90. plot(dist, 'DisplayName', 'Fit');
  91. legend( '-DynamicLegend' );
  92. xlabel('Skill Rating');
  93. ylabel('Win Probability');
  94. axis([0 5000 0 1]);
  95. title(strcat("Win Probability vs Skill Rating, ", label));
  96. newline = sprintf('\n');
  97. txt = ['mu = ', num2str(mu,4), newline, 'sigma = ', num2str(sigma,3)];
  98. text(500, .1, txt);
  99.  
  100. %auto correlation function calculation
  101. ac_length = 10;
  102. ac_count = zeros(1, ac_length*2+1);
  103. ac_error = zeros(1, ac_length*2+1);
  104. ac_func = zeros(1, ac_length*2+1);
  105. ac_func_sq = zeros(1, ac_length*2+1);
  106. for n = 1:n_games
  107.     for m = n-ac_length:n+ac_length
  108.         if m > 0 && m <= n_games  
  109.             if(result_at_game(n) == 0)
  110.                 continue;
  111.             end
  112.             ac_count(m - n +ac_length+1) = ac_count(m - n +ac_length+1) + 1;
  113.             if(result_at_game(n) == 1)
  114.                 ac_func(m - n + ac_length + 1) = ac_func(m-n+ac_length+1) + result_at_game(m);
  115.             else
  116.                 ac_func(m - n + ac_length + 1) = ac_func(m-n+ac_length+1) - result_at_game(m);
  117.             end
  118.             ac_func_sq(m-n+ac_length+1) =  ac_func_sq(m-n+ac_length+1) + result_at_game(m)*result_at_game(m);
  119.            
  120.         end
  121.     end
  122. end
  123.  
  124. for n = 1:ac_length*2+1
  125.    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));
  126.    ac_func(n) = ac_func(n) / ac_count(n);
  127. end
  128.  
  129. figure
  130. ac_func_x = -ac_length:ac_length;
  131. errorbar(ac_func_x, ac_func, ac_error);
  132. xlabel('Game Differential');
  133. ylabel('Auto Correlation');
  134. title(strcat("Win/Loss Auto-correlation function, ", label));
  135.  
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement