Skip to content

Commit

Permalink
Updates to use log as argument to multiLevel.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-andrade-inpe committed Feb 2, 2018
1 parent 0bc2b0e commit 0ba1f8b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/costanza.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- @example Basic example for testing goodness-of-fit.
-- This example reproduces Figure 4 of from Costanza R.
-- Model goodness of fit: a multiple resolution procedure. Ecological modelling. 1989 Sep 15;47(3-4):199-215.
-- @image costanza.png

import("calibration")

scene1 = CellularSpace{
Expand Down
6 changes: 6 additions & 0 deletions images/costanza.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

dofile(packageInfo("calibration").path.."/examples/costanza.lua")

chart:save("costanza.png")

clean()
Binary file added images/costanza.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added log/costanza-log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added log/costanza-original.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 33 additions & 3 deletions lua/GoodnessOfFit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ end
-- @arg data.discrete A boolean value indicating whether the values are discrete. The default value
-- is false, meaning that the atributes are continuous.
-- @arg data.k Value that determines the weigth for each square calibration. The default value is 0.1.
-- @arg data.log Value indicating whether the levels should be computed using log scale: 1, 2, 4, ... until
-- the maximum length in both directions. The default value (fales) means that all levels will be computed:
-- 1, 2, 3, ... until the maximum length in both directions.
-- @usage import("calibration")
--
-- cs1 = CellularSpace{
Expand All @@ -258,8 +261,9 @@ function multiLevel(data)
verifyBasicArguments(data)

defaultTableValue(data, "k", 0.1)
defaultTableValue(data, "log", false)

verifyUnnecessaryArguments(data, {"target", "select", "discrete", "k"})
verifyUnnecessaryArguments(data, {"target", "select", "discrete", "k", "log"})

local k = data.k
local cs1 = data.target[1]
Expand All @@ -285,7 +289,8 @@ function multiLevel(data)
local resolutionTable = {}
local expTable = {}
if data.discrete then
for i = 1, maxSquare do
local i = 1
while i <= maxSquare do
-- increase the square size and calculate fitness for each square.
local fitSquare = discreteSquareBySquare(i, cs1, cs2, attribute1, attribute2, minSquare, maxSquare)
if fitSquare ~= -1 then
Expand All @@ -296,9 +301,22 @@ function multiLevel(data)
fitnessSum = fitnessSum + (fitSquare * myexp)
exp = exp + myexp
end

if data.log then
if i == maxSquare then
i = i + 1
else
i = i * 2

if i > maxSquare then i = maxSquare end
end
else
i = i + 1
end
end
else
for i = 1, maxSquare do
local i = 1
while i <= maxSquare do
-- increase the square size and calculate fitness for each square.
local fitSquare = continuousSquareBySquare(i, cs1, cs2, attribute1, attribute2, minSquare, maxSquare)
if fitSquare ~= -1 then
Expand All @@ -309,6 +327,18 @@ function multiLevel(data)
fitnessSum = fitnessSum + (fitSquare * myexp)
exp = exp + myexp
end

if data.log then
if i == maxSquare then
i = i + 1
else
i = i * 2

if i > maxSquare then i = maxSquare end
end
else
i = i + 1
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions tests/alternative/MultipleRuns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ return{
end

unitTest:assertError(error_func, "Argument 'y' is mandatory.")
error_func = function()
error_func = function()
m = MultipleRuns{
model = MyModel2,
strategy = "factorial",
Expand Down Expand Up @@ -284,7 +284,7 @@ return{
end

unitTest:assertError(error_func, "Parameters used in strategy 'selected' must be in a table of scenarios.")
error_func = function()
error_func = function()
m = MultipleRuns{
model = MyModel4,
strategy = "selected",
Expand Down
9 changes: 9 additions & 0 deletions tests/basic/GoodnessOfFit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ return{

unitTest:assertEquals(result, 0.865, 0.01) -- 0.84 is the Total Fitness in Costanza Paper Example.

result = multiLevel{
target = {cs, cs2},
select = "costanza",
log = true,
discrete = true
}

unitTest:assertEquals(result, 0.85, 0.01)

local warningFunc = function()
result = multiLevel{
target = {sugar, sugar2},
Expand Down

0 comments on commit 0ba1f8b

Please sign in to comment.