src/de/pik/lagom/generic/Government.java

00001 
00002 // Copyright 2010 by Carlo Jaeger, Antoine Mandel, Steffen Fuerst and European Climate Forum
00003 // Licensed under the Open Software License version 3.0
00004 // See the file "License-OSL-3.0.txt" in the distribution for more information
00005 // The License text can be also found under http://www.opensource.org/licenses/osl-3.0.php
00007 
00008 package de.pik.lagom.generic;
00009 
00010 import static de.pik.lagom.annotations.Initialization.INDIVIDUAL;
00011 import static de.pik.lagom.annotations.Origin.*;
00012 import static de.pik.lagom.annotations.Variability.*;
00013 
00014 import net.sf.oval.constraint.NotNegative;
00015 import net.sf.oval.constraint.NotNull;
00016 import net.sf.oval.guard.Guarded;
00017 import net.sf.oval.guard.PostValidateThis;
00018 import net.sf.oval.guard.Pre;
00019 import de.pik.lagom.annotations.Description;
00020 import de.pik.lagom.annotations.NameInUI;
00021 import de.pik.lagom.annotations.WriteToFile;
00022 import de.pik.lagom.annotations.Initialization;
00023 import de.pik.lagom.annotations.Origin;
00024 import de.pik.lagom.annotations.Variability;
00025 import de.pik.lagom.annotations.NotDocumented;
00026 import de.pik.lagom.generic.initvalues.GovernmentInitValues;
00027 import de.pik.lagom.toolbox.ProbeBase;
00028 import de.pik.lagom.toolbox.StepManager;
00029 import de.pik.lagom.toolbox.StepManager.Callback;
00030 import de.pik.lagom.toolbox.math.FloatMath;
00031 
00033 @Guarded
00034 @Initialization(INDIVIDUAL)
00035 public class Government {
00036   public class Probe extends ProbeBase {
00037     @WriteToFile
00038     @NameInUI("Tax Rate")
00039     public double getTaxRate() {
00040       return taxRate;
00041     }
00042 
00043     @WriteToFile
00044     @NameInUI("Unemployment Wage")
00045     public double getUnemploymentWage() {
00046       return unemploymentWage;
00047     }
00048 
00049     @WriteToFile
00050     @NameInUI("Average Wage")
00051     @Description("The average wage of all work contract in the economy")
00052     public double getAverageWage() {
00053       return averageWage;
00054     }
00055 
00056     @WriteToFile
00057     @NameInUI("Average Wage Reference")
00058     @Description("The average wage reference of the firms")
00059     public double getAverageWageReference() {
00060       return stats.getAverageWageReference();
00061     }
00062 
00063 
00064     @WriteToFile
00065     @NameInUI("Std Deviation Wage Reference")
00066     @Description("The standard deviation of wage references across firms")
00067     public double getStdDeviationWageReference() {
00068       return stats.getDeviationWageReference();
00069     }
00070   
00071     
00072     @WriteToFile
00073     @NameInUI("Average Fallback")
00074     @Description("The average fallback of the households")
00075     public double getAverageFallback() {
00076       return stats.averageFallback;
00077     }
00078 
00079     @WriteToFile
00080     @NameInUI("Minimal WageReference")
00081     @Description("The minimal wage reference among firms")
00082     public double getMinWageReference() {
00083       return stats.minWageReference;
00084     }
00085     
00086     @WriteToFile
00087     @NameInUI("Maximum Fallback")
00088     @Description("The maximum fallback among households")
00089     public double getMaxFallback() {
00090       return stats.maxFallback;
00091     }
00092     
00093     @WriteToFile
00094     @NameInUI("Unemployment Rate")
00095     public double getUnemploymentRate() {
00096       return unemploymentRate;
00097     }
00098 
00099     @WriteToFile
00100     @NameInUI("Average Income")
00101     @Description("The average income (wage, unemployment wage, dividend and interest) of all households in the economy")
00102     public double getAverageIncome() {
00103       return averageIncome;
00104     }
00105 
00106     @Override
00107     public String toString() {
00108       return "Government";
00109     }
00110   }
00111 
00112   class Stats {
00113     @Guarded
00114     public class StepCallback extends Callback {
00115       @Override
00116       public Object getOwner() {
00117         return Government.Stats.this;
00118       }
00119     }
00120 
00121     @NotNegative
00122     @Description("The average wage reference ")
00123     @Origin(TECHNICAL)
00124     @Variability(PERIOD)  
00125     private double weightedAverageWageReference;
00126 
00127     @NotNegative
00128     @Description("The standard deviation of wage reference ")
00129     @Origin(TECHNICAL)
00130     @Variability(PERIOD)  
00131     private double deviationWageReference;
00132 
00133     
00134     @NotNegative
00135     @Description("The minimal wage reference")
00136     @Origin(TECHNICAL)
00137     @Variability(PERIOD)
00138     private double minWageReference;
00139     
00140     @NotNegative
00141     @Description("The average fallback")
00142     @Origin(TECHNICAL)
00143     @Variability(PERIOD)
00144     private double averageFallback;
00145     
00146     @NotNegative
00147     @Description("The maximal fallback")
00148     @Origin(TECHNICAL)
00149     @Variability(PERIOD)    
00150     private double maxFallback; 
00151 
00152     void init(StepManager pStepManager) {
00153       pStepManager.registerCallback(Foundation.FIRMS_ADJUST_WORKFORCE_STEP,
00154                                     StepManager.Timing.POST,
00155                                     1,
00156                                     new StepCallback() {
00157         @Override
00158         public void postStep() {
00159           averageWage = 0;
00160           weightedAverageWageReference=0;
00161           minWageReference=Float.MAX_VALUE;
00162           maxFallback=0;
00163           averageFallback=0;
00164           double nFirm=0;
00165           double lTotalTargetEmployment=0;
00166           double lAverageWageReference=0;
00167           double lOldAverageWageReference;
00168           deviationWageReference=0;
00169           for (final Firm lFirm : foundation.generateFirmList()) {
00170             nFirm++;
00171             weightedAverageWageReference += lFirm.getWageReference()*lFirm.getTargetEmployment();
00172             lOldAverageWageReference=lAverageWageReference;
00173             lAverageWageReference=((nFirm-1)*lAverageWageReference +lFirm.getWageReference())/nFirm; 
00174             deviationWageReference=((nFirm-1)*deviationWageReference+
00175                 (lFirm.getWageReference()-lAverageWageReference)*
00176                 (lFirm.getWageReference()-lOldAverageWageReference))/nFirm;
00177             assert(deviationWageReference<10000);
00178             minWageReference =Math.min(minWageReference, lFirm.getWageReference());
00179             lTotalTargetEmployment += lFirm.getTargetEmployment();
00180           }
00181           weightedAverageWageReference /=   lTotalTargetEmployment;
00182           deviationWageReference=Math.sqrt(deviationWageReference);
00183 
00184           for (final Household lHousehold : foundation.getHouseholds()) {
00185             averageFallback+= lHousehold.getUnemploymentAdjustedFallback();
00186             maxFallback = Math.max(maxFallback, lHousehold.getUnemploymentAdjustedFallback());
00187           }
00188           averageFallback /= foundation.getNumHouseholdsTotal();
00189         }
00190       }
00191       );
00192     }
00193 
00194     public double getAverageWageReference() {
00195       return weightedAverageWageReference;
00196     }
00197 
00198     public double getDeviationWageReference() {
00199       return deviationWageReference;
00200     }
00201 
00202     public double getMinWageReference() {
00203       return minWageReference;
00204     }
00205 
00206     public double getAverageFallback() {
00207       return averageFallback;
00208     }
00209 
00210     public double getMaxFallback() {
00211       return maxFallback;
00212     }
00213   }
00214   
00215   /*
00216   @Description("The probe of government")
00217   @Origin(TECHNICAL)
00218   @Variability("?")
00219   */
00220   @NotDocumented
00221   private final Government.Probe probe = new Probe();
00222 
00223   @Description("The probe of government")
00224   @Origin(TECHNICAL)
00225   @Variability(VOLATILE)  
00226   private final Stats stats = new Stats();
00227 
00228   @NotNull
00229   @Description("A reference to the foundation")
00230   @Origin(TECHNICAL)
00231   @Variability(CONSTANT)  
00232   private final Foundation foundation;
00233 
00234   @NotNull
00235   @Description("The initialization values for the Government")
00236   @Origin(TECHNICAL)
00237   @Variability(CONSTANT)  
00238   private final GovernmentInitValues initValues;
00239 
00240   @NotNegative
00241   @Description("The actual tax rate, as determinated in Government::calcTaxRate")
00242   @Origin(TECHNICAL)
00243   @Variability(PERIOD)  
00244   private double taxRate;
00245 
00246   @NotNegative
00247   @Description("The unemployment wage, computed in Government::calcWage")
00248   @Origin(TECHNICAL)
00249   @Variability(PERIOD)  
00250   private double unemploymentWage;
00251 
00252   @NotNegative
00253   @Description("The unemployment rate")
00254   @Origin(TECHNICAL)
00255   @Variability(PERIOD)  
00256   private double unemploymentRate;
00257 
00258   @NotNegative
00259   @Description("The average wage, computed in Government::calcWage")
00260   @Origin(TECHNICAL)
00261   @Variability(PERIOD)
00262   private double averageWage;
00263 
00264   @NotNegative
00265   @Description("The average income (including profits and rates), computed in Government::calcTaxRate")
00266   @Origin(TECHNICAL)
00267   @Variability(PERIOD)
00268   private double averageIncome;
00269   
00270   @Description("The money stock of the Government")
00271   @Origin(TECHNICAL)
00272   @Variability(PERIOD)
00273   private double money;
00274   
00275 
00282   Government(Foundation pFoundation, GovernmentInitValues pInitValues)
00283   {
00284     foundation = pFoundation;
00285     initValues = pInitValues;
00286   }
00287 
00289   @PostValidateThis
00290   void init(){
00291     taxRate = 0d;
00292     money=0d;
00293     unemploymentWage = initValues.unemploymentWageQuotient * foundation.initialDesiredWage();
00294     foundation.getProbeManager().addProbe(probe);
00295     unemploymentRate = initValues.getInitUnemploymentRate();
00296     stats.init(foundation.getStepManager());
00297   }
00298 
00300   @PostValidateThis
00301   void calcWage() {
00302     double lSumWage = 0.d;
00303     double lSumWorkAmount = 0.d;
00304     for (final Household lHousehold : foundation.getHouseholds()) {
00305       final double lWorkAmount = lHousehold.calcWorkAmount();
00306       if (lWorkAmount > 0) {
00307         lSumWage += lHousehold.calcCurrentWage();
00308         lSumWorkAmount += lWorkAmount;
00309       }
00310     }
00311 
00312     if (lSumWorkAmount > 0.d) {
00313       averageWage = (lSumWage / lSumWorkAmount);
00314       unemploymentWage = averageWage *getInitUnemploymentWageQuotient();
00315     }
00316 
00317     unemploymentRate = (foundation.getNumHouseholdsTotal() - lSumWorkAmount)
00318                         / foundation.getNumHouseholdsTotal();
00319     calcTaxRate(foundation.getNumHouseholdsTotal() - lSumWorkAmount);
00320   }
00321 
00328   @PostValidateThis
00329   @Pre(expr = "de.pik.lagom.toolbox.math.FloatMath.approxEqual(lUnemployment, " +
00330       "_this.environment.calcUnemploymentRate() * _this.environment.getNumHouseholdsTotal())",
00331       lang = "groovy")
00332   private void calcTaxRate(double lUnemployment) {
00333     double lEarnedIncome = 0;
00334     double lInsuranceCost=0 ;
00335 
00336     for (final Household lHousehold : foundation.getHouseholds()) {
00337       lEarnedIncome += lHousehold.calcIncome();
00338       lInsuranceCost += (1-lHousehold.calcWorkAmount())*getWage();
00339     }
00340     averageIncome =lEarnedIncome / foundation.getNumHouseholdsTotal();
00341 
00342     if (FloatMath.greaterZero(lEarnedIncome)&& FloatMath.greaterZero(lInsuranceCost)) {
00343       taxRate =(lInsuranceCost-money)/ lEarnedIncome;
00344       money += taxRate*lEarnedIncome-lInsuranceCost;
00345     }
00346     else{
00347       taxRate=0;
00348     }
00349     double lTest =taxRate*lEarnedIncome-lInsuranceCost;
00350     //assert (FloatMath.approxZero(money));
00351   }
00352 
00353   //---- initValues access
00354   private double getInitUnemploymentWageQuotient() {
00355     return initValues.unemploymentWageQuotient;
00356   }
00357   
00358 
00359 
00360   //---- getter/setter functions
00361   public Government.Probe getProbe() {
00362     return probe;
00363   }
00364 
00365   double getWage() {
00366     return unemploymentWage;
00367   }
00368 
00369   GovernmentInitValues getInitValues() {
00370     return initValues;
00371   }
00372 
00373   double getTaxRate() {
00374     return taxRate;
00375   }
00376 
00377   public double getMoney() {
00378     return money;
00379   }
00380 
00381   public double getAverageWage() {
00382     return averageWage;
00383   }
00384 
00385   public double getUnemploymentWage() {
00386     return unemploymentWage;
00387   }
00388 
00389   public double getUnemploymentRate() {
00390     return unemploymentRate;
00391   }
00392   
00393   // accidental add-on
00394   public double getCarbonTaxRate(int pIndex){
00395     if (pIndex==0){
00396       return initValues.getCarbonTax();
00397     }
00398     else{
00399       return -initValues.getCarbonTax();
00400     }   
00401   }
00402   
00403   public void collectTax (double pAmount){
00404     money+=pAmount;
00405   }
00406 }
00407 
00409 // EOF

Generated on Tue Sep 14 11:11:48 2010 for lagom_generiC by  doxygen 1.5.4