%% Use Dupire Model and Finite Difference Pricer to Price Vanilla Instrument
% This example shows the workflow to price a Vanilla instrument when you use a Dupire model and a FiniteDifference pricing method.
%% Create Vanilla Instrument Object
% Use fininstrument to create a Vanilla instrument object.

VanillaOpt = fininstrument("Vanilla",'ExerciseDate',datetime(2020,1,1),'Strike',105,'ExerciseStyle',"american",'Name',"vanilla_option")

%
%% Create Dupire Model Object
% Define the implied volatility surface data.

AssetPrice = 590;
Maturity = ["06-Mar-2018" "05-Jun-2018" "12-Sep-2018" "10-Dec-2018" "01-Jan-2019" ...
"02-Jul-2019" "01-Jan-2020" "01-Jan-2021" "01-Jan-2022" "01-Jan-2023"];
Maturity = repmat(Maturity,10,1);
Maturity = Maturity(:);

ExercisePrice = AssetPrice.*[0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.30 1.40];
ExercisePrice = repmat(ExercisePrice,1,10)';

ImpliedVol = [...
    0.190; 0.168; 0.133; 0.113; 0.102; 0.097; 0.120; 0.142; 0.169; 0.200; ...
    0.177; 0.155; 0.138; 0.125; 0.109; 0.103; 0.100; 0.114; 0.130; 0.150; ...
    0.172; 0.157; 0.144; 0.133; 0.118; 0.104; 0.100; 0.101; 0.108; 0.124; ...
    0.171; 0.159; 0.149; 0.137; 0.127; 0.113; 0.106; 0.103; 0.100; 0.110; ...
    0.171; 0.159; 0.150; 0.138; 0.128; 0.115; 0.107; 0.103; 0.099; 0.108; ...
    0.169; 0.160; 0.151; 0.142; 0.133; 0.124; 0.119; 0.113; 0.107; 0.102; ...
    0.169; 0.161; 0.153; 0.145; 0.137; 0.130; 0.126; 0.119; 0.115; 0.111; ...
    0.168; 0.161; 0.155; 0.149; 0.143; 0.137; 0.133; 0.128; 0.124; 0.123; ...
    0.168; 0.162; 0.157; 0.152; 0.148; 0.143; 0.139; 0.135; 0.130; 0.128; ...
    0.168; 0.164; 0.159; 0.154; 0.151; 0.147; 0.144; 0.140; 0.136; 0.132];

ImpliedVolData = table(Maturity, ExercisePrice, ImpliedVol);

% Use finmodel to create a Dupire model object.

DupireModel = finmodel("Dupire",'ImpliedVolData',ImpliedVolData)

%% Create ratecurve Object
% Create a flat ratecurve object using ratecurve.

Settle = datetime(2018,1,1);
Maturity = datetime(2020,9,1);
Rate = 0.06;
myRC = ratecurve('zero',Settle,Maturity,Rate)

%% Create FiniteDifference Pricer Object
% Use finpricer to create a FiniteDifference pricer object and use the ratecurve object for the 'DiscountCurve' name-value pair argument.

outPricer = finpricer("FiniteDifference",'Model',DupireModel,'DiscountCurve',myRC,'SpotPrice',100,'DividendValue',0.0262,'DividendType',"continuous")

%% Price Vanilla Instrument
% Use price to compute the price and sensitivities for the Vanilla instrument.

[Price, outPR] = price(outPricer,VanillaOpt,["all"])
outPR.Results

%
% Copyright 2019 The MathWorks, Inc.