Pólya urn flip and rollRoll Dungeons and Dragons diceBuild a program to analyze coin flip sequence choicesSimple single-player board game, expected score distributionSplit, flip and recombine integersYour Base to 1-2-3-Tribonacci to Binary back to Your BaseFlip a coin for me!Flip rows and columnsWhen Fibonacci meets the QueensBack to the Basics of MathSolve a matrix equation by Jacobi's method (Revised)
Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible
How can I track script which gives me "command not found" right after the login?
A link redirect to http instead of https: how critical is it?
What should tie a collection of short-stories together?
Life insurance that covers only simultaneous/dual deaths
Official degrees of earth’s rotation per day
Define, (actually define) the "stability" and "energy" of a compound
Property of summation
Time travel from stationary position?
Professor being mistaken for a grad student
how to draw discrete time diagram in tikz
Unexpected result from ArcLength
How to explain that I do not want to visit a country due to personal safety concern?
how to write formula in word in latex
Gravity magic - How does it work?
A limit with limit zero everywhere must be zero somewhere
Could the Saturn V actually have launched astronauts around Venus?
Did Ender ever learn that he killed Stilson and/or Bonzo?
How to terminate ping <dest> &
(Calculus) Derivative Thinking Question
Why do Australian milk farmers need to protest supermarkets' milk price?
What approach do we need to follow for projects without a test environment?
Is it possible to upcast ritual spells?
Brexit - No Deal Rejection
Pólya urn flip and roll
Roll Dungeons and Dragons diceBuild a program to analyze coin flip sequence choicesSimple single-player board game, expected score distributionSplit, flip and recombine integersYour Base to 1-2-3-Tribonacci to Binary back to Your BaseFlip a coin for me!Flip rows and columnsWhen Fibonacci meets the QueensBack to the Basics of MathSolve a matrix equation by Jacobi's method (Revised)
$begingroup$
Problem statement
Pólya is playing about with his urn again and he wants you to help him calculate some probabilities.
In this urn experiment Pólya has an urn which initially contains 1 red and 1 blue bead.
For every iteration, he reaches in and retrieves a bead, then inspects the colour and places the bead back in the urn.
He then flips a fair coin, if the coin lands heads he will insert a fair 6 sided die roll amount of the same coloured bead into the urn, if it lands tails he will remove half the number of the same colored bead from the urn (Using integer division - so if the number of beads of the selected colour is odd he will remove (c-1)/2 where c is the number of beads of that colour)
Given an integer n ≥ 0 and a decimal r > 0, give the probability to 2 decimal places that the ratio between the colours of beads after n iterations is greater than or equal to r in the shortest number of bytes.
An example set of iterations:
Let (x, y) define the urn such that it contains x red beads and y blue beads.
Iteration Urn Ratio
0 (1,1) 1
1 (5,1) 5 //Red bead retrieved, coin flip heads, die roll 4
2 (5,1) 5 //Blue bead retrieved, coin flip tails
3 (3,1) 3 //Red bead retrieved, coin flip tails
4 (3,4) 1.333... //Blue bead retrieved, coin flip heads, die roll 3
As can be seen the Ratio r is always ≥ 1 (so it's the greater of red or blue divided by the lesser)
Test cases:
Let F(n, r) define application of the function for n iterations and a ratio of r
F(0,5) = 0.00
F(1,2) = 0.50
F(1,3) = 0.42
F(5,5) = 0.28
F(10,4) = 0.31
F(40,6.25) = 0.14
This is code golf, so the shortest solution in bytes wins.
code-golf probability-theory
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
Problem statement
Pólya is playing about with his urn again and he wants you to help him calculate some probabilities.
In this urn experiment Pólya has an urn which initially contains 1 red and 1 blue bead.
For every iteration, he reaches in and retrieves a bead, then inspects the colour and places the bead back in the urn.
He then flips a fair coin, if the coin lands heads he will insert a fair 6 sided die roll amount of the same coloured bead into the urn, if it lands tails he will remove half the number of the same colored bead from the urn (Using integer division - so if the number of beads of the selected colour is odd he will remove (c-1)/2 where c is the number of beads of that colour)
Given an integer n ≥ 0 and a decimal r > 0, give the probability to 2 decimal places that the ratio between the colours of beads after n iterations is greater than or equal to r in the shortest number of bytes.
An example set of iterations:
Let (x, y) define the urn such that it contains x red beads and y blue beads.
Iteration Urn Ratio
0 (1,1) 1
1 (5,1) 5 //Red bead retrieved, coin flip heads, die roll 4
2 (5,1) 5 //Blue bead retrieved, coin flip tails
3 (3,1) 3 //Red bead retrieved, coin flip tails
4 (3,4) 1.333... //Blue bead retrieved, coin flip heads, die roll 3
As can be seen the Ratio r is always ≥ 1 (so it's the greater of red or blue divided by the lesser)
Test cases:
Let F(n, r) define application of the function for n iterations and a ratio of r
F(0,5) = 0.00
F(1,2) = 0.50
F(1,3) = 0.42
F(5,5) = 0.28
F(10,4) = 0.31
F(40,6.25) = 0.14
This is code golf, so the shortest solution in bytes wins.
code-golf probability-theory
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
I feel like there is a formula for this...
$endgroup$
– Embodiment of Ignorance
5 hours ago
$begingroup$
Something to do with beta binomials maybe, but it might be longer to write that out
$endgroup$
– Expired Data
5 hours ago
$begingroup$
depends on the language; R and Mathematica might be able to do it efficiently.
$endgroup$
– Giuseppe
3 hours ago
add a comment |
$begingroup$
Problem statement
Pólya is playing about with his urn again and he wants you to help him calculate some probabilities.
In this urn experiment Pólya has an urn which initially contains 1 red and 1 blue bead.
For every iteration, he reaches in and retrieves a bead, then inspects the colour and places the bead back in the urn.
He then flips a fair coin, if the coin lands heads he will insert a fair 6 sided die roll amount of the same coloured bead into the urn, if it lands tails he will remove half the number of the same colored bead from the urn (Using integer division - so if the number of beads of the selected colour is odd he will remove (c-1)/2 where c is the number of beads of that colour)
Given an integer n ≥ 0 and a decimal r > 0, give the probability to 2 decimal places that the ratio between the colours of beads after n iterations is greater than or equal to r in the shortest number of bytes.
An example set of iterations:
Let (x, y) define the urn such that it contains x red beads and y blue beads.
Iteration Urn Ratio
0 (1,1) 1
1 (5,1) 5 //Red bead retrieved, coin flip heads, die roll 4
2 (5,1) 5 //Blue bead retrieved, coin flip tails
3 (3,1) 3 //Red bead retrieved, coin flip tails
4 (3,4) 1.333... //Blue bead retrieved, coin flip heads, die roll 3
As can be seen the Ratio r is always ≥ 1 (so it's the greater of red or blue divided by the lesser)
Test cases:
Let F(n, r) define application of the function for n iterations and a ratio of r
F(0,5) = 0.00
F(1,2) = 0.50
F(1,3) = 0.42
F(5,5) = 0.28
F(10,4) = 0.31
F(40,6.25) = 0.14
This is code golf, so the shortest solution in bytes wins.
code-golf probability-theory
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
Problem statement
Pólya is playing about with his urn again and he wants you to help him calculate some probabilities.
In this urn experiment Pólya has an urn which initially contains 1 red and 1 blue bead.
For every iteration, he reaches in and retrieves a bead, then inspects the colour and places the bead back in the urn.
He then flips a fair coin, if the coin lands heads he will insert a fair 6 sided die roll amount of the same coloured bead into the urn, if it lands tails he will remove half the number of the same colored bead from the urn (Using integer division - so if the number of beads of the selected colour is odd he will remove (c-1)/2 where c is the number of beads of that colour)
Given an integer n ≥ 0 and a decimal r > 0, give the probability to 2 decimal places that the ratio between the colours of beads after n iterations is greater than or equal to r in the shortest number of bytes.
An example set of iterations:
Let (x, y) define the urn such that it contains x red beads and y blue beads.
Iteration Urn Ratio
0 (1,1) 1
1 (5,1) 5 //Red bead retrieved, coin flip heads, die roll 4
2 (5,1) 5 //Blue bead retrieved, coin flip tails
3 (3,1) 3 //Red bead retrieved, coin flip tails
4 (3,4) 1.333... //Blue bead retrieved, coin flip heads, die roll 3
As can be seen the Ratio r is always ≥ 1 (so it's the greater of red or blue divided by the lesser)
Test cases:
Let F(n, r) define application of the function for n iterations and a ratio of r
F(0,5) = 0.00
F(1,2) = 0.50
F(1,3) = 0.42
F(5,5) = 0.28
F(10,4) = 0.31
F(40,6.25) = 0.14
This is code golf, so the shortest solution in bytes wins.
code-golf probability-theory
code-golf probability-theory
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
Expired Data
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago
Expired DataExpired Data
2015
2015
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Expired Data is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
I feel like there is a formula for this...
$endgroup$
– Embodiment of Ignorance
5 hours ago
$begingroup$
Something to do with beta binomials maybe, but it might be longer to write that out
$endgroup$
– Expired Data
5 hours ago
$begingroup$
depends on the language; R and Mathematica might be able to do it efficiently.
$endgroup$
– Giuseppe
3 hours ago
add a comment |
$begingroup$
I feel like there is a formula for this...
$endgroup$
– Embodiment of Ignorance
5 hours ago
$begingroup$
Something to do with beta binomials maybe, but it might be longer to write that out
$endgroup$
– Expired Data
5 hours ago
$begingroup$
depends on the language; R and Mathematica might be able to do it efficiently.
$endgroup$
– Giuseppe
3 hours ago
$begingroup$
I feel like there is a formula for this...
$endgroup$
– Embodiment of Ignorance
5 hours ago
$begingroup$
I feel like there is a formula for this...
$endgroup$
– Embodiment of Ignorance
5 hours ago
$begingroup$
Something to do with beta binomials maybe, but it might be longer to write that out
$endgroup$
– Expired Data
5 hours ago
$begingroup$
Something to do with beta binomials maybe, but it might be longer to write that out
$endgroup$
– Expired Data
5 hours ago
$begingroup$
depends on the language; R and Mathematica might be able to do it efficiently.
$endgroup$
– Giuseppe
3 hours ago
$begingroup$
depends on the language; R and Mathematica might be able to do it efficiently.
$endgroup$
– Giuseppe
3 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
JavaScript (ES6), 145 139 135 132 bytes
Takes input as (r)(n). This is a naive solution that actually performs the entire simulation.
r=>g=(n,B=!(k=s=0),R=1,h=d=>++d<7?h(d,[0,d].map(b=>g(n,b?-~B>>1:B,b?R:-~R>>1)&g(n,B+b,R+d-b))):s/k)=>n--?h``:s+=(k++,B>R?B/R:R/B)>=r
Try it online!
Too slow for the last 2 test cases.
$endgroup$
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Expired Data is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181551%2fp%25c3%25b3lya-urn-flip-and-roll%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (ES6), 145 139 135 132 bytes
Takes input as (r)(n). This is a naive solution that actually performs the entire simulation.
r=>g=(n,B=!(k=s=0),R=1,h=d=>++d<7?h(d,[0,d].map(b=>g(n,b?-~B>>1:B,b?R:-~R>>1)&g(n,B+b,R+d-b))):s/k)=>n--?h``:s+=(k++,B>R?B/R:R/B)>=r
Try it online!
Too slow for the last 2 test cases.
$endgroup$
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 145 139 135 132 bytes
Takes input as (r)(n). This is a naive solution that actually performs the entire simulation.
r=>g=(n,B=!(k=s=0),R=1,h=d=>++d<7?h(d,[0,d].map(b=>g(n,b?-~B>>1:B,b?R:-~R>>1)&g(n,B+b,R+d-b))):s/k)=>n--?h``:s+=(k++,B>R?B/R:R/B)>=r
Try it online!
Too slow for the last 2 test cases.
$endgroup$
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 145 139 135 132 bytes
Takes input as (r)(n). This is a naive solution that actually performs the entire simulation.
r=>g=(n,B=!(k=s=0),R=1,h=d=>++d<7?h(d,[0,d].map(b=>g(n,b?-~B>>1:B,b?R:-~R>>1)&g(n,B+b,R+d-b))):s/k)=>n--?h``:s+=(k++,B>R?B/R:R/B)>=r
Try it online!
Too slow for the last 2 test cases.
$endgroup$
JavaScript (ES6), 145 139 135 132 bytes
Takes input as (r)(n). This is a naive solution that actually performs the entire simulation.
r=>g=(n,B=!(k=s=0),R=1,h=d=>++d<7?h(d,[0,d].map(b=>g(n,b?-~B>>1:B,b?R:-~R>>1)&g(n,B+b,R+d-b))):s/k)=>n--?h``:s+=(k++,B>R?B/R:R/B)>=r
Try it online!
Too slow for the last 2 test cases.
edited 6 hours ago
answered 6 hours ago
ArnauldArnauld
78.7k795327
78.7k795327
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
add a comment |
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
$begingroup$
I really like this answer, I found that in order to solve the later test cases I needed to add code to merge the same ratio probabilities. So I'm not surprised it's too slow
$endgroup$
– Expired Data
3 hours ago
add a comment |
Expired Data is a new contributor. Be nice, and check out our Code of Conduct.
Expired Data is a new contributor. Be nice, and check out our Code of Conduct.
Expired Data is a new contributor. Be nice, and check out our Code of Conduct.
Expired Data is a new contributor. Be nice, and check out our Code of Conduct.
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181551%2fp%25c3%25b3lya-urn-flip-and-roll%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
I feel like there is a formula for this...
$endgroup$
– Embodiment of Ignorance
5 hours ago
$begingroup$
Something to do with beta binomials maybe, but it might be longer to write that out
$endgroup$
– Expired Data
5 hours ago
$begingroup$
depends on the language; R and Mathematica might be able to do it efficiently.
$endgroup$
– Giuseppe
3 hours ago