View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.simulation.calculation.deterministic;
17  
18  import p3j.experiment.results.BasicResults;
19  import p3j.misc.math.Matrix2D;
20  import p3j.simulation.calculation.deterministic.parameters.InFlowParameters;
21  
22  /**
23   * Calculation regarding the sub-populations with an in-flow matrix.
24   * 
25   * Created on July 22, 2006
26   * 
27   * @author Christina Bohk
28   * @author Roland Ewald
29   * 
30   */
31  public class InFlowPopulation extends
32      AbstractPopulation<InFlowParameters, BasicResults> {
33  
34    @Override
35    public BasicResults calculatePopulation(String subPopName, int generation,
36        InFlowParameters parameters) {
37  
38      if (generation != 0)
39        throw new IllegalArgumentException(
40            "Only supports generation-0 populations");
41  
42      BasicResults results = new BasicResults(subPopName, generation,
43          parameters.getNumOfYears(), parameters.getMaxAge());
44  
45      calculateSurvivalProbabilities(parameters, results);
46  
47      // Calculate rest:
48  
49      // ...for females
50      calculateMeanAndEndPopulation(results.getMeanXf(), results.getEndXf(),
51          parameters.getMigrantsXf(), parameters.getSurviveProbO100f(),
52          results.getP1f(), results.getP2f(), parameters.getNumOfYears(),
53          parameters.getMaxAge());
54  
55      // ...for males
56      calculateMeanAndEndPopulation(results.getMeanXm(), results.getEndXm(),
57          parameters.getMigrantsXm(), parameters.getSurviveProbO100m(),
58          results.getP1m(), results.getP2m(), parameters.getNumOfYears(),
59          parameters.getMaxAge());
60  
61      return results;
62    }
63  
64    /**
65     * Calculates mean and end populations.
66     * 
67     * @param meanPopulation
68     *          mean population (will be filled)
69     * @param endPopulation
70     *          end population (will be filled)
71     * @param migrants
72     *          the mirgants
73     * @param surviveProbO100
74     *          survival probability of the over-100-years-olds
75     * @param p1
76     *          the P_1 matrix (infant survival probability first half of first
77     *          year)
78     * @param p2
79     *          the P_2 matrix (infant survival probability second half of first
80     *          year)
81     * @param numberOfYears
82     *          number of years to be predicted
83     * @param maximumAge
84     *          the maximum age class
85     */
86    protected static void calculateMeanAndEndPopulation(Matrix2D meanPopulation,
87        Matrix2D endPopulation, Matrix2D migrants, Matrix2D surviveProbO100,
88        Matrix2D p1, Matrix2D p2, int numberOfYears, int maximumAge) {
89  
90      for (int year = 1; year < numberOfYears; year++) {
91        for (int age = 0; age < maximumAge + 1; age++) {
92  
93          // Calculate mean population
94          if (age == maximumAge) {
95            meanPopulation.setQuick(
96                age,
97                year,
98                endPopulation.getQuick(age - 1, year - 1)
99                    * p2.getQuick(age - 1, year - 1)
100                   + (migrants.getQuick(year, age) / 2)
101                   * surviveProbO100.getQuick(year, 0));
102         } else {
103           switch (age) {
104           case 0:
105             meanPopulation.setQuick(age, year, 0);
106             break;
107           case 1:
108             meanPopulation.setQuick(
109                 age,
110                 year,
111                 (migrants.getQuick(year - 1, age - 1) + migrants.getQuick(year,
112                     age) / 2) * p2.getQuick(age - 1, year - 1));
113             break;
114           default:
115             meanPopulation.setQuick(
116                 age,
117                 year,
118                 (migrants.getQuick(year, age) / 2 + endPopulation.getQuick(
119                     age - 1, year - 1)) * p2.getQuick(age - 1, year - 1));
120             break;
121           }
122         }
123 
124         // Calculate end population
125         if (age == maximumAge) {
126           endPopulation.setQuick(
127               age,
128               year,
129               meanPopulation.getQuick(age, year)
130                   * surviveProbO100.getQuick(year, 0)
131                   + migrants.getQuick(year, age) / 2);
132 
133         } else {
134           switch (age) {
135           case 0:
136             endPopulation.setQuick(age, year,
137                 meanPopulation.getQuick(age, year) * p1.getQuick(age, year)
138                     + migrants.getQuick(year, age));
139             break;
140           default:
141             endPopulation.setQuick(age, year,
142                 meanPopulation.getQuick(age, year) * p1.getQuick(age, year)
143                     + migrants.getQuick(year, age) / 2);
144             break;
145           }
146         }
147       }
148     }
149   }
150 }