Last active
March 6, 2017 19:40
-
-
Save adamashton/408b91aaac472dcd90faf58cfa645015 to your computer and use it in GitHub Desktop.
SameBirthday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MonteCarlo | |
{ | |
class Program | |
{ | |
private static Random r = new Random(); | |
private static readonly double GestationPeriodMean = 280.6; | |
private static readonly double GestationPeriodStandardDeviation = 9.7; | |
private static readonly int Simulations = 10000000; | |
static void Main(string[] args) | |
{ | |
double conceptionDifferenceDelta = 0; | |
bool sharedBirthday = true; | |
while (sharedBirthday) | |
{ | |
var sameDayBirths = GetSameDayBirthCount(conceptionDifferenceDelta); | |
Console.WriteLine(string.Format("ConceptionDateDiff:{0} {1:P2}", conceptionDifferenceDelta, (double)sameDayBirths / Simulations)); | |
sharedBirthday = sameDayBirths > 0; | |
conceptionDifferenceDelta += 1; | |
} | |
} | |
private static int GetSameDayBirthCount(double conceptionDifferenceDelta) | |
{ | |
int sameDayBirths = 0; | |
for (int i = 0; i < Simulations; i++) | |
{ | |
double birthDateFirst = getBirthDate(); | |
double birthDateSecond = getBirthDate(); | |
// move the conception date of second by conceptionDifferenceDelta days | |
birthDateSecond += conceptionDifferenceDelta; | |
if ((int) birthDateFirst == (int) birthDateSecond) | |
sameDayBirths++; | |
} | |
return sameDayBirths; | |
} | |
private static double getBirthDate() | |
{ | |
// Box-Muller transform - http://stackoverflow.com/a/218600/3006667 | |
double u1 = 1.0 - r.NextDouble(); //uniform(0,1] random doubles | |
double u2 = 1.0 - r.NextDouble(); | |
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * | |
Math.Sin(2.0 * Math.PI * u2); //random normal(0,1) | |
double result = GestationPeriodMean + GestationPeriodStandardDeviation * randStdNormal; //random normal(mean,stdDev^2) | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ConceptionDateDiff:0 2.91 % | |
ConceptionDateDiff:1 2.90 % | |
ConceptionDateDiff:2 2.88 % | |
ConceptionDateDiff:3 2.84 % | |
ConceptionDateDiff:4 2.79 % | |
ConceptionDateDiff:5 2.73 % | |
ConceptionDateDiff:6 2.64 % | |
ConceptionDateDiff:7 2.55 % | |
ConceptionDateDiff:8 2.44 % | |
ConceptionDateDiff:9 2.35 % | |
ConceptionDateDiff:10 2.24 % | |
ConceptionDateDiff:11 2.11 % | |
ConceptionDateDiff:12 1.99 % | |
ConceptionDateDiff:13 1.85 % | |
ConceptionDateDiff:14 1.73 % | |
ConceptionDateDiff:15 1.59 % | |
ConceptionDateDiff:16 1.47 % | |
ConceptionDateDiff:17 1.35 % | |
ConceptionDateDiff:18 1.23 % | |
ConceptionDateDiff:19 1.12 % | |
ConceptionDateDiff:20 1.01 % | |
ConceptionDateDiff:21 0.90 % | |
ConceptionDateDiff:22 0.80 % | |
ConceptionDateDiff:23 0.72 % | |
ConceptionDateDiff:24 0.63 % | |
ConceptionDateDiff:25 0.55 % | |
ConceptionDateDiff:26 0.48 % | |
ConceptionDateDiff:27 0.42 % | |
ConceptionDateDiff:28 0.36 % | |
ConceptionDateDiff:29 0.31 % | |
ConceptionDateDiff:30 0.27 % |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The length of human pregnancy as calculated by ultrasonographic measurement of the fetal biparietal diameter (H. Kieler, O. Axelsson, S. Nilsson, U. Waldenströ) has a very nice table of 10 published studies, which found various means between 272-283 days. The authors contribute their own results as well, recording over 800 mothers who went into labor spontaneously. When pregnancy length was calculated using ultrasound in the second trimester the mean was 280.6 days, standard deviation 9.7 days. When it was calculated using LMP, the mean was 283.6 days with a standard deviation of 10.5 days.