public static void Sample(object caller, float scale, string year, int gender, IList <double> source, IList <double> weights, string title, string xlabel = "age", string ylabel = "population", [CallerMemberName] string callerName = "") { var env = new SimSharp.ThreadSafeSimulation(42); var length = source.Count; var total = 0; var simulatedTotal = 0; var cbsPoints = new List <DataPoint>(length); for (int i = 0; i < length; i++) { total += (int)weights[i]; simulatedTotal += (int)(weights[i] * scale); cbsPoints.Add(new DataPoint(i, (int)(weights[i] * scale))); } int[] data = new int[length]; // Sample some probability data, based on scale of total. for (var s = 0; s < total * scale; s++) { data[(int)env.RandChoice(source, weights)]++; } var points = new List <DataPoint>(length); for (int j = 0; j < data.Length; j++) { points.Add(new DataPoint(j, data[j])); } // Assert (through visual inspection) var model = new PlotModel { Title = $"{title} {year} - {total} citizens {Constants.DisplayNames[gender]} scale {100 * scale}% " }; model.DefaultColors = new List <OxyColor> { OxyColors.Silver, OxyColors.Black }; model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = xlabel }); model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = ylabel }); model.Series.Add(new LineSeries { ItemsSource = points, StrokeThickness = 4 }); model.Series.Add(new LineSeries { ItemsSource = cbsPoints, StrokeThickness = 1 }); model.Series[0].Title = "simulated"; model.Series[1].Title = "actual"; Helpers.SvgWriter(model, caller, $"{year}-{total}-{Constants.DisplayNames[gender]}-scale-{scale}", new[] { points, cbsPoints }, callerName); }
// TODO: This probability data set does not contain years. Year not specified. public void MaritalAge(float scale, int startDate, int endDate, int gender) { var env = new SimSharp.ThreadSafeSimulation(42); for (int y = startDate; y <= endDate; y++) { var index = MaritalStatusProbability.YearIndex(y); Helpers.Sample(this, scale, y.ToString(), gender, MaritalStatusProbability.MaritalAgeSource, MaritalStatusProbability.MaritalAgeWeights[gender, index], "Marital Age distribution" ); } }
public void AgeDistribution(float scale, int startYear, int endYear, int gender) { // Arrange var env = new SimSharp.ThreadSafeSimulation(42); for (int y = startYear; y <= endYear; y++) { var index = AgeProbability.YearIndex(y); Helpers.Sample(this, scale, y.ToString(), gender, AgeProbability.Source[gender, index], AgeProbability.Weights[gender, index], "Age distribution" ); } }
public static void MaritalDuration1() { //Arrange var env = new SimSharp.ThreadSafeSimulation(42); var collection = new MaritalDurationCollection(); var coupleList = new List <double>(); //Act //The married couples are randomly assigned into a marital duration list, using the duration weights given in the "MaritalDuration" class for (int i = 0; i < 3217582; i++) { coupleList.Add(env.RandChoice(DivorceProbability.DivorceRateSource, DivorceProbability.DivorceRateWeights)); } //The couples are sorted into a collection based on their i variable. // i is in this case the years someone is married, the duration. for (double i = 0; i < 72; i++) { collection.Add(new MaritalDurationProbability() { Duration = i, MarriedDurationCouples = coupleList.Where(d => d == i).Count() }); } //Collection is exported into a CSV file saved in the location below. var export = Frame.FromRecords(collection); export.SaveCsv($"{Global.GetDataFolder()}maritalDurationList.csv"); //Assert //We took 3 random durations from the list and we asserted them to see if they are //really the same number each time. //We tested it to 5 years, 31 years and, 54 years. int actualDuration5 = coupleList.Where(d => d == 5).Count(); int expectedDuration5 = 59944; int actualDuration54 = coupleList.Where(d => d == 54).Count(); int expectedDuration54 = 34631; int actualDuration31 = coupleList.Where(d => d == 31).Count(); int expectedDuration31 = 53246; Assert.Equal(expectedDuration5, actualDuration5); Assert.Equal(expectedDuration31, actualDuration31); Assert.Equal(expectedDuration54, actualDuration54); }
public static void ReadFile() { // Arrange Frame <int, string> frame; List <double> weights; var collection = new MaritalStatusProbabilityCollection(); frame = Frame.ReadCsv("../../../../../doc/data/marital_status.csv"); var env = new SimSharp.ThreadSafeSimulation(42); // Act //For the years 1950 to 2019 we are going to put a test amount of people through the weights and see what happens for (int j = 1950; j < 2020; j++) { //A list partnertype made, where we will put everyone in. var people = new List <PartnerType>(); //Weights are decided from "marital_status.csv", each year takes their own amount of weights. weights = frame.GetColumn <double>(Convert.ToString(j)).Values.Select(c => Convert.ToDouble(c)).Take(3).ToList(); MaritalStatus.Weights = weights; //For 100000 citizens of each year, they each get randomly sorted in a source, based on the //weights given above. for (int i = 0; i < 100000; i++) { people.Add(env.RandChoice(MaritalStatus.Source, MaritalStatus.Weights)); } //Here we are added all of them into a collection based on the MaritalStatusPorbability class which can be viewed at the top. collection.Add(new MaritalStatusProbability() { Singles = people.Where(p => p == PartnerType.Single).Count(), Married = people.Where(p => p == PartnerType.Married).Count(), Partnership = people.Where(p => p == PartnerType.Partnership).Count(), Year = j }); } //This gets exported into a CSV file which can be located as stated below. var export = Frame.FromRecords(collection); export.SaveCsv($"{Global.GetDataFolder()}maritalStatusWeights.csv"); }
public void ChildrenProbabilities() { //Arrange var env = new SimSharp.ThreadSafeSimulation(42); Frame <int, string> frame; frame = Frame.ReadCsv($"{Global.GetDataFolder()}maritalAgeProbability.csv"); var womenAges = frame.GetColumn <double>("MarriedWomen").Values.Select(c => Convert.ToDouble(c)).ToList(); Frame <int, string> MotherData = Frame.ReadCsv("../../../../../doc/data/motherbirthing_ages.csv"); List <double> weights; List <double> childChoice; var childAmount = new Dictionary <int, List <double> >(); var collection = new ChildrenProbabilityCollection(); var collectionNew = new ChildrenProbabilityCollection(); //Act //Here the mothers are randomly assigned to a designated amount of children. //The reults are first put in a list and after that they get sorted. for (int i = 0; i < 32; i++) { childAmount.Add(i, new List <double>()); childChoice = new List <double>(); int age = i + 18; weights = MotherData.GetColumn <double>(Convert.ToString(age)).Values.Select(c => Convert.ToDouble(c)).ToList(); for (int j = 0; j < womenAges[i]; j++) { Children.WeightGetChildren = weights; childChoice.Add(env.RandChoice(Children.SourceGetChildren, Children.WeightGetChildren)); } var children = childChoice.Where(p => p == 1).Count(); //childAmount = new List<double>(); for (int j = 0; j < children; j++) { childAmount[i].Add(env.RandChoice(Children.SourceAmountChildren, Children.WeightsAmountChildren)); } //Here they are getting sorted and added to a new collection collectionNew.Add(new ChildrenProbability() { Age = age, childrenAmount = { } //WomenNoChildren = childChoice.Where(p => p == 0).Count(), //MotherOfOneAgeFirstBorn = childAmount[i].Where(p => p == 1).Count(), //MotherOfTwoAgeFirtBorn = childAmount[i].Where(p => p == 2).Count(), //MotherOfThreeAgeFirstBorn = childAmount[i].Where(p => p == 3).Count(), //BirthingMothersTotal = children }); } //Both collections get exported out var exportNew = Frame.FromRecords(collectionNew); var export = Frame.FromRecords(collection); exportNew.SaveCsv($"{Global.GetDataFolder()}childrenProbabilityDifferent.csv"); //Assert int expectedFirstChildren34 = 2564; int actualFirstChildren34 = childAmount[16].Where(p => p == 1).Count(); Assert.Equal(expectedFirstChildren34, actualFirstChildren34); int expectedSecondChildren18 = 6; int actualSecondChildren18 = childAmount[0].Where(p => p == 2).Count(); Assert.Equal(expectedSecondChildren18, actualSecondChildren18); int expectedThirdChildren43 = 73; int actualThirdChildren43 = childAmount[25].Where(p => p == 3).Count(); Assert.Equal(expectedThirdChildren43, actualThirdChildren43); }
public void MaritalAgePropability() { //Arrange var env = new SimSharp.ThreadSafeSimulation(42); Frame <int, string> frame; frame = Frame.ReadCsv("../../../../../doc/data/MaritalAgeWeights.csv"); List <double> femaleWeights; List <double> maleWeights; var womenList = new Dictionary <int, Dictionary <PartnerType, int> >(); var menList = new Dictionary <int, Dictionary <PartnerType, int> >(); var collection = new MaritalAgePropabilityCollection(); var femaleAge = Age.FemaleWeights; var maleAge = Age.MaleWeights; //Act //In here we decide what marital status someone has at a certain age. //In our case it goes from 18 to 99 years old. for (int i = 18; i < 100; i++) { //We made a dictionary for both men and women. womenList.Add(i, new Dictionary <PartnerType, int>()); womenList[i].Add(PartnerType.Single, 0); womenList[i].Add(PartnerType.Married, 0); womenList[i].Add(PartnerType.Partnership, 0); menList.Add(i, new Dictionary <PartnerType, int>()); menList[i].Add(PartnerType.Single, 0); menList[i].Add(PartnerType.Married, 0); menList[i].Add(PartnerType.Partnership, 0); maleWeights = frame.GetColumn <double>(Convert.ToString(i)).Values.Select(c => Convert.ToDouble(c)).Take(3).ToList(); femaleWeights = frame.GetColumn <double>(Convert.ToString(i)).Values.Select(c => Convert.ToDouble(c)).Skip(3).Take(3).ToList(); //Female age is all the predictated ages of females in 2020. //This can be found in the probability class "Age". //First they get the weights from their age group. And with those weights deciding the results in a randomchoice, they get put into their dictionary. for (int j = 0; j < femaleAge[i]; j++) { MaritalStatus.Weights = femaleWeights; womenList[i][env.RandChoice(MaritalStatus.Source, MaritalStatus.Weights)]++; } //The exact same happens as stated above in women. for (int j = 0; j < maleAge[i]; j++) { MaritalStatus.Weights = maleWeights; menList[i][env.RandChoice(MaritalStatus.Source, MaritalStatus.Weights)]++; } //In here we sort everything into their respective classes. //i here is yet again representing an age group. collection.Add(new MaritalAgeProbability() { SingleWomen = womenList[i][PartnerType.Single], MarriedWomen = womenList[i][PartnerType.Married], DivorcedWomen = womenList[i][PartnerType.Partnership], SingleMen = womenList[i][PartnerType.Single], MarriedMen = womenList[i][PartnerType.Married], DivorcedMen = womenList[i][PartnerType.Partnership], Age = i }); } //Putting the collection into a CSV which can be found in the location stated below. var export = Frame.FromRecords(collection); export.SaveCsv($"{Global.GetDataFolder()}maritalAgeProbability.csv"); //Assert //We asserted a few ages to see if the test would keep on the same results as before. //For women we used ages 18, 36 and 98 as for men we used 30, 57 and 67. Assert.Equal(42679, womenList[36][PartnerType.Single]); Assert.Equal(92, womenList[18][PartnerType.Married]); Assert.Equal(741, womenList[98][PartnerType.Partnership]); Assert.Equal(9900, menList[67][PartnerType.Single]); Assert.Equal(81637, menList[57][PartnerType.Married]); Assert.Equal(1603, menList[30][PartnerType.Partnership]); }