backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 5th, 2012 at 12:02:54 PM permalink
I'm wondering how to approach this statistics question, if there is a way to model it beyond brute force.

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.
TIMSPEED
TIMSPEED
  • Threads: 89
  • Posts: 1246
Joined: Aug 11, 2010
October 5th, 2012 at 12:20:52 PM permalink
Exactly how bored are you??
Gambling calls to me...like this ~> http://www.youtube.com/watch?v=4Nap37mNSmQ
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 5th, 2012 at 12:27:33 PM permalink
Just bored enough to intellectually challenge myself with something slightly less than earth-shattering, I suppose :)
MangoJ
MangoJ
  • Threads: 10
  • Posts: 905
Joined: Mar 12, 2011
October 5th, 2012 at 1:30:30 PM permalink
Why would you put the second half part back into the bottle in the first if you will anyway need it the very next day ?
estebanrey
estebanrey
  • Threads: 4
  • Posts: 40
Joined: Oct 4, 2012
October 5th, 2012 at 1:39:23 PM permalink
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?
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 5th, 2012 at 1:53:31 PM permalink
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.
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 5th, 2012 at 1:54:19 PM permalink
Quote: MangoJ

Why 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.
FarFromVegas
FarFromVegas
  • Threads: 7
  • Posts: 878
Joined: Dec 10, 2010
October 5th, 2012 at 2:10:53 PM permalink
Quote: backprop

I 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.
Each of us is entitled to his own opinion, but not to his own facts. Preparing for a fight about your bad decision is not as smart as making a good decision.
WongBo
WongBo
  • Threads: 62
  • Posts: 2126
Joined: Feb 3, 2012
October 5th, 2012 at 3:07:26 PM permalink
Poor all the pills into a pitcher of grape koolaid.
Invite your followers to drink.
???
profit!!
In a bet, there is a fool and a thief. - Proverb.
sodawater
sodawater
  • Threads: 64
  • Posts: 3321
Joined: May 14, 2012
October 5th, 2012 at 4:12:40 PM permalink
deleted
Last edited by: sodawater on Oct 1, 2018
MonkeyMonkey
MonkeyMonkey
  • Threads: 13
  • Posts: 770
Joined: May 1, 2012
October 5th, 2012 at 5:01:34 PM permalink
Quote: backprop

Just 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.
Wizard
Administrator
Wizard
  • Threads: 1518
  • Posts: 27037
Joined: Oct 14, 2009
October 5th, 2012 at 5:11:58 PM permalink
I don't see an easy elegant solution to this. The four methods to solve it that come to my mind are:

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%.
"For with much wisdom comes much sorrow." -- Ecclesiastes 1:18 (NIV)
dwheatley
dwheatley
  • Threads: 25
  • Posts: 1246
Joined: Nov 16, 2009
October 5th, 2012 at 7:37:52 PM permalink
The 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.
Wisdom is the quality that keeps you out of situations where you would otherwise need it
Wizard
Administrator
Wizard
  • Threads: 1518
  • Posts: 27037
Joined: Oct 14, 2009
October 5th, 2012 at 7:52:36 PM permalink
Quote: dwheatley

The 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.
"For with much wisdom comes much sorrow." -- Ecclesiastes 1:18 (NIV)
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 5th, 2012 at 9:03:40 PM permalink
For 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.
Wizard
Administrator
Wizard
  • Threads: 1518
  • Posts: 27037
Joined: Oct 14, 2009
October 5th, 2012 at 11:10:00 PM permalink
Quote: backprop

For 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.
"For with much wisdom comes much sorrow." -- Ecclesiastes 1:18 (NIV)
odiousgambit
odiousgambit
  • Threads: 327
  • Posts: 9734
Joined: Nov 9, 2009
October 6th, 2012 at 2:04:56 AM permalink
Quote: sodawater

