This notebook is part of the chapter 11 of the Book:  Mathematica Beyond Mathematics: The Wolfram Language in the Real World.1st Edition. Chapman and Hall/CRC. by J. Guillermo Sánchez León. It is Copyright by CRC, it is only for yourself.

. Financial Information

.. Introduction

.. FinancialData

FinancialData["SP500", "Name" ]

FinancialData["^GSPC"]

DateListPlot[FinancialData["^GSPC",{"January 1 2005","March 25 2019"}],Joined->True]

.. Visualization

CandlestickChart[{"NYSE:GE",{{2018,01,1},{2018,12,31}}}]

InteractiveTradingChart[{"NYSE:GE",{{2018,10,1},{2019,03,25}}}]

.. Automatic Adjustments

googleStock=FinancialData["GOOG",{{2015,3,10},{2019,02,25},"Day"},"Value"];

dist=EstimatedDistribution[googleStock,LogNormalDistribution[μ, σ]]

QuantilePlot[googleStock,dist,Filling->Automatic,FillingStyle->Red]

.  Financial Functions

.. Bonds

Clear["Global`*"]

FindRoot[FinancialBond[{"FaceValue"->1000,"Coupon"->0.05,"Maturity"->{2019,2,28},"CouponInterval"->(1/4)},{"InterestRate"->y,"Settlement"->{2013,1,6}}]==900,{y,.1}]

.. The Black–Scholes Equation

spotPrice =9.87;euribor12m=0.00058;vol=0.528374;

FinancialDerivative[{"European", "Call"}, {"StrikePrice" -> spotPrice, 
  "Expiration" -> 1},  {"InterestRate" -> euribor12m, "Volatility" -> vol, 
  "CurrentPrice" -> spotPrice}]

BlackScholesModel={-r c(t,s)+r s ((∂c(t,s))/(∂s))+(1/2) (s^2) (σ^2) (((∂^2)c(t,s))/(∂(s^2)))+((∂c(t,s))/(∂t))==0,c(T,s)==max(s-k,0)};

(dsol = c[t, s] /. 
    DSolve[BlackScholesModel, c[t, s], {t, s}][[
     1]]) // TraditionalForm

dsol /. {t -> 0, s ->spotPrice, k ->spotPrice, σ ->vol, T -> 1, 
  r ->   euribor12m}

. Optimization

.. What is Constrained Optimization?

({{Function, Application, Description}, {FindMinimum/FindMaximum, Local 
Numerical optimization, Linear programming methods,non-linear,
interior points,and the use of second derivatives}, {NMinimize/NMaximize, Global 
Numerical optimization, Linear programming methods,Nelder-Mead,
differential evolution,simulated annealing,
random search}, {Minimize/Maximize, Exact 
Global optimization, Linear programming methods,algebraic 
cylindrical decomposition,Lagrange 
multipliers and other analytical methods,
integer linear programming.}, {LinearProgramming, Linear 
optimization, Simplex,modified simplex,interior point}})

Clear["`Global`*"]

var= {x,y},ob = x+y,c1 = 0≤x≤1,c2 =0≤y≤2

{NMaximize[{ob,c1,c2}, var], Maximize[{ob,c1,c2}, var], 
FindMaximum[{ob,c1,c2}, var]}

x + y/.{x->1,y->2}

Minimize[{ob,c1,c2}, var]

Plot3D[x + y, {x,-1,2}, {y, -1, 2},  RegionFunction -> Function[{x,y},0≤x<1&&0≤y<2],AxesLabel->Automatic,Mesh->None]

Clear["`Global`*"]

var= {x,y};ob = x + 2 y;c1 =- 5 x + y == 7;c2 =x + y ≥ 26;c3=x ≥ 3;c4=y ≥ 3;

NMinimize[{ob,c1,c2,c3,c4,var∈Integers},var]

x + 2 y			-> 	c: {1, 2}
	-5 x + y == 7  	->	m1: {-5, 1} 	b1:  {7, 0}
	x + y ≥ 26   		->	m2:  {1, 1}	    b2:  {26, 1}

LinearProgramming[{1,2},{{-5,1},{1,1}},{{7,0},{26,1}},{{3,Infinity},{4,Infinity}},Integers]//Quiet

Clear["`Global`*"]

m=Minimize[{Exp[-x y],{x,y}∈Disk[]},{x,y}]

g=Plot3D[Exp[-x y],{x,y}∈Disk[],Axes->True,Boxed->False,PlotStyle->Opacity@0.6,AxesLabel->Automatic];

Show[g,Graphics3D[{PointSize@Large,Red,Point[{x,y,m[[1]]}/.m[[2]]]}]]

.. Application: Portfolio Optimization

var = {(Subscript[x, 1])(*Mortgages*), (Subscript[x, 2])(*Mutual funds*),(Subscript[x, 3])(*Personal loans*),(Subscript[x, 4]) (*Commercial loans*),(Subscript[x, 5])(*Certificates/Bonds*), (Subscript[x, 6])(*Government debt*)};

of = 9 (Subscript[x, 1])+ 12 (Subscript[x, 2]) + 15 (Subscript[x, 3]) + 8 (Subscript[x, 4]) + 6 (Subscript[x, 5]) + 3 (Subscript[x, 6]);

c1 = (Subscript[x, 1])+ (Subscript[x, 2])+(Subscript[x, 3])+(Subscript[x, 4]) +(Subscript[x, 5 ])+ (Subscript[x, 6])==1;

c2=((3 (Subscript[x, 1])+ 6 (Subscript[x, 2]) + 8 (Subscript[x, 3]) + 2 (Subscript[x, 4]) +(Subscript[x, 5]) )/((Subscript[x, 1])+ (Subscript[x, 2])+(Subscript[x, 3])+(Subscript[x, 4]) +(Subscript[x, 5 ])))≤ 5

c3 = (Subscript[x, 4]) ≥ 0.2;

c4 = (Subscript[x, 2]) + (Subscript[x, 3])≤ (Subscript[x, 1]);

c5=Map[#≥ 0 &, var];

sol=NMaximize[{of,  c1, c2, c3, c4, c5}, var]

. The Shortest Path Problem

..  The Traveling Salesman Problem

.. A Tour Around South American Cities

cities={Entity["City", {"Asuncion", "Asuncion", "Paraguay"}], 
 Entity["City", {"Bogota", "DistritoCapital", "Colombia"}], 
 Entity["City", {"RioDeJaneiro", "RioDeJaneiro", "Brazil"}], 
 Entity["City", {"BuenosAires", "BuenosAires", "Argentina"}], 
 Entity["City", {"Caracas", "DistritoCapital", "Venezuela"}], 
 Entity["City", {"LaPaz", "LaPaz", "Bolivia"}], 
 Entity["City", {"Lima", "Lima", "Peru"}], 
 Entity["City", {"Montevideo", "Montevideo", "Uruguay"}], 
 Entity["City", {"Quito", "Pichincha", "Ecuador"}], 
 Entity["City", {"Santiago", "Metropolitana", "Chile"}]};

order=Last[FindShortestTour[GeoPosition[cities]]]

GeoListPlot[cities[[order]],Joined->True]