A doctor gives you a prescription of 45 pills. You are to take 1.5 pills once each day. Obviously this means you will wind up cutting pills at some point.
Each day, you shake two units out of the bottle. One of the following things can happen:
- You get 2 whole pills. You cut one in half, take your dose, and put the other half pill back in the bottle. (Obviously this will happen on the first day).
- You get 1 whole pill and a half pill and take them
- You get 2 half pills, so you shake another out of the bottle, at which time:
--- You get a half pill and take the dose
--- You get a whole pill and break it in half, taking you dose and depositing the other half back into the bottle.
Ignore the effects of half pills being more likely to settle to the bottom.
You continue this for each day of the month. On the last day, there will be an equivalent of 1.5 pills in the bottle. What are the odds that it is:
- one and a half pills?
- three half-pills?
I'm writing a little script to run through iterations of the scenario, but is there a more elegant way to calculate?
Thanks.
Quote: backprop
Each day, you shake two units out of the bottle. One of the following things can happen:
- You get 2 whole pills. You cut one in half, take your dose, and put the other half pill back in the bottle. (Obviously this will happen on the first day).
- You get 1 whole pill and a half pill and take them
- You get 2 half pills, so you shake another out of the bottle, at which time:
--- You get a half pill and take the dose
--- You get a whole pill and break it in half, taking you dose and depositing the other half back into the bottle.
*You get a whole pill and break it in half, taking you dose and depositing the other half back into the bottle.
Why would you do that? Surely if you've got two halves then get another whole, the obvious thing would be to put one of the halfs back and just take the whole and half you have left. Why would you break the whole one in two?
Quote: estebanrey*You get a whole pill and break it in half, taking you dose and depositing the other half back into the bottle.
Why would you do that? Surely if you've got two halves then get another whole, the obvious thing would be to put one of the halfs back and just take the whole and half you have left. Why would you break the whole one in two?
Fair point, but it doesn't really matter to the question. The bottle winds up getting a half pill deposited back into it either way.
Quote: MangoJWhy would you put the second half part back into the bottle in the first if you will anyway need it the very next day ?
I only have one bottle, it's childproof, and I have a toddler.
Quote: backpropI only have one bottle, it's childproof, and I have a toddler.
Soon enough you'll find the kid is the only one with the hand strength to open the bottle for you.
Invite your followers to drink.
???
profit!!
Quote: backpropJust bored enough to intellectually challenge myself with something slightly less than earth-shattering, I suppose :)
You'll fit in well around here.
I bet if you made the question more like:
You're in AC and need to bet $7.50 a day from the chips in your pocket.
When you start you have only red chips, so the first day you'll need to cheque change one red into two pink to place your bet.
etc.
you'd get better results.
1. Simulation
2. Spreadsheet
3. Matrix algebra
4. Recursion
Simulation would be what I go with. However, simulation problems are not mathematically challenging. Forgive me if I pass. My rough guess at the probability that there will be a whole pill in the bottle on the last day is about 10%.
Quote: dwheatleyThe states you could use to do Matrix Algebra are all different. There's no movement between states: you need to individually track the number of whole and half pills in the bottle. I agree, not elegant.
You're right. The removal of pills from the bottle makes for another kind of animal. I stand corrected. If you put the pills back in the bottle then I would maintain that a 46x46 matrix, representing the possible day to day transitions, could be taken to the 30th power for the right answer.
Quote: backpropFor the record, the simulation settles in at around 34.5% having a full/half on the last day, and the remainder with three half pills.
Thanks. I knew somebody would eventually do one.
Quote: sodawaterJust take 9 pills at once every 6 days
Very good! I would certainly be tempted to take 2 pills one day, then one the next. Perhaps sodawater and I make bad patients.
However, after he had the first 2 he felt much better.
He's a Doctor.
Quote: backpropFor the record, the simulation settles in at around 34.5% having a full/half on the last day, and the remainder with three half pills.
I think you have this backwards.
Furthermore, I get slightly different percentages.
I've written lots of different simulations before. Hundreds of them, for all sorts of things. (Dice tossing, drawing cards from a deck, etc., etc.) I like writing them. (Probably because my math skills aren't good enough to calculate the probability of what I'm trying to determine otherwise.)
I spent about 25 minutes this morning writing a program to simulate this. (That's all it took me... and what else do I have to do on an early Saturday morning?)
I've run my program several times, and I've looked over my code for errors and I just don't see any.
The results of the last 100 million run simulation looks like this:
Number of times we had THREE, half pills on the final day: 31,183,435
Percentage: .31183435
Number of times we had just ONE, half pill (and one FULL pill) on the final day: 68,816,565
Percentage: .68816565
Total sims this run: 100,000,000
Almost identical percentages occur, of course, for all other simulations, of all durations.
Quote: MonkeyMonkeyAn aspect of this that I haven't seen mentioned is the number of pills that could tumble out of the bottle. What sort of assumption is being made? If you could only ever get 2 where: a) both are whole, b) one is half, one is whole, then we're missing a possible instance where 3 halves could tumble out. if 3 pieces can come out there are a lot of other combinations, some of which would allow you to pick the necessary pieces to make the 1 1/2 you need and wouldn't that potentially skew the result at the end?
It's a simplification. Taking your scenario to its logical conclusion, there's no point in modeling anything because you could just dump out the entire bottle onto the counter, spread them out, and take the most efficient combination of pills available.
For the same reason, I mentioned that we have to ignore the fact that smaller pills have a tendency to settle to the bottom as you handle the container, and that skews the results as well.
In the end, I'm thinking more math than physics here. If we have to give a physical description as to how it's done, pretend the assumption is that you reach in with your index finger, pin the first object you encounter to the side of the bottle, and pull it out. And yes, you'd have to ignore the fact that your finger is more likely to come across a whole pill than a half pill.....ad nauseum.
Quote: EdCollinsI think you have this backwards.
Of course you're right. I was prematurely determining marking a full/half before getting the correct dosage on the last day. I get similar numbers as you. Thanks for fact checking. Here's the script.
<script>
var numpills=45;
var numtrials=1000000;
var numdays=30;
// this counter tracks the number of cycles where there is a full/half pill left, vs only halfs
var totalNumFulls=0;
for(i=0;i<numtrials;i++){
// fill up the pill bottle with full pills
var bottle=new Array();
for(var p=0;p<numpills;p++){
bottle[p]=1;
}
for(d=0;d<numdays;d++){
// keep shaking out pills until we get to or past our 1.5 dosage
var dosagefound=false;
var pillsout=0;
// if it's the last day, just figure out now if there's a full pill and add it to the tally
if(d==numdays-1){
for(var j=0;j<bottle.length;j++){
if(bottle==1){
totalNumFulls++;
}
}
}
// keep shaking
while(!dosagefound){
// get the first available random pill
pilltocheck=Math.floor(Math.random()*bottle.length)
// add it to what's out of the bottle for today.
pillsout+=bottle[pilltocheck];
// if you don't have enough, remove that pill from the bottle array and proceed
if(pillsout < 1.5){
bottle.splice(pilltocheck,1);
}
// if you have exactly what you need, stop shaking and remove that pill from the bottle array
else if(pillsout == 1.5){
dosagefound=true;
bottle.splice(pilltocheck,1);
}
// if you're past what you need, stop shaking and return 0.5 to the bottle position where you got the full
else if(pillsout > 1.5){
dosagefound=true;
bottle[pilltocheck]=0.5;
}
}
}
}
document.write("Out of " + numtrials + " trials, " + totalNumFulls + " (" + totalNumFulls*1.0/numtrials*100.0 + "%) had a full/half while " + (numtrials - totalNumFulls) + " (" + (numtrials - totalNumFulls)*1.0/numtrials*100.0 + "%) had three halfs.")
</script>
Out of 1000000 trials, 687399 (68.73989999999999%) had a full/half while 312601 (31.2601%) had three halfs.
Thanks for the mental exercise.
Note: I'm a little surprised your percentages aren't even closer to mine than what they are. A million runs should put it pretty darn close.
OPEN"c:\test.txt" FOR OUTPUT AS #1
RANDOMIZE TIMER : max_sims = 1000000
FOR sim = 1 TO max_sims
number_half_pills = 0 : number_full_pills = 45
FOR day = 1 TO 29
number_pills_left = number_full_pills + number_half_pills
rand_pill_one = RND(1, number_pills_left)
DO
rand_pill_two = RND(1, number_pills_left)
LOOP UNTIL rand_pill_two <> rand_pill_one
IF rand_pill_one <= number_half_pills AND rand_pill_two <= number_half_pills THEN
DO
rand_pill_three = RND(1, number_pills_left)
LOOP UNTIL rand_pill_three <> rand_pill_one AND rand_pill_three <> rand_pill_two
IF rand_pill_three <= number_half_pills THEN
number_half_pills = number_half_pills - 3
ELSE
number_half_pills = number_half_pills - 1 : number_full_pills = number_full_pills - 1
END IF
ELSEIF (rand_pill_one <= number_half_pills AND rand_pill_two > number_half_pills) OR_
rand_pill_two <= number_half_pills AND rand_pill_one > number_half_pills THEN
number_half_pills = number_half_pills - 1 : number_full_pills = number_full_pills - 1
ELSEIF rand_pill_one > number_half_pills AND rand_pill_two > number_half_pills THEN
number_half_pills = number_half_pills + 1 : number_full_pills = number_full_pills - 2
END IF
NEXT day
IF number_half_pills = 3 THEN
INCR three_half_pills
ELSEIF number_half_pills = 1 THEN
INCR one_half_pills
END IF
NEXT sim
PRINT #1, "Number of times we had THREE, half pills on the final day:"; three_half_pills
PRINT #1, "Percentage:"; three_half_pills/max_sims
PRINT #1,
PRINT #1, "Number of times we had just ONE, half pill (and one FULL pill) on the final day:"; one_half_pills
PRINT #1, "Percentage:"; one_half_pills/max_sims
PRINT #1,
PRINT #1, "Total sims this run:"; max_sims
CLOSE #1
Here's the method:
Let (W,H) be the state with W whole pills and H half pills.
From (W,H):
(1) your first pill is a whole (probability W / (W+H))...
(1a) and your second pill is also whole (probability (W-1) / (W+H-1)); this is state (W-2,H+1)
(1b) and your second pill is a half (probability H / (W+H-1)); this is state (W-1,H-1)
(2) your first pill is a half (probability H / (W+H))...
(2a) and your second pill is whole (probability W / (W+H-1)); this is state (W-1,H-1)
(2b) and your second pill is also a half (probability (H-1) / (W+H-1))...
(2b1) and your third pill is whole (probability W / (W+H-2)); this is state (W-1,H-1) (two halves out, then a whole out, then a half back in)
(2b2) and your third pill is also a half (probability (H-2) / (W+H-2)); this is state (W,H-3)
The initial state is (45,0)
Since you can get from (W,H) to (W, something < H) but not (W, something > H) or (something > W, anything), work from W=45 down to W=0, and for each W, work from H = (45-W)/2 (since 2H + W <= 45) down to H = 0 (you need to work down because, for example, state (0,12) will contribute to state (0,9)).
From state (W,H):
add W / (W+H) x (W-1) / (W+H-1) to state (W-2, H+1);
add W / (W+H) x H / (W+H-1), H / (W-H) x W / (W+H-1), and H / (W+H) x (H-1) / (W+H-1) x W / (W+H-2) to state (W-1,H-1);
add H / (W+H) x (H-1) / (W+H-1) x (H-2) / (W+H-2) to state (W, H-3).
The eventual probabilities for one of each and three halves are in states (1,1) and (0,3), respectively.
Quote: ThatDonGuyAssuming that the ratio of the probabilities of drawing a whole pill to drawing a half pill is equal to the ratio of whole pills to half pills, I did a brute force recursive/state-mapped solution and got 0.688130417076 for having a whole and a half left, and 0.311869582925 for having three halves.
Thanks. I see that's pretty darn close to my .68816565 and .31183435 figures arrived at via a simulation.
Quote: ThatDonGuyAssuming that the ratio of the probabilities of drawing a whole pill to drawing a half pill is equal to the ratio of whole pills to half pills, I did a brute force recursive/state-mapped solution and got 0.688130417076 for having a whole and a half left, and 0.311869582925 for having three halves.
Here's the method:
Let (W,H) be the state with W whole pills and H half pills.
From (W,H):
(1) your first pill is a whole (probability W / (W+H))...
(1a) and your second pill is also whole (probability (W-1) / (W+H-1)); this is state (W-2,H+1)
(1b) and your second pill is a half (probability H / (W+H-1)); this is state (W-1,H-1)
(2) your first pill is a half (probability H / (W+H))...
(2a) and your second pill is whole (probability W / (W+H-1)); this is state (W-1,H-1)
(2b) and your second pill is also a half (probability (H-1) / (W+H-1))...
(2b1) and your third pill is whole (probability W / (W+H-2)); this is state (W-1,H-1) (two halves out, then a whole out, then a half back in)
(2b2) and your third pill is also a half (probability (H-2) / (W+H-2)); this is state (W,H-3)
The initial state is (45,0)
Since you can get from (W,H) to (W, something < H) but not (W, something > H) or (something > W, anything), work from W=45 down to W=0, and for each W, work from H = (45-W)/2 (since 2H + W <= 45) down to H = 0 (you need to work down because, for example, state (0,12) will contribute to state (0,9)).
From state (W,H):
add W / (W+H) x (W-1) / (W+H-1) to state (W-2, H+1);
add W / (W+H) x H / (W+H-1), H / (W-H) x W / (W+H-1), and H / (W+H) x (H-1) / (W+H-1) x W / (W+H-2) to state (W-1,H-1);
add H / (W+H) x (H-1) / (W+H-1) x (H-2) / (W+H-2) to state (W, H-3).
The eventual probabilities for one of each and three halves are in states (1,1) and (0,3), respectively.
This is really cool; thanks. I need to get my head around it by trying to attack it the same way. So it's basically recursing through all the (valid) states 'downstream' of (45,0) and then finding the total of (1,1) instances and (0,3) instances and dividing by the total # of states achieved?
Quote: backpropThis is really cool; thanks. I need to get my head around it by trying to attack it the same way. So it's basically recursing through all the (valid) states 'downstream' of (45,0) and then finding the total of (1,1) instances and (0,3) instances and dividing by the total # of states achieved?
Not the total number of instances, but the probability of getting to each state, starting with a probability of 1 for state (45,0). Other than that, yes, you go through the possible branches from each state.
Quote: ten2winYou should ask SOOPOO for the answer.
He's a Doctor.
Or a drug addict. All the pills will be gone in the first hour.
Chances of the last pill being whole: 100%
Quote: sodawaterbecause that's 2 pills per day, not 1.5
Right, misstated.
I basically meant to suggest that you would take one at midnight every other day, sorry about that.
N-M-N-N-M-N-N-M-N-N-M-N-N-M-N (15 Pills, 10 Days)
Quote: ThatDonGuyAssuming that the ratio of the probabilities of drawing a whole pill to drawing a half pill is equal to the ratio of whole pills to half pills, I did a brute force recursive/state-mapped solution and got 0.688130417076 for having a whole and a half left, and 0.311869582925 for having three halves.
Here's the method:
.....
I followed this method and got very similar numbers:
Probability of whole/half: 68.81304169264088%
Probability of halves: 31.186958287814676%
I'm guessing the small difference is due to floating point operation errors but maybe I have something slightly off.
Here's the javascript. It was interesting that scenarios up to 36 pills went fairly quickly, but 45 took over an hour (on my old-ish laptop).
I didn't do absolutely pure recursion, as I kept the tallies for the probabilities of (1,1) and (0,3) in global variables. But otherwise it's very simple. Yesterday I missed the half/half/whole probability for about two hours and kept winding up with total probabilities < 100%. If not for that it would have been a 15 minute exercise.
Thanks for the pointers.
<script>
phalfs=0;
pwholehalf=0;
function drilldown(wp,hp,p,i){
if (p<=0 || wp<0 || hp<0) return;
if(wp==1 && hp==1){
pwholehalf+=p;
return;
}
if(wp==0 & hp==3){
phalfs+=p;
return;
}
// Now figure out the different combinations and probabilities of 'next' states.
// 1. next state is wp-2, hp+1
//whole and whole
if(wp>=2) drilldown(wp-2,hp+1,p*((wp/(wp+hp))*((wp-1)/(wp+hp-1))),i+1);
// 2. next state is wp-1, hp-1
// add possibilities of whole/half, half/whole, half/half/whole
phw=0;
if(wp>=1 && hp>=1) phw+=(wp/(wp+hp))*(hp/(wp+hp-1))
if(wp>=1 && hp>=1) phw+=(hp/(wp+hp))*(wp/(wp+hp-1))
if(hp>=2 && wp>=1) phw+=(hp/(hp+wp)*(hp-1)/(hp+wp-1)*wp/(hp+wp-2));
if(phw>0) drilldown(wp-1,hp-1,p*phw,i+1);
// 3. next state is hp-3
//three halfs
if(hp>=3) drilldown(wp,hp-3,p*(hp/(wp+hp))*((hp-1)/(wp+hp-1))*((hp-2)/(wp+hp-2)),i+1)
}
drilldown(45,0,1.0,0);
document.write("<br /><br />Probability of whole/half: " + pwholehalf*100.0 + "% <br />Probability of halves: " + phalfs*100.0 + "%");
</script>
Quote: backpropI'm guessing the small difference is due to floating point operation errors but maybe I have something slightly off.
If somebody has an arbitrary-precision calculator (and if I crunched the numbers correctly), the probability of having one whole and one half is exactly:
(581103 x 15647406142778942242813) / (4194304 x 625 x 184877 x 37349 x 11191 x 65231)
Note that all of the numbers are relatively prime to each other.
Quote: backprop... Here's the script.....
While you're busy writing a script, why not have the doctor rewrite you a script for a different dosage pill? If you need 150mg per day, and he gave you 100mg dosage, how about getting a script for three 50mg pills rather than breaking the 100s apart? Or maybe there's a single 150mg you could take.
Quote: zippyboyWhile you're busy writing a script, why not have the doctor rewrite you a script for a different dosage pill? If you need 150mg per day, and he gave you 100mg dosage, how about getting a script for three 50mg pills rather than breaking the 100s apart? Or maybe there's a single 150mg you could take.
Thanks for your input. The question - in the "Math" forum even - was about math, not a real medical situation.
Pills with casings and time-release pills shouldn't be split.
Quote: backpropI followed this method and got very similar numbers:
Probability of whole/half: 68.81304169264088%
Probability of halves: 31.186958287814676%
I'm guessing the small difference is due to floating point operation errors but maybe I have something slightly off.
Here's the javascript. It was interesting that scenarios up to 36 pills went fairly quickly, but 45 took over an hour (on my old-ish laptop).
I didn't do absolutely pure recursion, as I kept the tallies for the probabilities of (1,1) and (0,3) in global variables. But otherwise it's very simple. Yesterday I missed the half/half/whole probability for about two hours and kept winding up with total probabilities < 100%. If not for that it would have been a 15 minute exercise.
Thanks for the pointers.
I think your recursion is diving too deep and redundantly. I just did an iterative solution (also Javascript. Combinations function, C(n,r), omitted for clarity):
var data = [];
var size = 45;
data[size] = [];
data[size][0] = 1;
for (whole = data.length-1; whole >=0; whole--)
{
if (!data[whole]) continue;
for (half = data[whole].length-1; half >= 0; half--)
{
if (!data[whole][half]) continue;
// test each possible state for each day;
// if state has >=2 whole pills, can transition via whole/whole
if (whole >= 2)
{
if (!data[whole-2]) data[whole-2] = [];
if (!data[whole-2][half+1]) data[whole-2][half+1] = 0;
data[whole-2][half+1] += data[whole][half] * C(whole,2)/C(whole+half,2);
}
// if state has >=1 whole and >=1 half, can transition via whole/half
if ((whole >= 1) && (half >= 1))
{
if (!data[whole-1]) data[whole-1] = [];
if (!data[whole-1][half-1]) data[whole-1][half-1] = 0;
data[whole-1][half-1] += data[whole][half] * C(whole,1)*C(half,1)/C(whole+half,2);
}
// if state has >=2 half and >=1 whole, can transition via h/h/whole
// leads to the same end-state as whole/half
if ((whole >= 1) && (half >= 2))
{
if (!data[whole-1]) data[whole-1] = [];
if (!data[whole-1][half-1]) data[whole-1][half-1] = 0;
data[whole-1][half-1] += data[whole][half] * (C(half,2)/C(whole+half,2)) * (C(whole,1)/C(whole+half-2,1));
}
// if state has >=3 half, can transition via h/h/h
if ((half >= 3))
{
if (!data[whole][half-3]) data[whole][half-3] = 0;
data[whole][half-3] += data[whole][half] * (C(half,3)/C(whole+half,3));
}
}
}
console.log("p(whole/half) = " +data[1][1]*100.0 + "%; p(half/half/half) = " + data[0][3]*100.0 + "%");
Output is
p(whole/half) = 68.81304170759032%; p(half/half/half) = 31.186958292409596%.
Runtime is 12ms. And you could keep a fraction object (numerator and denominator) in the array instead of just a single percentage to get the exact result.
n.b. I actually used "i" and "j" in my code, instead of "whole" and "half", but the code parser in this forum treats an "i" in brackets as the italics format so I did a search/replace.
Also, Cloud9 FTW!
Quote: MathExtremistI think your recursion is diving too deep and redundantly. I just did an iterative solution (also Javascript. Combinations function, C(n,r), omitted for clarity):
Why in holy hell would you use Javascript for something like this? My brain hurts.
Quote: AcesAndEightsWhy in holy hell would you use Javascript for something like this? My brain hurts.
Because the OP did. The client is always right. :)
I would normally use C# for this kind of thing because I like the built in set manipulators, but I admit to not having learned any useful languages recently. The hardcore coding part of my career is long over.
btw I did javascript because I have effectively notepad and a browser on my netbook where I usually noodle with this stuff :)
Quote: backpropThat is really cool. It is taking me a few minutes to sink in but I think it's finally clicking. I think I need to write it to actually understand but it makes sense. Thanks!
btw I did javascript because I have effectively notepad and a browser on my netbook where I usually noodle with this stuff :)
In that case, definitely check out Cloud9. You can still use Javascript (or Python, Ruby, or PHP) but you also get a proper code editor with syntax highlighting and auto-completion, plus debug mode with breakpoints, state inspection, etc.
www.c9.io
Nobody should ever need to code in Notepad. Not with all the fun stuff on the web. But if you must, at least download something like Notepad++ and Firebug.
Quote: MathExtremistIn that case, definitely check out Cloud9. You can still use Javascript (or Python, Ruby, or PHP) but you also get a proper code editor with syntax highlighting and auto-completion, plus debug mode with breakpoints, state inspection, etc.
www.c9.io
Nobody should ever need to code in Notepad. Not with all the fun stuff on the web. But if you must, at least download something like Notepad++ and Firebug.
emacs ftw
Quote: MathExtremistBecause the OP did. The client is always right. :)
I would normally use C# for this kind of thing because I like the built in set manipulators, but I admit to not having learned any useful languages recently. The hardcore coding part of my career is long over.
Ah I didn't notice that.
Quote: ThatDonGuyIf somebody has an arbitrary-precision calculator (and if I crunched the numbers correctly), the probability of having one whole and one half is exactly:
(581103 x 15647406142778942242813) / (4194304 x 625 x 184877 x 37349 x 11191 x 65231)
Note that all of the numbers are relatively prime to each other.
bc
scale=99
a=(581103*15647406142778942242813) / (4194304*625*184877*37349*11191*65231)
a
0.688130417075903792314368434720157072863971135103004607340044931959853168119832890833389191020411554