Just 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.
the next time Dame Fortune toys with your heart, your soul and your wallet, raise your glass and praise her thus: “Thanks for nothing, you cold-hearted, evil, damnable, nefarious, low-life, malicious monster from Hell!”   She is, after all, stone deaf. ... Arnold Snyder
Switch
Switch
  • Threads: 12
  • Posts: 934
Joined: Apr 29, 2010
October 6th, 2012 at 5:25:07 AM permalink
I know a guy who tried to commit suicide by swallowing a whole bottle of pills.

However, after he had the first 2 he felt much better.
ten2win
ten2win
  • Threads: 20
  • Posts: 331
Joined: Jul 27, 2010
October 6th, 2012 at 7:44:16 AM permalink
You should ask SOOPOO for the answer.

He's a Doctor.
I don't know everything but I know a lot.
EdCollins
EdCollins
  • Threads: 20
  • Posts: 1739
Joined: Oct 21, 2011
October 6th, 2012 at 7:44:41 AM permalink
Quote: backprop

For 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.
MonkeyMonkey
MonkeyMonkey
  • Threads: 13
  • Posts: 770
Joined: May 1, 2012
October 6th, 2012 at 8:04:22 AM permalink
An 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?
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 6th, 2012 at 8:11:55 AM permalink
You certainly ran more iterations than me (I did 1M), but something else is going on. Let me look over my code, too, and I'll post it. I'm on a little netbook and all I have to run is javascript.
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 6th, 2012 at 8:17:25 AM permalink
Quote: MonkeyMonkey

An 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.
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 6th, 2012 at 8:40:04 AM permalink
Quote: EdCollins

I 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.
EdCollins
EdCollins
  • Threads: 20
  • Posts: 1739
Joined: Oct 21, 2011
October 6th, 2012 at 8:56:30 AM permalink
No problem. Thanks for verifying that my numbers were correct.

Thanks for the mental exercise.
EdCollins
EdCollins
  • Threads: 20
  • Posts: 1739
Joined: Oct 21, 2011
October 6th, 2012 at 9:18:00 AM permalink
My code, not that it matters. And yes, it's not the most elegant want to code it, by any means. But it was fastest for me, and it got the job done.

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
MonkeyMonkey
MonkeyMonkey
  • Threads: 13
  • Posts: 770
Joined: May 1, 2012
October 6th, 2012 at 9:22:17 AM permalink
FWIW, there are "code" tags here that should hold the indenting.
EdCollins
EdCollins
  • Threads: 20
  • Posts: 1739
Joined: Oct 21, 2011
October 6th, 2012 at 10:33:13 AM permalink
Ah, thanks!
ThatDonGuy
ThatDonGuy
  • Threads: 122
  • Posts: 6679
Joined: Jun 22, 2011
October 6th, 2012 at 12:18:33 PM permalink
Assuming 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.
EdCollins
EdCollins
  • Threads: 20
  • Posts: 1739
Joined: Oct 21, 2011
October 6th, 2012 at 12:24:23 PM permalink
Quote: ThatDonGuy

Assuming 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.
Toes14
Toes14
  • Threads: 18
  • Posts: 455
Joined: May 6, 2010
October 6th, 2012 at 1:00:47 PM permalink
Or you could just split all the pills in half and take 3 of them each dose, saving yourself the time wondering about the issue . . .
"Bite my Glorious Golden Ass!" - Bender Bending Rodriguez
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 6th, 2012 at 2:29:31 PM permalink
Quote: ThatDonGuy

Assuming 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?
ThatDonGuy
ThatDonGuy
  • Threads: 122
  • Posts: 6679
Joined: Jun 22, 2011
October 6th, 2012 at 3:25:35 PM permalink
Quote: backprop

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?


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.
buzzpaff
buzzpaff
  • Threads: 112
  • Posts: 5328
Joined: Mar 8, 2011
October 6th, 2012 at 4:11:03 PM permalink
Quote: ten2win

You should ask SOOPOO for the answer.

He's a Doctor.



Or a drug addict. All the pills will be gone in the first hour.
Mission146
Mission146
  • Threads: 142
  • Posts: 16832
