From: Nils Forssén Date: Mon, 26 Sep 2022 15:13:49 +0000 (+0200) Subject: pushing X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;p=TSRT04.git pushing --- diff --git a/Ecology/pngs/gunilla.fig b/Ecology/pngs/gunilla.fig index ffe2494..c7dc2a3 100644 Binary files a/Ecology/pngs/gunilla.fig and b/Ecology/pngs/gunilla.fig differ diff --git a/TSRT04_2022_nilfo359_amafa469.txt b/TSRT04_2022_nilfo359_amafa469.txt new file mode 100644 index 0000000..e71fc24 --- /dev/null +++ b/TSRT04_2022_nilfo359_amafa469.txt @@ -0,0 +1,113 @@ +function [dice_results] = dice_sim(num_of_dice) +%dice_sim Simulates ´num_of_dice´ number of rolls +% OUTPUT: ´dice_results´ 1-dimensional vector of all rolls + dice_results = randi(6, 1, num_of_dice); +end + + +function [sum_vec] = organize_dice(dice_results) +%organize_dice Organizes ´dice_results´ by their value +% OUTPUT: ´sum_vec´ 1-dimensional summary vector, dice index corresponds +% to dice value + sum_vec = zeros(1, 6); + for num=1:6 + for i=1:length(dice_results) + if dice_results(i) == num + sum_vec(num) = sum_vec(num) + 1; + end + end + end +end + + +function [count] = toss_til_yahtzee(dice_total) +%toss_til_yahtzee Performs dice rolls with ´dice_total´ number of dice until yahtzee +% OUTPUT: ´count´ number of rolls until yahtzee + count = 1; + % Initial roll + results = dice_sim(dice_total); + [main_n, main_most_common] = max(organize_dice(results)); + + while main_n < dice_total + + % Continue rolling until all dice equal + results = dice_sim(dice_total - main_n); + sigma = organize_dice(results); + [n, most_common] = max(sigma); + + % If rolled dice contain more matching values than saved dice, + % switch. + if n > main_n + main_most_common = most_common; + main_n = n; + else + main_n = main_n + sigma(main_most_common); + end + count = count + 1; + end +end + + +function [mhat, s2hat] = yatzy(experiments) +%yatzy Rolls dice until yahtzee ´experiments´ number of times, plots the data +% and calculates statistical properties of the experiment. +% OUTPUT: ´mhat´ average number of rolls til yahtzee +% OUTPUT: ´s2hat´ variance in dice rolls required for yahtzee + dice_total = 5; + results = zeros(experiments, 1); + + % Do experiment + for i=1:experiments + results(i) = toss_til_yahtzee(dice_total); + if mod(i, 1000) == 0 + % Status update + fprintf('%d%% Done\n',i/(experiments/100)); + end + end + + % Matrices for statistics calculations. + A = [ + 0 1/6 1/36 1/216 1/1296 + 0 5/6 10/36 15/216 25/1296 + 0 0 25/36 80/216 250/1296 + 0 0 0 120/216 900/1296 + 0 0 0 0 120/1296 + ]; + e_1 = [1;0;0;0;0]; + e_5 = [0;0;0;0;1]; + + % Statistics y values for plotting. Only 50 up to 50 total rolls + % plotted. + chance = zeros(50,1); + for i=1:50 + chance(i, 1) = e_1.' * A^i * e_5; + end + + % Clear figure and plot data, histogram for experiment and dashed line + % for theoretical statistics + clf; + histogram(results); + title("Number of dice toss to get yahtzee!") + ylabel("Number of attempts") + xlabel("Rolls") + hold on; + yyaxis right; + ylabel("Theoretical percentage") + plot([1:50], chance, linewidth=3, LineStyle='--'); + legend("Number of attempts", "Theoretical percentage"); + + % Statistics calculations + mean_ = mean(results); + variance = sum((results - mean(results)).^2)/(length(results) - 1); + fprintf('Mean: %d\n',mean_); + fprintf('Variance: %d\n',variance); + mhat = mean_; + s2hat = variance; +end + + + +% ------------------EXAMPLE------------------- +% Run the experiment by running the main function yatzy(experiments), all other functions should be accessible to yatzy(experiments) +% >> +[mhat, s2hat] = yatzy(10000) \ No newline at end of file diff --git a/Yatzee/dice_sim.m b/Yatzee/dice_sim.m index 65af3c6..819326e 100644 --- a/Yatzee/dice_sim.m +++ b/Yatzee/dice_sim.m @@ -1,4 +1,5 @@ function [dice_results] = dice_sim(num_of_dice) -%dice_sim Simulates ´num of dice´ many dice rolls and returns results vector -dice_results = randi(6, 1, num_of_dice); +%dice_sim Simulates ´num_of_dice´ number of rolls +% OUTPUT: ´dice_results´ 1-dimensional vector of all rolls + dice_results = randi(6, 1, num_of_dice); end \ No newline at end of file diff --git a/Yatzee/hist_plot.m b/Yatzee/hist_plot.m deleted file mode 100644 index aa9f5b0..0000000 --- a/Yatzee/hist_plot.m +++ /dev/null @@ -1 +0,0 @@ -histogram(dice_results, 6) \ No newline at end of file diff --git a/Yatzee/main.asv b/Yatzee/main.asv deleted file mode 100644 index 91b9529..0000000 --- a/Yatzee/main.asv +++ /dev/null @@ -1,30 +0,0 @@ -clear; -clc; - -% Roll x dice -count = 1; -results = dice_sim(5); -[main_n, main_most_common] = max(sum_vec(results)); -main_most_common -results -main_n -while count < 5 - results = dice_sim(5 - main_n); - % determine most common dice - - [n, most_common] = max(sum_vec(results)); - if n > main_n - main_most_common = most_common; - main_n = n; - else - if most_common - main_n = main - - end - main_most_common - results - main_n - % reroll other dice - % repeat until all dice are unified - count = count + 1; -end diff --git a/Yatzee/main.m b/Yatzee/main.m deleted file mode 100644 index bd00a1a..0000000 --- a/Yatzee/main.m +++ /dev/null @@ -1,23 +0,0 @@ -clear; -clc; -dice = 10; -% Roll x dice -count = 1; -results = dice_sim(dice); -[main_n, main_most_common] = max(sum_vec(results)); -while main_n < dice - results = dice_sim(dice - main_n); - % determine most common dice - sigma = sum_vec(results); - [n, most_common] = max(sigma); - if n > main_n - main_most_common = most_common; - main_n = n; - else - main_n = main_n + sigma(main_most_common); - end - % reroll other dice - % repeat until all dice are unified - count = count + 1; -end -count \ No newline at end of file diff --git a/Yatzee/monte_carlo.m b/Yatzee/monte_carlo.m new file mode 100644 index 0000000..e69de29 diff --git a/Yatzee/organize_dice.m b/Yatzee/organize_dice.m new file mode 100644 index 0000000..a788c9f --- /dev/null +++ b/Yatzee/organize_dice.m @@ -0,0 +1,13 @@ +function [sum_vec] = organize_dice(dice_results) +%organize_dice Organizes ´dice_results´ by their value +% OUTPUT: ´sum_vec´ 1-dimensional summary vector, dice index corresponds +% to dice value + sum_vec = zeros(1, 6); + for num=1:6 + for i=1:length(dice_results) + if dice_results(i) == num + sum_vec(num) = sum_vec(num) + 1; + end + end + end +end \ No newline at end of file diff --git a/Yatzee/sum_vec.m b/Yatzee/sum_vec.m deleted file mode 100644 index af727f5..0000000 --- a/Yatzee/sum_vec.m +++ /dev/null @@ -1,11 +0,0 @@ -function [sum_vec] = sum_vec(dice_results) -%UNTITLED5 Summary of this function goes here -% Detailed explanation goes here -sum_vec = zeros(1, 6); -for num=1:6 - for i=1:length(dice_results) - if dice_results(i) == num - sum_vec(num) = sum_vec(num) + 1; - end - end -end \ No newline at end of file diff --git a/Yatzee/toss_til_yahtzee.m b/Yatzee/toss_til_yahtzee.m new file mode 100644 index 0000000..aa0643d --- /dev/null +++ b/Yatzee/toss_til_yahtzee.m @@ -0,0 +1,26 @@ +function [count] = toss_til_yahtzee(dice_total) +%toss_til_yahtzee Performs dice rolls with ´dice_total´ number of dice until yahtzee +% OUTPUT: ´count´ number of rolls until yahtzee + count = 1; + % Initial roll + results = dice_sim(dice_total); + [main_n, main_most_common] = max(organize_dice(results)); + + while main_n < dice_total + + % Continue rolling until all dice equal + results = dice_sim(dice_total - main_n); + sigma = organize_dice(results); + [n, most_common] = max(sigma); + + % If rolled dice contain more matching values than saved dice, + % switch. + if n > main_n + main_most_common = most_common; + main_n = n; + else + main_n = main_n + sigma(main_most_common); + end + count = count + 1; + end +end \ No newline at end of file diff --git a/Yatzee/yatzy.m b/Yatzee/yatzy.m new file mode 100644 index 0000000..c2cef5c --- /dev/null +++ b/Yatzee/yatzy.m @@ -0,0 +1,56 @@ +function [mhat, s2hat] = yatzy(experiments) +%yatzy Rolls dice until yahtzee ´experiments´ number of times, plots the data +% and calculates statistical properties of the experiment. +% OUTPUT: ´mhat´ average number of rolls til yahtzee +% OUTPUT: ´s2hat´ variance in dice rolls required for yahtzee + dice_total = 5; + results = zeros(experiments, 1); + + % Do experiment + for i=1:experiments + results(i) = toss_til_yahtzee(dice_total); + if mod(i, 1000) == 0 + % Status update + fprintf('%d%% Done\n',i/(experiments/100)); + end + end + + % Matrices for statistics calculations. + A = [ + 0 1/6 1/36 1/216 1/1296 + 0 5/6 10/36 15/216 25/1296 + 0 0 25/36 80/216 250/1296 + 0 0 0 120/216 900/1296 + 0 0 0 0 120/1296 + ]; + e_1 = [1;0;0;0;0]; + e_5 = [0;0;0;0;1]; + + % Statistics y values for plotting. Only 50 up to 50 total rolls + % plotted. + chance = zeros(50,1); + for i=1:50 + chance(i, 1) = e_1.' * A^i * e_5; + end + + % Clear figure and plot data, histogram for experiment and dashed line + % for theoretical statistics + clf; + histogram(results); + title("Number of dice toss to get yahtzee!") + ylabel("Number of attempts") + xlabel("Rolls") + hold on; + yyaxis right; + ylabel("Theoretical percentage") + plot([1:50], chance, linewidth=3, LineStyle='--'); + legend("Number of attempts", "Theoretical percentage"); + + % Statistics calculations + mean_ = mean(results); + variance = sum((results - mean(results)).^2)/(length(results) - 1); + fprintf('Mean: %d\n',mean_); + fprintf('Variance: %d\n',variance); + mhat = mean_; + s2hat = variance; +end \ No newline at end of file