pushing main
authorNils Forssén <forssennils@gmail.com>
Mon, 26 Sep 2022 15:13:49 +0000 (17:13 +0200)
committerNils Forssén <forssennils@gmail.com>
Mon, 26 Sep 2022 15:13:49 +0000 (17:13 +0200)
Ecology/pngs/gunilla.fig
TSRT04_2022_nilfo359_amafa469.txt [new file with mode: 0644]
Yatzee/dice_sim.m
Yatzee/hist_plot.m [deleted file]
Yatzee/main.asv [deleted file]
Yatzee/main.m [deleted file]
Yatzee/monte_carlo.m [new file with mode: 0644]
Yatzee/organize_dice.m [new file with mode: 0644]
Yatzee/sum_vec.m [deleted file]
Yatzee/toss_til_yahtzee.m [new file with mode: 0644]
Yatzee/yatzy.m [new file with mode: 0644]

index ffe2494728c82dad4de87272071209d0adb762f3..c7dc2a3e7e16874a3d5675f883ab6aa4cb3dbc9a 100644 (file)
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 (file)
index 0000000..e71fc24
--- /dev/null
@@ -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
index 65af3c6a76b8e7a5050394a5a7802d8bceff836d..819326e90c0d87dcb5130526cc248a9b30399403 100644 (file)
@@ -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 (file)
index aa9f5b0..0000000
+++ /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 (file)
index 91b9529..0000000
+++ /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 (file)
index bd00a1a..0000000
+++ /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 (file)
index 0000000..e69de29
diff --git a/Yatzee/organize_dice.m b/Yatzee/organize_dice.m
new file mode 100644 (file)
index 0000000..a788c9f
--- /dev/null
@@ -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 (file)
index af727f5..0000000
+++ /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 (file)
index 0000000..aa0643d
--- /dev/null
@@ -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 (file)
index 0000000..c2cef5c
--- /dev/null
@@ -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