Drug Half Life calculator with PHP - php

I have found an excel file online that helps with the calculations of a Drugs Half-life and helps to determine how much of the given drug is likely to remain in ones system based on
Hal-life Hour Number
The quantity of the Drug taken per dose
how much is in your system from previous doses
Below is a screenshot of the Excel file showing both the Output with the calculations already performed and also shows the actual Math that is involved for each day...
The Columns A, B, C, D, E, etc.. is the Day 24 hours
Column D Row 6 is the Half life for a Drug in Hours
From the Image below you can see that the calculation is perfromed and that Value is then used in the Next Day's equation
Ok so I am not that knowledgeable with Math outside of basic Addition, Subtraction, Multiplication, and Division I do not know much more then that.
My goal is to create a tool similar to this Excel file but with PHP, I am not sure how to do so but I think all the answers are right here in the image above as far as the math portion.
Looking at D3 I can see that it takes...
D2's value which is 30 in the Image
It then Adds C3's value
Then Multiplies that by 1/2
I am not sure what the ^ does though?
Then it Divides 24 hours by D6 which holds the Hour Number
In my PHP I would like to have a Function that I can pass an array of Data, so let's say I pass in an Array with...
the Number of Days to calculate (my image shows like 4 days so if I pass 10 days, it will shows the daily results up to 10 days)
Then an array of the Daily amount consumed in mg (so in my image this would be an array with 30,0,30,0,0,0,0
Then I would also pass in the Half Life in Hours, so in my image the drug used has a half-life of 4.5 hours
This function would then return an array with the data for each day, should show the remaining mg in one's system for each day, I can then use this data result to build charts, graphs, or simply a List
I would appreciate any help to get me started, I think I can pull this off on my own but I need help getting the math portion 100%, above I break down the equation as I see it, please help me understand better for example I am not sure what ^ does in the equation or how to do it in PHP
I hope my question is not too vague, I will come back with more specific once I get a good start on this but please help if you can so far, thank you for reading.

function calcHalfLife( $mgTaken , $drugHalfLifeHours , $day ) {
//total number of half-lifes elapsed
$total_half_lifes = ($day * 24) / $drugHalfLifeHours;
//total reduction in dosage
$reductionFactor = pow( 0.5 , $total_half_lifes );
//return the current dosage in the person's system
return round( $mgTaken * $reductionFactor , 2 );
}
The above function should do the trick. Pass in the MG dosage taken, the number of hours for that drug's half-life, and the number of days since the dosage.
The function calculates the number of half-lifes experienced by the drug by taking the number of days * 24 hours in a day and dividing that by the total number of hours it takes for a half-life of that drug. This is the number of times the drug's dosage would be cut in half.
It then takes 0.5 (50% as a decimal) and increments it to the power of the total number of half lifes experienced by the drug. So 1 half life would be 0.5, 2 would be 0.5 * 0.5 = 0.25 etc etc. This is the decimal representation of the percentage of the drug left in the person's system.
It then multiplies that remainder by the original amount taken and rounds it off to 2 decimal places. The return floating point value will be a representation of the remaining MG dosage of the drug in the person's system.
If you're looking to build a function/system that allows you to calculate a daily dosage of that drug (ie: the person takes the same dosage each day as opposed to once) that is a very different formula and function, but the basic principle is the same as the one I wrote for you here.
Good luck ;)

Related

Probabilities Function -