Joined: May 15, 2012
October 6th, 2012 at 6:21:45 PM permalink
It's not within the parameters of the OP, but why wouldn't you just take your first pill at noon, your second pill at midnight, and then go every twelve hours from then on. The first, "Half," of the midnight pill constitutes the half for the previous day while the second, "Half," is the half for the following day.

Chances of the last pill being whole: 100%
https://wizardofvegas.com/forum/off-topic/gripes/11182-pet-peeves/120/#post815219
sodawater
sodawater
  • Threads: 64
  • Posts: 3321
Joined: May 14, 2012
October 6th, 2012 at 6:23:39 PM permalink
deleted
Last edited by: sodawater on Oct 1, 2018
Mission146
Mission146
  • Threads: 142
  • Posts: 16832
Joined: May 15, 2012
October 6th, 2012 at 6:39:30 PM permalink
Quote: sodawater

because 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)
https://wizardofvegas.com/forum/off-topic/gripes/11182-pet-peeves/120/#post815219
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 7th, 2012 at 9:23:25 AM permalink
Quote: ThatDonGuy

Assuming 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>
ThatDonGuy
ThatDonGuy
  • Threads: 122
  • Posts: 6679
Joined: Jun 22, 2011
October 7th, 2012 at 12:07:10 PM permalink
Quote: backprop

I'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.
zippyboy
zippyboy
  • Threads: 2
  • Posts: 1124
Joined: Jan 19, 2011
October 7th, 2012 at 5:15:31 PM permalink
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.
"Poker sure is an easy game to beat if you have the roll to keep rebuying."
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 7th, 2012 at 5:17:19 PM permalink
Quote: zippyboy

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.



Thanks for your input. The question - in the "Math" forum even - was about math, not a real medical situation.
Nareed
Nareed
  • Threads: 373
  • Posts: 11413
Joined: Nov 11, 2009
October 7th, 2012 at 5:54:51 PM permalink
Do split pills digest the same way as whole pills?

Pills with casings and time-release pills shouldn't be split.
Donald Trump is a fucking criminal
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
October 8th, 2012 at 5:51:59 PM permalink
Quote: backprop

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.


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!
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
AcesAndEights
AcesAndEights
  • Threads: 67
  • Posts: 4300
Joined: Jan 5, 2012
October 8th, 2012 at 6:08:20 PM permalink
Quote: MathExtremist

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):


Why in holy hell would you use Javascript for something like this? My brain hurts.
"So drink gamble eat f***, because one day you will be dust." -ontariodealer
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
October 8th, 2012 at 6:36:32 PM permalink
Quote: AcesAndEights

Why 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.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
backprop
backprop
  • Threads: 1
  • Posts: 12
Joined: Oct 5, 2012
October 9th, 2012 at 3:19:14 AM permalink
That 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 :)
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
October 9th, 2012 at 9:18:12 AM permalink
Quote: backprop

That 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.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
AcesAndEights
AcesAndEights
  • Threads: 67
  • Posts: 4300
Joined: Jan 5, 2012
October 9th, 2012 at 5:17:40 PM permalink
Quote: MathExtremist

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.


emacs ftw
"So drink gamble eat f***, because one day you will be dust." -ontariodealer
AcesAndEights
AcesAndEights
  • Threads: 67
  • Posts: 4300
Joined: Jan 5, 2012
October 9th, 2012 at 5:17:55 PM permalink
Quote: MathExtremist

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.


Ah I didn't notice that.
"So drink gamble eat f***, because one day you will be dust." -ontariodealer
ALFERALFER
ALFERALFER
  • Threads: 1
  • Posts: 20
Joined: Nov 15, 2011
October 10th, 2012 at 12:22:37 PM permalink
Quote: ThatDonGuy

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.






bc
scale=99
a=(581103*15647406142778942242813) / (4194304*625*184877*37349*11191*65231)
a
0.688130417075903792314368434720157072863971135103004607340044931959853168119832890833389191020411554
  • Jump to: