Skip to content

Commit

Permalink
Updated tests for discrete transition costs.
Browse files Browse the repository at this point in the history
  • Loading branch information
olof3 committed Sep 6, 2017
1 parent 310492d commit eb99af1
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions test/discrete_transition_costs.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
using Base.Test
using PyPlot
using Interpolations
include("../src/dev.jl")
include(joinpath(Pkg.dir("DynamicApproximations"),"src","dev.jl"))


## Test 1
# Compare the approximation costs computed using ℓ which is obtained from
# compute_discrete_transition_costs and computed via simple linear interpolation

function compute_cost(ℓ, I, Y)
cost = 0.0;
for k=1:length(I)-1
cost += ℓ[I[k], I[k+1]](Y[k], Y[k+1])
end
return cost
end



t = linspace(0,π,50)
y = sin.(t)
g = sin.(t)


= dev.compute_discrete_transition_costs(y)
= dev.compute_discrete_transition_costs(g)

## Test 1a
I1 = [1,10,30,40,50]
Y1 = g[I1]

I = [1,10,30,40,50]
Y = sin.(t[I])
y1 = interpolate((I1,), Y1, Gridded(Linear()))[1:length(g)]

cost = 0.0;
for k=1:length(I)-1
i = I[k]
ip = I[k+1]
println(i, ":", ip)
cost += ℓ[i, ip](Y[k], Y[k+1])
end
@test compute_cost(ℓ,I1,Y1) sum((y1[1:end-1]-g[1:end-1]).^2)

y2 = interpolate((t[I],), y[I], Gridded(Linear()))[t]

plot(t, sin.(t))
plot!(t[I], Y)
plot!(t, y2, "r--")
println(cost)
## Test 1b
I2 = [1,15,30,36,50]
Y2 = randn(5)

println("Error ℓ: ", cost, " Error interp: ", sum((y-y2).^2))
y2 = interpolate((t[I2],), Y2, Gridded(Linear()))[t]

@test compute_cost(ℓ,I2,Y2) sum((y2[1:end-1]-g[1:end-1]).^2)

#plot(t, sin.(t))
#plot!(t[I1], Y1)
#plot!(t, y1, color="red")

##

## Test 2
# Check the transition costs for a simple example
= dev.compute_discrete_transition_costs([1.0, 2.0, 3.0])

# (y - 1)^2
Expand Down

0 comments on commit eb99af1

Please sign in to comment.