This is a bit of a math question, and cause am quite weak at math [ :( ] I can't figure this out
I have an application that must "randomly" decide if you won or not with a maximum daily winners, the problem is that i don't want to do a simple x chance of winning cause this might result in 20 people winning at the start of the day, and then everyone will keep losing, is there a generic formula to do this?
tl;dr
I have x amount of Gifts (x=20)
The user must know immediately if he won or not (can't do it at the end of the day)
And I want to randomly spread them throughout the day, is there a generic function/script?
After some suggestions in the comments, I could settle with either,
a solution that takes a predictable number of daily contestants (i ll just have a random guess for the first few days and change it accordingly)
a solution considering the time of the day, the gifts won so far, and the remaining gifts
Any ideas?
There is no math question here, not really, just some decisions that you need to make.
One possibility is to make the probability of winning be X/N where N is the expected number of visitors, until the gifts run out for that day. It is random, so it might be the case that on some days the gifts exhaust early. So what? That is how probability works. Extreme imbalances are unlikely. For example, say you have 20 gifts and 1000 visitors on an average day. The probability that the gifts will be exhausted by the 500th visitor is a binomial probability: the probability of having at least 20 successes in 500 trials where the probability of success is 20/1000 = 0.02. This probability works out to be just 0.003.
On days when there are unclaimed gifts -- increase the gift count for the next day and correspondingly increase the probability of winning. If you spin it the right way, this could increase interest in the game in sort of the same way that people buy more lottery tickets on days when a jackpot goes unclaimed.
Note that essentially the same idea can be implemented on different time resolutions. For example, use 4-hour time slots in place of whole days (with X and N adjusted accordingly). This will guarantee a more even spread of the gifts throughout the day (but to pull it off you might need to take into account that the expected number of visitors in a 4-hour time slot is unlikely to be constant over the course of a day. Different time slots might need different denominators).

How to calculate luck factor based on bet winchance/result?

I am working on a website where users can bet on events with variable win chance. One of the rquests is to display the "Luck factor" of a certain user, based on his bets.
Here is the definition of the Luck factor:
The luck percentage displayed shows how many bets you have won compared to how many you 'should' have won. For example, if you play 10 times with a 10% chance of winning and win two of the 10 bets, your luck will show as 200%, since you have won twice as many as you 'should' have. Bet size is not taken into account when calculating luck, so it is possible to have a luck less than 100% and still show a profit if your winning bets risked more than your losing bets.
Here is my (MySQL) database structure:
Table bet
Columns:
winchance (0.01 - 99.99)
win (true/false)
The application is written in php, but I am sure a pseudocode example would push me to the right direction.
If I understand your question, right, You can take the average of winning probability using mysql winchance column and real winning ratio, would be (number of wins / total number of bets). Given these two values, luck factor would be real ratio / winchance avg * 100.
For instance, avg win chance is 0.1 and real winning ratio, is 2 / 10 = 0.2, then luck factor is 0.2/0.1 * 100 = 200%. This shall be easily calculated with mysql inbuilt functions itself.

clever way to increment a value in a large array

Hep hey!
I am building an statistic overview of how many people is supposed to be at work at any given 5 minuts interval on a given day.
Say, we have 6 people working at 10.50, same at 10.55, then one go home and we got 5 people working at 11.00
Now, the way i imagined to keep track of this was to have an array with 5x12x24 elements (1 element per 5 minuts for an 24 hour interval), where i run through each employees shift time and increment the elements for the given 5 min intervals their shift takes them over.
(say a person works from 9.00 to 10.00, then i will increment the values from 9.00, 9.05, 9.10 up to 10.00 by one)
I need the data to make a diagram later, that is why i store it in an array.
Now my question is, which way is the fastest to do this?
Should i start out with an array which contains all the time elements and then increment it as i run through the shift hours of the employees ($arr['9.05']++) or should i start out by making an empty array and just check if the value of the time exsists, if not, create that element and if it does, increment it?
Or is there in general a smarter way to do this?
I ask as i can see this becomming a pretty heavy operation if you have 50+ employees which have to run through this function, so the smarter it can be made, the better :)
PS. the shift times comes from a database that i do not have access to, so i only have the timestamps of the start of the shit and the finish.

Making more recent items more likely to be drawn

There are a few hundred of book records in the database and each record has a publish time. In the homepage of the website, I am required to write some codes to randomly pick 10 books and put them there. The requirement is that newer books need to have higher chances of getting displayed.
Since the time is an integer, I am thinking like this to calculate the probability for each book:
Probability of a book to be drawn = (current time - publish time of the book) / ((current time - publish time of the book1) + (current time - publish time of the book1) + ... (current time - publish time of the bookn))
After a book is drawn, the next round of the loop will minus the (current time - publish time of the book) from the denominator and recalculate the probability for each of the remaining books, the loop continues until 10 books have been drawn.
Is this algorithm a correct one?
By the way, the website is written in PHP.
Feel free to suggest some PHP codes if you have a better algorithm in your mind.
Many thanks to you all.
Here's a very similar question that may help: Random weighted choice The solution is in C# but the code is very readable and close to PHP syntax so it should be easy to adapt.
For example, here's how one could do this in MySQL:
First calculate the total age of all books and store it in a MySQL user variable:
SELECT SUM(TO_DAYS(CURDATE())-TO_DAYS(publish_date)) FROM books INTO #total;
Then choose books randomly, weighted by their age:
SELECT book_id FROM (
SELECT book_id, TO_DAYS(CURDATE())-TO_DAYS(publish_date) AS age FROM books
) b
WHERE book_id NOT IN (...list of book_ids chosen so far...)
AND RAND()*#total < b.age AND (#total:=#total-b.age)
ORDER BY b.publish_date DESC
LIMIT 10;
Note that the #total decreases only if a book has passed the random-selection test, because of short-circuiting of AND expressions.
This is not guaranteed to choose 10 books in one pass -- it's not even guaranteed to choose any books on a given pass. So you have to re-run the second step until you've found 10 books. The #total variable retains its decreased value so you don't have to recalculate it.
First off I think your formula will guarantee that earlier books get picked. Try to set your initial probabilities based on:
Age - days since publication
Max(Age) - oldest book in the sample
Book Age(i) - age of book i
... Prob (i) = [Max (age) + e - Book Age (i)] / sum over all i [ Max (age) + e - Book age(i) ]
The value e ensures that the oldest book has some probability of being selected. Now that that is done, you can always recalc the prob of any sample.
Now you have to find an UNBIASED way of picking books. Probably the best way would be to calculate the cumulative distribution using the above then pick a uniform (0,1) r.v. Find where that r.v. is in the cumulative distribution and pick the book nearest to it.
Can't help you on the coding. Make sense?

Determine Click-thru Percentage With PHP

I am currently working on a small sponsorship application(PHP/MySql) for my personal blog, and am almost finish, but I am stuck on how to calculate the click-thru rate of my sponsors campaigns.
I was always terrible with working out percentages, so any practical help would be appreciated. The data is stored in the DB as simple numbers.. So as expected, when a page refreshes, or a sponsors ad is clicked, the data updates with an incrementation of 1.
So using these values...say $clicks and $impressions, how would I determine the click-thru rate? What would be the sum I would use to calculate? An example function would really be appreciated.
Kind Regards, Lea
What you're after is the percentage of $impressions that leads to $clicks. The ratio is found by calculating $clicks/$impressions, and then you can multiply by 100 to see the percentage.
As an example, if there are 100 impressions and 1 click, the ratio will be 1/100 = 0.01, and the percentage will be 0.01 * 100 = 1%.

Categories