12  反実仮想シミュレーション

using Plots
using JLD2
using Random
using LinearAlgebra
using DataFrames

ENV["GKS_ENCODING"] = "utf8"
gr(fontfamily="IPAMincho")
Plots.GRBackend()
mutable struct array_struct
    CCP1Mat::Matrix{Float64}
    CCP2Mat::Matrix{Float64}
    stateTransitionMat::Matrix{Float64}
    equilibriumProfitFirm1::Matrix{Float64}
    equilibriumProfitFirm2::Matrix{Float64}
    expectedShockUnderBestActionFirm1::Matrix{Float64}
    expectedShockUnderBestActionFirm2::Matrix{Float64}
    exanteV1::Vector{Float64}
    exanteV2::Vector{Float64}
    updatedCCP1Numerator::Matrix{Float64}
    updatedCCP2Numerator::Matrix{Float64}
    CCP1MatUpdated::Matrix{Float64}
    CCP2MatUpdated::Matrix{Float64}
    stateTransitionMatFirm2ActionSpecific::Array{Float64, 3}
    stateTransitionMatFirm1ActionSpecific::Array{Float64, 3}
    exanteV1Updated::Vector{Float64}
    exanteV2Updated::Vector{Float64}
end


mutable struct global_param_struct
    beta::Float64
    eulergamma::Float64
    entryMat::Matrix{Int}
    numFirmsVec::Vector{Int}
    economyVec::Vector{Int}
    possibleActionArray::Array{Int, 3}
end
for file in readdir("functions/dynamic_game/")
    include("functions/dynamic_game/" * file)
end
@load "tmp/dynamic_game/data_workspace.jld2";
@load "tmp/dynamic_game/estimates_1.jld2";
TransitionMat = [0.7 0.3; 0.4 0.6]

Random.seed!(123);

NumSimPeriods = 15;

12.1 ベースラインの均衡

BaselineParameterValues = [
    0.3,
    -0.27,
    0.45,
    -0.15,
    -2.10,
    0.2,
    -0.27,
    0.45,
    -0.15,
    -2.10
];

BaselineParameterMat = reshape(BaselineParameterValues, (5, 2));
profitFirm1 = calculateFirmProfit(
    BaselineParameterMat,
    global_param.entryMat,
    global_param.numFirmsVec,
    global_param.economyVec,
    global_param.possibleActionArray,
    1
);
profitFirm2 = calculateFirmProfit(
    BaselineParameterMat,
    global_param.entryMat,
    global_param.numFirmsVec,
    global_param.economyVec,
    global_param.possibleActionArray,
    2
);
data_base_struct = createInitialDataStruct(
    profitFirm1,
    profitFirm2,
    TransitionMat,
    global_param
);
@time calculateMPE!(
    data_base_struct,
    TransitionMat, 
    profitFirm1, 
    profitFirm2, 
    global_param
    );

CCP1Matbase = data_base_struct.CCP1Mat;
CCP2Matbase = data_base_struct.CCP2Mat;
exanteV1base = data_base_struct.exanteV1;
exanteV2base = data_base_struct.exanteV2;
  1.587827 seconds (5.63 M allocations: 364.474 MiB, 6.94% gc time, 99.75% compilation time)
n1, n2 = simulateEntryFirms(
    TransitionMat,
    CCP1Matbase,
    CCP2Matbase,
    global_param,
    NumSimPeriods
);

12.2 反実仮想シミュレーション: 差別化戦略

CounterfactualParameterValues = [
    0.6,
    -0.27,
    0.45,
    -0.15,
    -2.10,
    0.2,
    -0.27,
    0.45,
    -0.15,
    -2.10
];

CounterfactualParameterMat = reshape(
    CounterfactualParameterValues, 
    (5, 2)
    );

profitFirm1_cf = calculateFirmProfit(
    CounterfactualParameterMat,
    global_param.entryMat,
    global_param.numFirmsVec,
    global_param.economyVec,
    global_param.possibleActionArray,
    1
);
profitFirm2_cf = calculateFirmProfit(
    CounterfactualParameterMat,
    global_param.entryMat,
    global_param.numFirmsVec,
    global_param.economyVec,
    global_param.possibleActionArray,
    2
);
data_cf_struct = createInitialDataStruct(
    profitFirm1_cf,
    profitFirm2_cf,
    TransitionMat,
    global_param
);

@time output = calculateMPE!(
    data_cf_struct,
    TransitionMat,
    profitFirm1_cf,
    profitFirm2_cf,
    global_param
    );

CCP1Matcf = data_cf_struct.CCP1Mat;
CCP2Matcf = data_cf_struct.CCP2Mat;
exanteV1cf = data_cf_struct.exanteV1;
exanteV2cf = data_cf_struct.exanteV2;
  0.000297 seconds (3.65 k allocations: 392.828 KiB)
n1_cf, n2_cf = simulateEntryFirms(
    TransitionMat,
    CCP1Matcf,
    CCP2Matcf,
    global_param,
    NumSimPeriods
);
plot1 = plot(
    1:NumSimPeriods,
    n1,
    linestyle = :dash,
    title = "企業1の店舗存在確率",
    label = "現状維持"
);
plot!(
    1:NumSimPeriods,
    n1_cf,
    label = "新ブランド展開           "
);
xlabel!("期間")
ylabel!("店舗存在確率")


plot2 = plot(
    1:NumSimPeriods,
    n2,
    linestyle = :dash,
    title = "企業2の店舗存在確率",
    label = "現状維持"
);
plot!(
    1:NumSimPeriods,
    n2_cf,
    label = "新ブランド展開           "
);
xlabel!("期間")
ylabel!("店舗存在確率")

plot(plot1, plot2, layout = (1, 2))
2 4 6 8 10 12 14 期間 0.0 0.2 0.4 0.6 店舗存在確率 企業1の店舗存在確率 現状維持 新ブランド展開 2 4 6 8 10 12 14 期間 0.0 0.1 0.2 0.3 0.4 0.5 店舗存在確率 企業2の店舗存在確率 現状維持 新ブランド展開
diff_value_1 = exanteV1cf - exanteV1base;

DataFrame(
    exanteV1base = exanteV1base,
    exanteV1cf = exanteV1cf,
    diff_value_1 = diff_value_1
)
8×3 DataFrame
RowexanteV1baseexanteV1cfdiff_value_1
Float64Float64Float64
14.65685.339430.682633
24.587325.250260.662943
36.568767.742261.17349
46.158877.318751.15988
54.612385.282170.669785
64.546175.196670.650505
76.015817.181051.16524
85.60886.75991.1511