Повышение доходности с инвестиций
作者
using Plots
function plotInvest(N, PurchaseYears, Maturity, InterestRates, args...)
plotSol = false
smallScale = true
xsol = nothing
if length(args) >= 1
plotSol = true
xsol = args[1]
if length(args) >= 2
smallScale = args[2]
end
end
nPtotal = length(PurchaseYears)
T = nPtotal - N
if smallScale
fig = plot(size = (700, 100 + 25*(N+2)),
background_color = :white,
legend = false)
else
fig = plot(size = (700, 700),
background_color = :white,
legend = false)
end
plot!(xaxis = false, yaxis = false, grid = false)
x = range(0.08, 0.98, length = T+1)
xwidth = x[2] - x[1]
xl = 0.02
if smallScale
yt = 0.98
y = range(0.84, 0.02, length = N+2)
ywidth = y[1] - y[2]
else
yt = 0.94
y = range(0.92, 0.02, length = N+2)
ywidth = y[1] - y[2]
end
for j in 1:length(x)
plot!([x[j], x[j]], [yt, y[end]],
linestyle = :dash, color = :black, linewidth = 1)
end
if smallScale
plot!([xl, xl], [y[1], y[end]], color = :black, linewidth = 1)
end
if smallScale
for i in 1:length(y)
plot!([xl, x[end]], [y[i], y[i]], color = :black, linewidth = 1)
end
plot!([x[1], x[end]], [yt, yt], color = :black, linewidth = 1)
for j in 1:T
annotate!(x[j] + xwidth/2, y[1] + (yt - y[1])/2,
text("Год $j", 8, :center, :middle))
end
for i in 0:N
rate_idx = i + T
if rate_idx <= length(InterestRates)
rate_str = "($(InterestRates[rate_idx])%)"
else
rate_str = "Н/Д"
end
annotate!(xl + (x[1] - xl)/2, y[i+2] + ywidth/2,
text("$i\n$rate_str", 8, :center, :middle))
end
for j in 1:T
if plotSol && xsol !== nothing && xsol[j] > 1e-2
plot!([x[j], x[j+1], x[j+1], x[j], x[j]],
[y[2], y[2], y[2] + ywidth, y[2] + ywidth, y[2]],
fill = (0, RGBA(0.85, 0.15, 0.15, 0.6)), linewidth = 1)
else
plot!([x[j], x[j+1], x[j+1], x[j], x[j]],
[y[2], y[2], y[2] + ywidth, y[2] + ywidth, y[2]],
fill = (0, RGBA(0.8, 0.8, 0.8, 0.6)), linewidth = 1)
end
if plotSol && xsol !== nothing
annotate!(x[j] + xwidth/2, y[2] + ywidth/2,
text("x$j = $(round(xsol[j], digits=2))", 8, :center, :middle))
else
annotate!(x[j] + xwidth/2, y[2] + ywidth/2,
text("x$j", 8, :center, :middle))
end
end
for i in 1:N
Sidx = i + T
if Sidx <= length(PurchaseYears) && Sidx <= length(Maturity)
j = PurchaseYears[Sidx]
maturity_period = Maturity[Sidx]
if j + maturity_period <= length(x) && j >= 1
if plotSol && xsol !== nothing && Sidx <= length(xsol) && xsol[Sidx] > 1e-2
plot!([x[j], x[j+maturity_period], x[j+maturity_period], x[j], x[j]],
[y[i+2], y[i+2], y[i+2] + ywidth, y[i+2] + ywidth, y[i+2]],
fill = (0, RGBA(0.85, 0.15, 0.15, 0.6)), linewidth = 1)
else
plot!([x[j], x[j+maturity_period], x[j+maturity_period], x[j], x[j]],
[y[i+2], y[i+2], y[i+2] + ywidth, y[i+2] + ywidth, y[i+2]],
fill = (0, RGBA(0.8, 0.8, 0.8, 0.6)), linewidth = 1)
end
if plotSol && xsol !== nothing && Sidx <= length(xsol)
annotate!(x[j] + (x[j+maturity_period] - x[j])/2, y[i+2] + ywidth/2,
text("x$Sidx = $(round(xsol[Sidx], digits=2))", 8, :center, :middle))
else
annotate!(x[j] + (x[j+maturity_period] - x[j])/2, y[i+2] + ywidth/2,
text("x$Sidx", 8, :center, :middle))
end
end
end
end
else
if plotSol && xsol !== nothing
investment_groups = Dict{Tuple{Int, Int}, Vector{Int}}()
significant_investments = findall(x -> x > 1e-2, xsol)
for i in significant_investments
if i <= length(PurchaseYears) && i <= length(Maturity)
j = PurchaseYears[i]
maturity = Maturity[i]
key = (j, maturity)
if !haskey(investment_groups, key)
investment_groups[key] = []
end
push!(investment_groups[key], i)
end
end
y_positions = Dict{Int, Float64}()
current_y = y[1]
y_step = (y[1] - y[end]) / max(1, length(significant_investments))
sorted_groups = sort(collect(keys(investment_groups)), by = x -> (x[1], x[2]))
for key in sorted_groups
investments = investment_groups[key]
for (idx, investment) in enumerate(investments)
y_positions[investment] = current_y
current_y -= y_step
end
end
for i in significant_investments
if i <= length(PurchaseYears) && i <= length(Maturity) && haskey(y_positions, i)
j = PurchaseYears[i]
maturity = Maturity[i]
y_pos = y_positions[i]
if j + maturity <= length(x) && j >= 1
plot!([x[j], x[j + maturity]], [y_pos, y_pos],
color = :green, linewidth = 2)
annotate!(xl + (x[1] - xl)/2, y_pos,
text("{$i}", 8, :center, :middle))
end
end
end
annotate!(x[1]/2, yt + (0.98 - yt)/2,
text("Год", 8, :right, :middle))
for j in 1:T
annotate!(x[j] + xwidth/2, yt + (0.98 - yt)/2,
text("$j", 8, :center, :middle))
end
end
end
return fig
end