@submodel.title = Calculate Gini Coefficient Map @author = Alessandro Ribeiro Campos, Juliana Leroy Davis, William Leles Souza Costa e Britraldo Silveira Soares Filho @submodel.description = Calculate GINI Coefficient @submodel.group = Submodels @notes = "More detalis about the calculation: http://csr.ufmg.br/dinamica/dokuwiki/doku.php?id=agent_based_model:start#download_gini_coefficient http://csr.ufmg.br/dinamica/dokuwiki/doku.php?id=agent_based_model:start#gini_coefficient" @showproperties = yes @date = Mon Feb 18 12:18:38 2013 @version = 2.0.6.20130201 @description = It is a measure of inequality that varies from 0 to 1; 0 corresponds to complete equality and 1 corresponds to complete inequality. We use the GINI index to measure the inequality in the rabbit calorie distribution. @title = Gini Coefficient Script {{ // Inputs @alias = Input Group {{ // Energy input map ( calories) // // === // Map in which the pixel values are the amount of calories of each rabbit @submodel.in.constant.description = Mapa de entrada @submodel.in.constant.optional = no @submodel.in.constant.order = 0 @submodel.in.constant.title = Input Map inputMap := Map .UNBOUND; }}; @collapsed = no Group {{ // Calculate the amount and extent of rabbit groups // // === // Divide the population in groups: calculate the number of groups and number of // rabbits per group @collapsed = yes Group {{ @collapsed = no Group {{ extractMapAttributes2081 := ExtractMapAttributes { map = inputMap, extractDynamicAttributes = .yes, extractStatisticalAttributes = .yes }; // Amount of rabbits getLookupTableValue13070 := GetLookupTableValue { table = extractMapAttributes2081, key = 9, valueIfNotFound = 0 }; // Greater value getLookupTableValue13170 := GetLookupTableValue { table = extractMapAttributes2081, key = 11, valueIfNotFound = 0 }; // Lower value getLookupTableValue13190 := GetLookupTableValue { table = extractMapAttributes2081, key = 10, valueIfNotFound = 0 }; // Calculate the Amount of groups // // === // The calcutation is done according to Struges Rule: // // k = 1 + 3,3 log10 n // // n= number of rabbits @collapsed = no calculateValue12980 := CalculateValue { expression = [ floor(1 + 3.3 * log(v1)) ], useDefaultValue = .no, defaultValue = 0 } {{ NumberValue getLookupTableValue13070 1; }}; // Calculate the extent of the groups @collapsed = no calculateValue13130 := CalculateValue { expression = [ ceil((v1 - v2) / v3) ], useDefaultValue = .no, defaultValue = 0 } {{ // Greater value NumberValue getLookupTableValue13170 1; // Lower value NumberValue getLookupTableValue13190 2; // Amount of classes NumberValue calculateValue12980 3; }}; @collapsed = no calculateValue2372 := CalculateValue { expression = [ if v1 = 0 then 1 else v1 + 1 ], useDefaultValue = .no, defaultValue = 0 } {{ NumberValue calculateValue13130 1; }}; }}; }}; // Prepare table keys to calories frequency table according to rabbit groups @collapsed = yes Group {{ lookupTable3705 := LookupTable [ "Key" "Value" ]; int3686 := Int 0; Group {{ calcHistogram7382 := CalcHistogram { map = inputMap, intervalWidth = calculateValue2372, cumulative = .no, relative = .no }; @collapsed = no calculateLookupTable7656 := CalculateLookupTable { expression = [ line ], keyName = .none, valueName = .none } {{ NumberTable calcHistogram7382 1; }}; }}; ForEach calculateLookupTable7656 .no {{ step = step; muxValue3688 := MuxValue int3686 calculateValue3694; step3671 := Step step; muxLookupTable3681 := MuxLookupTable lookupTable3705 setLookupTableValue3676; @collapsed = no calculateValue3664 := CalculateValue { expression = [ t1[v1] ], useDefaultValue = .yes, defaultValue = 0 } {{ NumberTable calculateLookupTable7656 1; NumberValue step3671 1; }}; @collapsed = no calculateValue3694 := CalculateValue { expression = [ v1 + 1 ], useDefaultValue = .no, defaultValue = 0 } {{ NumberValue muxValue3688 1; }}; setLookupTableValue3676 := SetLookupTableValue { table = muxLookupTable3681, key = calculateValue3694, value = calculateValue3664 }; }}; }}; // Calculate Relative Frequency of calories (Y) @collapsed = yes Group {{ extractMapValues2799 _ _ := ExtractMapValues inputMap .no; lookupTable2793 := LookupTable [ "Key" "Value" ]; @collapsed = no calculateLookupTable8371 := CalculateLookupTable { expression = [ t1[line] + v1 - 1 ], keyName = .none, valueName = .none } {{ NumberTable setLookupTableValue3676 1; NumberValue calculateValue2372 1; }}; // Calculate the amount of calories per group @collapsed = no ForEach setLookupTableValue3676 .no {{ step0 = step; step2807 := Step step0; muxLookupTable2795 := MuxLookupTable extractMapValues2799 calculateLookupTable2777; ForEach extractMapValues2799 .no {{ @collapsed = no calculateLookupTable2777 := CalculateLookupTable { expression = [ if t1[line] >= t2[v1] and t1[line] <= t3[v1] then 0 else t1[line] ], keyName = .none, valueName = .none } {{ NumberTable muxLookupTable2795 1; NumberTable setLookupTableValue3676 2; NumberTable calculateLookupTable8371 3; NumberValue step2807 1; }}; @collapsed = no calculateLookupTable2779 := CalculateLookupTable { expression = [ if t1[line] >= t2[v1] and t1[line] <= t3[v1] then t1[line] else 0 ], keyName = .none, valueName = .none } {{ NumberTable muxLookupTable2795 1; NumberTable setLookupTableValue3676 2; NumberTable calculateLookupTable8371 3; NumberValue step2807 1; }}; }}; extractLookupTableAttributes2827 := ExtractLookupTableAttributes { table = calculateLookupTable2779, extractStatisticalKeyAttributes = .yes, extractStatisticalValueAttributes = .yes, extractDynamicKeyValueAttributes = .yes }; muxLookupTable2797 := MuxLookupTable lookupTable2793 setLookupTableValue2818; getLookupTableValue2815 := GetLookupTableValue { table = extractLookupTableAttributes2827, key = 31, valueIfNotFound = 0 }; // Amount of calories per group setLookupTableValue2818 := SetLookupTableValue { table = muxLookupTable2797, key = step2807, value = getLookupTableValue2815 }; }}; // Relative Frequency of calories (Y) @collapsed = no Group {{ extractLookupTableAttributes3565 := ExtractLookupTableAttributes { table = setLookupTableValue2818, extractStatisticalKeyAttributes = .yes, extractStatisticalValueAttributes = .yes, extractDynamicKeyValueAttributes = .yes }; lookupTable3563 := LookupTable [ "Key" "Value" ]; getLookupTableValue3568 := GetLookupTableValue { table = extractLookupTableAttributes3565, key = 31, valueIfNotFound = 0 }; @collapsed = no ForEach setLookupTableValue2818 .no {{ step1 = step; step3576 := Step step1; muxLookupTable3573 := MuxLookupTable lookupTable3563 setLookupTableValue3589; @collapsed = no calculateValue3578 := CalculateValue { expression = [ if t1[v1] > 0 then t1[v1] / v2 else 0 ], useDefaultValue = .no, defaultValue = 0 } {{ NumberTable setLookupTableValue2818 1; NumberValue step3576 1; NumberValue getLookupTableValue3568 2; }}; // Relative Frequency of calories(Y) setLookupTableValue3589 := SetLookupTableValue { table = muxLookupTable3573, key = step3576, value = calculateValue3578 }; }}; }}; }}; // Cauculate Cumulative Relative Frequency of poulation (X) @collapsed = yes Group {{ // Calculando o histograma do mapa. @collapsed = no Group {{ // Cumulative calcHistogram5572 := CalcHistogram { map = inputMap, intervalWidth = calculateValue2372, cumulative = .yes, relative = .yes }; }}; int36860 := Int 0; lookupTable37050 := LookupTable [ "Key" "Value" ]; ForEach calcHistogram5572 .no {{ step2 = step; muxValue36880 := MuxValue int36860 calculateValue36940; step36710 := Step step2; muxLookupTable36810 := MuxLookupTable lookupTable37050 setLookupTableValue36760; @collapsed = no calculateValue36940 := CalculateValue { expression = [ v1 + 1 ], useDefaultValue = .no, defaultValue = 0 } {{ NumberValue muxValue36880 1; }}; @collapsed = no calculateValue36640 := CalculateValue { expression = [ t1[v1] ], useDefaultValue = .no, defaultValue = 0 } {{ NumberTable calcHistogram5572 1; NumberValue step36710 1; }}; // Cumulative Relative Frequency of population (X) setLookupTableValue36760 := SetLookupTableValue { table = muxLookupTable36810, key = calculateValue36940, value = calculateValue36640 }; }}; }}; // Cumulative Relative Frequency of calories (Y) @collapsed = yes Group {{ lookupTable294200 := LookupTable [ "Classe" "Freq_acumlada" ]; ForEach setLookupTableValue2818 .no {{ step3 = step; muxLookupTable294600 := MuxLookupTable lookupTable294200 setLookupTableValue296700; step296300 := Step step3; @collapsed = no calculateValue294800 := CalculateValue { expression = [ if v1 = 1 then t1[v1] else t1[v1] + t2[v1 - 1] ], useDefaultValue = .yes, defaultValue = 0 } {{ NumberTable setLookupTableValue3589 1; NumberTable muxLookupTable294600 2; NumberValue step296300 1; NumberValue calculateValue2372 2; }}; // Cumulative Relative Frequency of calories (Y) setLookupTableValue296700 := SetLookupTableValue { table = muxLookupTable294600, key = step296300, value = calculateValue294800 }; }}; }}; // Calculate Yk + (Yk+1) = B // // Calories // // === // Cumulative Relative Frequency of a group + Cumulative Relative Frequency of the // next group @collapsed = yes Group {{ lookupTable358500 := LookupTable [ "Classe" "Xk" ]; ForEach setLookupTableValue296700 .no {{ step4 = step; muxLookupTable358900 := MuxLookupTable lookupTable358500 setLookupTableValue360800; step359100 := Step step4; @collapsed = no calculateValue359300 := CalculateValue { expression = [ if v1 = 1 then t1[v1] else t1[v1] + t1[v1 - 1] ], useDefaultValue = .yes, defaultValue = 0 } {{ NumberTable setLookupTableValue296700 1; NumberTable muxLookupTable358900 2; NumberValue step359100 1; }}; // Yk + (Yk+1) = B // // === // Cumulative Relative Frequency of a group + Cumulative Relative Frequency of the // next group setLookupTableValue360800 := SetLookupTableValue { table = muxLookupTable358900, key = step359100, value = calculateValue359300 }; }}; }}; // Calculate Xk+(Xk-1)= A // // Rabbits // // === // Cumulative Relative Frequency of a group + Cumulative Relative Frequency of the // previous group @collapsed = yes Group {{ lookupTable35851 := LookupTable [ "Classe" "Xk" ]; ForEach setLookupTableValue36760 .no {{ step5 = step; muxLookupTable35891 := MuxLookupTable lookupTable35851 setLookupTableValue36081; step35911 := Step step5; @collapsed = no calculateValue35931 := CalculateValue { expression = [ if v1 = 1 then t1[v1] else t1[v1] - t1[v1 - 1] ], useDefaultValue = .yes, defaultValue = 0 } {{ NumberTable setLookupTableValue36760 1; NumberTable muxLookupTable35891 2; NumberValue step35911 1; NumberValue calculateValue2372 2; }}; // Xk+(Xk-1)= A // // === // Cumulative Relative Frequency of a group + Cumulative Relative Frequency of the // previous group setLookupTableValue36081 := SetLookupTableValue { table = muxLookupTable35891, key = step35911, value = calculateValue35931 }; }}; }}; // Calculate Gini Coefficient= 1- (A*B) @collapsed = yes Group {{ // A*B @collapsed = no calculateLookupTable3651 := CalculateLookupTable { expression = [ t1[line] * t2[line] ], keyName = .none, valueName = .none } {{ NumberTable setLookupTableValue36081 1; NumberTable setLookupTableValue360800 2; }}; extractLookupTableAttributes3639 := ExtractLookupTableAttributes { table = calculateLookupTable3651, extractStatisticalKeyAttributes = .yes, extractStatisticalValueAttributes = .yes, extractDynamicKeyValueAttributes = .yes }; getLookupTableValue39560 := GetLookupTableValue { table = extractLookupTableAttributes3639, key = 31, valueIfNotFound = 0 }; // 1- (A*B)= Gini Coefficient @collapsed = no calculateValue39590 := CalculateValue { expression = [ 1 - v1 ], useDefaultValue = .no, defaultValue = 0 } {{ NumberValue getLookupTableValue39560 1; }}; }}; }}; // Output @alias = Output Group {{ // GINI Coefficient @alias = GINI Coeficient @submodel.out.object.description = Double precision real object @submodel.out.object.optional = no @submodel.out.object.order = 0 @submodel.out.object.title = Double _ := Double calculateValue39590; }}; }};