Mathematica for Financial Applications Mathematica에 대한 소개와 응용 사례
Total Page:16
File Type:pdf, Size:1020Kb
Mathematica for Financial Applications Mathematica에 대한 소개와 응용 사례 Yu-Sung Chang Wolfram Research, Inc. © 2009 Wolfram Research, Inc. • Terms of Use How Are We: About Wolfram Research Founded by Stephen Wolfram and Theodore Gray Wolfram Research, Inc. Champaign, IL What is Mathematica? 2 Korea2009FinancialPublic_for_PUBLIC.nb What Is Mathematica? Number of Built-in Functions: From 557 to 2500 Features of Mathematica Basic Mathematica Tutorial Korea2009FinancialPublic_for_PUBLIC.nb 3 Basic Arithmetic + 3.27 5.32 ê 3.12 4.97513 1 2 + 3 7 13 21 100 ! 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976 156518286253697920827223758251185210916864 000 000 000 000 000 000 000 000 2 2 3 a = 1 + + 7 4 471 196 N@a, 30 D 2.40306122448979591836734693878 Pi π + x y ê z y x + z Linear Algebra 2.0 3.1 −1.1 A = 0.2 −0.29 3.3 −1.5 4.2 4.7 Transpose @AD 882., 0.2, −1.5 <, 83.1, −0.29, 4.2 <, 8−1.1, 3.3, 4.7 << Inverse @AD 880.309722, 0.390433, −0.201646 <, 80.119836, −0.157679, 0.138757 <, 8−0.00824, 0.265511, 0.0244148 << 3.8 0.8 0.1 B = 2.6 3.2 1.7 4 5 4.1 A.B 8811.26, 6.02, 0.96 <, 813.206, 15.732, 13.057 <, 824.02, 35.74, 26.26 << Clear @a, b, c, d, e, f, g, h, i, x, y, zD; a b c A = d e f g h i Transpose @AD êê MatrixForm 4 Korea2009FinancialPublic_for_PUBLIC.nb a d g b e h c f i Inverse @AD êê MatrixForm −f h+e i c h−b i −c e+b f −c e g+b f g+c d h−a f h−b d i+a e i −c e g+b f g+c d h−a f h−b d i+a e i −c e g+b f g+c d h−a f h−b d i+a e i f g−d i −c g+a i c d−a f −c e g+b f g+c d h−a f h−b d i+a e i −c e g+b f g+c d h−a f h−b d i+a e i −c e g+b f g+c d h−a f h−b d i+a e i −e g+d h b g−a h −b d+a e −c e g+b f g+c d h−a f h−b d i+a e i −c e g+b f g+c d h−a f h−b d i+a e i −c e g+b f g+c d h−a f h−b d i+a e i A.B êê MatrixForm 3.8 a + 2.6 b + 4 c 0.8 a + 3.2 b + 5 c 0.1 a + 1.7 b + 4.1 c 3.8 d + 2.6 e + 4 f 0.8 d + 3.2 e + 5 f 0.1 d + 1.7 e + 4.1 f 3.8 g + 2.6 h + 4 i 0.8 g + 3.2 h + 5 i 0.1 g + 1.7 h + 4.1 i Linear Solve 2.0 3.1 −1.1 A = 0.2 −0.29 3.3 −1.5 4.2 4.7 −1 B = 2.2 0.5 = X LinearSolve @A, BD 880.448408 <, 8−0.397351 <, 80.604572 << A.X êê MatrixForm −1. 2.2 0.5 Clear @a, b, c, d, e, f, g, h, i, x, y, zD; a b c A = d e f ; g h i = B 88x<, 8y<, 8z<<; LinearSolve @A, BD −f h x + e i x + c h y − b i y − c e z + b f z :: >, −c e g + b f g + c d h − a f h − b d i + a e i −f g x + d i x + c g y − a i y − c d z + a f z −e g x + d h x + b g y − a h y − b d z + a e z : >, : >> c e g − b f g − c d h + a f h + b d i − a e i −c e g + b f g + c d h − a f h − b d i + a e i Korea2009FinancialPublic_for_PUBLIC.nb 5 Guide: Matrices and Linear Algebra Basic Calculus Derivatives f = x3 + 3 x2 + x + 2 D@f, xD 1 + 6 x + 3 x2 ∂x f 1 + 6 x + 3 x2 D@f, 8x, 2<D 6 + 6 x = n + + + f x Log @x aD c D@f, xD 1 n x−1+n + a + x 2 + 2 DAx y , 88x, y<<E 82 x, 2 y< ∂ x2 + y2 88x,y<< I M 82 x, 2 y< Integration 3 Integrate Ax , xE x4 4 x3 x ‡ x4 4 n + n−1 Integrate Ax a x , xE a x xn + n 1 + n 1 x ‡ x4 − a4 x ArcTan − + + A a E Log @ a xD Log @a xD − + − 2 a3 4 a3 4 a3 3 − Integrate Ax , 8x, 1, 2<E 15 4 6 Korea2009FinancialPublic_for_PUBLIC.nb 2 x3 x ‡−1 15 4 3 − → Plot Ax , 8x, 1, 2<, Filling 0E 3 Integrate Ax , 8x, a, b<E a4 b4 − + 4 4 Basic Statistics Statistical measures = data RandomReal @1, 810 <D 80.496091, 0.149269, 0.35607, 0.471387, 0.460904, 0.383276, 0.419492, 0.111618, 0.92457, 0.914342 < Mean @data D 0.468702 Median @data D 0.440198 Variance @data D 0.0732423 StandardDeviation @data D 0.270633 Skewness @data D 0.608371 Kurtosis @data D 2.52045 MovingAverage @data, 3D 80.33381, 0.325575, 0.429454, 0.438522, 0.421224, 0.304795, 0.485227, 0.650177 < Quantile @data, .9 D 0.914342 Korea2009FinancialPublic_for_PUBLIC.nb 7 Correlations = In[114]:= data RandomReal @1, 810 <D Out[114]= 80.80533, 0.721439, 0.28599, 0.362672, 0.455081, 0.868824, 0.612958, 0.838698, 0.193259, 0.400555 < = In[115]:= data2 RandomReal @10, 810 <D Out[115]= 88.74986, 9.32905, 0.834222, 8.39764, 3.59373, 6.81173, 5.84848, 4.52274, 3.92227, 5.2548 < In[116]:= 8Covariance @data, data2 D, Correlation @data, data2 D< Out[116]= 80.344837, 0.525245 < DateListPlot @CountryData @"SouthKorea", 8"GDP", 81970, 2007 <<DD 8 µ 10 11 6 µ 10 11 4 µ 10 11 2 µ 10 11 0 1970 1980 1990 2000 CountryData @"US", "Properties"D êê Short 8AdultPopulation, AgriculturalProducts, 220 , WaterArea, WaterwayLength < ? *Data 8AlgebraicRulesData, GraphicsData, AstronomicalData, ImageData, BoxData, IsotopeData, ButtonData, KnotData, CellGroupData, LatticeData, ChartElementData, OLEData, ChemicalData, OutputFormData, CityData, ParticleData, ColorData, PolyhedronData, CompressedData, ProteinData, ControllerInformationData, RawData, CountryData, SeriesData, ElementData, StyleData, ExampleData, SystemInformationData, FinancialData, TextData, FiniteGroupData, ValuesData, GenomeData, VectorGlyphData, GeodesyData, VirtualGroupData, GeoProjectionData, WeatherData, GraphData, WordData < Real examples 1: Correlation between GDP per capita and life expectancy = @ data Cases A8CountryData @ , "GDPPerCapita"D, CountryData @ , "LifeExpectancy"D< & ê CountryData @D, 9_ ? NumberQ, _ ?NumberQ =E; ListPlot @data D 80 70 60 50 10 000 20 000 30 000 40 000 50 000 60 000 = gdp data @@All, 1DD; = life data @@All, 2DD; Correlation @Log @10, gdp D, life D 0.743374 8 Korea2009FinancialPublic_for_PUBLIC.nb Real examples 2: Dow Jones Index and S & P 500 = dowjones FinancialData @"^DJI", "Price", 82004, 1, 1<, "Value"D; = sandp FinancialData @"^GSPC", "Price", 82004, 1, 1<, "Value"D; Row @8ListPlot @dowjones D, ListPlot @sandp D<D 14 000 1400 12 000 1200 10 000 1000 200 400 600 800 1000 1200 1400 200 400 600 800 1000 1200 1400 ListPlot @Transpose @8dowjones, sandp <DD 1400 1200 1000 8000 9000 10 000 11 000 12 000 13 000 14 000 Correlation @sandp, dowjones D 0.98451 Fitting General fit = data 880, 1<, 81, 0<, 83, 2<, 85, 4<<; = line Fit @data, 81, x<, xD 0.186441 + 0.694915 x = 2 parabola Fit Adata, 91, x, x =, xE 0.678392 − 0.266332 x + 0.190955 x2 → Show @ListPlot @data, PlotStyle 8PointSize @Large D, Red <D, Plot @8line, parabola <, 8x, 0, 5<DD 4 3 2 1 1 2 3 4 5 Korea2009FinancialPublic_for_PUBLIC.nb 9 Model fit = data 880, 1<, 81, 0<, 83, 2<, 85, 4<<; = lm LinearModelFit @data, x, xD FittedModel B 0.186441 + 0.694915 x F lm @"Data"D 880, 1<, 81, 0<, 83, 2<, 85, 4<< lm @"DesignMatrix"D 881., 0. <, 81., 1. <, 81., 3. <, 81., 5. << lm @"ANOVATable"D DF SS MS F Statistic P-Value x 1 7.12288 7.12288 8.75521 0.0977564 Error 2 1.62712 0.813559 Total 3 8.75 = + + nlm NonlinearModelFit @data, a x ^ 2 b x c, 8a, b, c<, xD 2 FittedModel B 0.678392 - 0.266332 x + 0.190955 x F nlm @"Properties"D êê Short 8AdjustedRSquared, AIC, ANOVATable, 44 , StandardizedResiduals, StudentizedResiduals < = data 880, 1<, 81, 0<, 83, 2<, 85, 4<, 86, 4<, 87, 5<<; = + nlm NonlinearModelFit @data, Log @a b x ^ 2D, 8a, b<, xD Log 1.50632 + 1.42633 x2 FittedModel B B F F Normal @nlm D 2 Log A1.50632 + 1.42633 x E → → Show @ListPlot @data, PlotStyle Red D, Plot @nlm @xD, 8x, 0, 7<D, Frame True D 5 4 3 2 1 0 0 1 2 3 4 5 6 7 10 Korea2009FinancialPublic_for_PUBLIC.nb nlm @"FitResiduals"D 80.59033, −1.07591, −0.663282, 0.384644, 0.0324629, 0.731751 < % → ListPlot @ , Filling Axis D Real example 1 = data Transpose @8sandp, dowjones <D; ListPlot @data D 14 000 12 000 10 000 1000 1200 1400 = lm LinearModelFit @data, x, xD FittedModel B 1134.22 + 8.04978 x F Normal @lm D 1134.22 + 8.04978 x → → Show @ListPlot @data, PlotStyle 8PointSize @Tiny D, Red <D, Plot @lm @xD, 8x, 800, 1500 <, PlotStyle Thick DD 14 000 12 000 10 000 1000 1200 1400 Real example 2 = data Transpose @8gdp, life <D; → ListPlot @data, PlotRange All D Korea2009FinancialPublic_for_PUBLIC.nb 11 80 70 60 50 20 000 40 000 60 000 80 000 100 000 = + nlm NonlinearModelFit @data, a Log @xD b, 8a, b<, xD FittedModel 28.2557 + 4.97588 Log x B @ D F Normal @nlm D 28.2557 + 4.97588 Log @xD → → Show @ListPlot @data, PlotStyle Red D, Plot @nlm @xD, 8x, 0, 100000 <, PlotStyle Thick DD 80 70 60 50 20 000 40 000 60 000 80 000 100 000 Real example 3 = yahoo FinancialData @"F", "Price", 82007, 1, 1<, "Value"D; = goog FinancialData @"TM", "Price", 82007, 1, 1<, "Value"D; FinancialData @"^FVX", "Name"D 5 Year Treasury Note = bond FinancialData @"^FVX", "Price", 82004, 1, 1<, "Value"D; Length @bond D 1458 Length @dowjones D 1460 = − data Transpose @8bond, dowjones @@1 ;; 3DD<D; ListPlot @data D 14 000 12 000 10 000 3 4 5 12 Korea2009FinancialPublic_for_PUBLIC.nb = + + + nlm NonlinearModelFit @data, a x b c ê Sin @d x eD, 8a, b, c, d, e<, xD FittedModel 7264.07 + á19à x + 0.132641 Csc 5.61136 - 4.11568 x B @ D F Normal @nlm D 7264.07 + 1012.17 x + 0.132641 Csc @5.61136 − 4.11568 xD Plot @nlm @xD, 8x, 0, 5<D 12 000 11 000 10 000 9000 1 2 3 4 5 Optimization Symbolic 2 − + Minimize A2 x 3 x 5, xE 31 3 : , :x → >> 8 4 Symbolic constrained − 2 + 2 ≤ Maximize A9x 2 y, x y 1=, 8x, y<E 4 2 : 5 , :x → − + 5 , y → − >> 5 5 = 2 + 2 ≤ − − g RegionPlot Ax y 1, 8x, 2, 2<, 8y, 2, 2<E − Solve @x 2 y c, yD 1 ::y → H−c + xL>> 2 − + − → − → Manipulate @Show @g, Plot @1 ê 2 H c xL, 8x, 2, 2<D, ImageSize 150 D, 8c, 3, 3<, ContentSize 8200, 180 <D Korea2009FinancialPublic_for_PUBLIC.nb 13 c Conditions + + Maximize @a x ^ 2 b x c, xD c b 0 && a 0 b 0 && a < 0 H L »» H L 0 Hb 0 && a 0L »» Hb 0 && a < 0L b2−4 a c b − b > 0 && a < 0 b < 0 && a < 0 , x → − b > 0 && a < 0 b < 0 && a < 0 : 4 a H L »» H L : 2 a H L »» H L >> ∞ True Indeterminate True Numerical = 2 − + Clear @f, xD; fAx_ E : 2 x 3 x 5; NMinimize @f@xD, xD 83.875, 8x → 0.75 << = pts First @Last @Reap @NMinimize @f@xD, x, EvaluationMonitor Sow @xDDDDD; − →