- Page 2 and 3: Python for FinanceSecond EditionFin
- Page 4 and 5: CreditsAuthorYuxing YanProject Coor
- Page 8 and 9: www.PacktPub.comeBooks, discount of
- Page 10 and 11: Table of ContentsPrefaceixChapter 1
- Page 12 and 13: Table of ContentsAppendix F -how to
- Page 14 and 15: Table of ContentsAppendix C - data
- Page 16 and 17: Table of ContentsCapital budgeting
- Page 18 and 19: PrefaceIt is our firm belief that a
- Page 20 and 21: PrefaceChapter 2, Introduction to P
- Page 22 and 23: PrefaceChapter 13, Credit Risk Anal
- Page 24 and 25: ConventionsIn this book, you will f
- Page 26: PrefaceErrataAlthough we have taken
- Page 29 and 30: Python BasicsThere are several reas
- Page 31 and 32: Python BasicsThe top-left panel (wi
- Page 33 and 34: Python Basics4. From the IPython sh
- Page 35 and 36: Python BasicsIn the preceding codes
- Page 37 and 38: Python Basics(1+r)^nfv: fture value
- Page 39 and 40: Python BasicsAssuming that we inves
- Page 41 and 42: Python BasicsBy using a loop, we ca
- Page 43 and 44: Python BasicsThe print() function c
- Page 45 and 46: Python BasicsWe could retrieve data
- Page 47 and 48: Python BasicsData typesDescriptioni
- Page 49 and 50: Python BasicsFor example, from the
- Page 51 and 52: Python BasicsIn Chapter 3, Time Val
- Page 53 and 54: Python BasicsTo retrieve the file,
- Page 55 and 56: Python BasicsHere PV is the present
- Page 58 and 59:
Introduction toPython ModulesIn thi
- Page 60 and 61:
Chapter 2In addition, when a module
- Page 62 and 63:
Chapter 2Recall that in Chapter 1,
- Page 64 and 65:
Chapter 2To find a specific module,
- Page 66 and 67:
Chapter 2In the previous example, d
- Page 68 and 69:
Chapter 2----------a : array_likeCa
- Page 70 and 71:
Chapter 2If n returns are given, th
- Page 72 and 73:
Chapter 2Similarly, we could save a
- Page 74 and 75:
Chapter 2The next example presents
- Page 76 and 77:
Chapter 2Introduction to statsmodel
- Page 78 and 79:
Chapter 2From the preceding output,
- Page 80 and 81:
Chapter 2'core', 'crosstab', 'cut',
- Page 82 and 83:
x[bad] = np.nan# missing code is np
- Page 84 and 85:
[ 57 ]Chapter 2For finance, time se
- Page 86 and 87:
Chapter 2Stock A Stock B0 0.452820
- Page 88 and 89:
Chapter 22010-01-08 110.929466>> >d
- Page 90 and 91:
Chapter 2After clicking the logo sh
- Page 92 and 93:
For a Mac, we have the following co
- Page 94 and 95:
Chapter 2After answering y, the fol
- Page 96:
Chapter 213. What is wrong with the
- Page 99 and 100:
Time Value of MoneyIntroduction to
- Page 101 and 102:
Time Value of MoneyLater in this ch
- Page 103 and 104:
Time Value of MoneyNote that in the
- Page 105 and 106:
Time Value of MoneyFor perpetuity,
- Page 107 and 108:
Time Value of MoneyExample 1: >>>pv
- Page 109 and 110:
Time Value of MoneyThe following ta
- Page 111 and 112:
Time Value of MoneyExample 1: >>>pv
- Page 113 and 114:
Time Value of MoneyIf using the sam
- Page 115 and 116:
Time Value of MoneyWhile using just
- Page 117 and 118:
Time Value of MoneyThe related grap
- Page 119 and 120:
Time Value of MoneyNow, let's write
- Page 121 and 122:
Time Value of MoneyFor annuity due,
- Page 123 and 124:
Time Value of MoneyThe total future
- Page 125 and 126:
Time Value of MoneyNormally, if a s
- Page 127 and 128:
Time Value of MoneyAn annuity is de
- Page 129 and 130:
Time Value of MoneyTo find out the
- Page 131 and 132:
Time Value of MoneyThe output is sh
- Page 133 and 134:
Time Value of Moneydef pvGrowingAnn
- Page 135 and 136:
Time Value of Money15. We know that
- Page 137 and 138:
Sources of DataDiving into deeper c
- Page 139 and 140:
Sources of DataTo find the usage of
- Page 141 and 142:
Sources of DataTo view the first an
- Page 143 and 144:
Sources of DataIn the preceding pro
- Page 145 and 146:
Sources of Data>>>ret[0:3]array([-0
- Page 147 and 148:
Sources of DataAssume that two pric
- Page 149 and 150:
Sources of Datad0=x.datefor i in ra
- Page 151 and 152:
Sources of DataThe so-called candle
- Page 153 and 154:
Sources of DataRetrieving data from
- Page 155 and 156:
Sources of DataAssume that the data
- Page 157 and 158:
Sources of DataGenerating two dozen
- Page 159 and 160:
Sources of Data15 spreadBBB Moody's
- Page 161 and 162:
Sources of Data0 201512311 20130228
- Page 163 and 164:
Sources of DataAssume that TORQcq.p
- Page 165 and 166:
Sources of Datasigma = np.std(ret)x
- Page 167 and 168:
Sources of DataAppendix C - Python
- Page 169 and 170:
Sources of DataThe corresponding gr
- Page 171 and 172:
Sources of DataAppendix F -how to g
- Page 173 and 174:
Sources of Data3. Manually download
- Page 176 and 177:
Bond and Stock ValuationBond or fix
- Page 178 and 179:
Chapter 5In Chapter 3, Time Value o
- Page 180 and 181:
Chapter 5On the bottom, we have the
- Page 182 and 183:
Chapter 5By applying equation (8),
- Page 184 and 185:
Chapter 5Note that Rm=APR/m is from
- Page 186 and 187:
Chapter 5Interest rate quotationEff
- Page 188 and 189:
Chapter 5The related graph is given
- Page 190 and 191:
Chapter 5For this reason, a Python
- Page 192 and 193:
Chapter 5>>>w20.4878049>>>w1*1 + w2
- Page 194 and 195:
Chapter 5If a bond holder could con
- Page 196 and 197:
Based on this result, the YTM is 7.
- Page 198 and 199:
Chapter 5Stock valuationThere are s
- Page 200 and 201:
Chapter 5……(23)The selling pric
- Page 202 and 203:
Chapter 5Here,Equity is the market
- Page 204 and 205:
Here, PV is the present value, R is
- Page 206 and 207:
Chapter 5Appendix C - Python progra
- Page 208 and 209:
Chapter 5sellingPrice/(1+R)**nselli
- Page 210 and 211:
Chapter 5A3/A- 32.00 49.00 59.00 72
- Page 212 and 213:
Capital Asset Pricing ModelCapital
- Page 214 and 215:
Chapter 6Here is the covariance bet
- Page 216 and 217:
Chapter 6Here, yi is the ith observ
- Page 218 and 219:
default to the md5 hash or the url
- Page 220 and 221:
Chapter 6(beta,alpha,r_value,p_valu
- Page 222 and 223:
Chapter 60 1.0 NaN 1.51 1.5 1.0 2.0
- Page 224 and 225:
Chapter 6The first 10 lines are sho
- Page 226 and 227:
Chapter 6Saving our data to an Exce
- Page 228 and 229:
Simple string manipulationFor Pytho
- Page 230 and 231:
Chapter 6The output is shown here:T
- Page 232 and 233:
Chapter 6The two most used panels a
- Page 234 and 235:
Chapter 6ReferencesPlease refer to
- Page 236 and 237:
Chapter 6Note 2 - how to download a
- Page 238 and 239:
Chapter 616. From this chapter, we
- Page 240 and 241:
Multifactor Models andPerformance M
- Page 242 and 243:
The pandas DataFrame is used to con
- Page 244 and 245:
Chapter 7import statsmodels.api as
- Page 246 and 247:
Chapter 7When ratios of equity book
- Page 248 and 249:
Chapter 7To save space, we will not
- Page 250 and 251:
Chapter 7The corresponding output i
- Page 252 and 253:
Chapter 7The output is shown here:P
- Page 254 and 255:
Chapter 7The only modification is t
- Page 256 and 257:
Chapter 7y = pd.DataFrame({'key': [
- Page 258 and 259:
Chapter 7'SP500': [0.1,0.17, -0.05,
- Page 260 and 261:
Chapter 7Sometimes, we need to merg
- Page 262 and 263:
Appendix A - list of related Python
- Page 264 and 265:
Chapter 7The first and last several
- Page 266 and 267:
Chapter 7• http://canisius.edu/~y
- Page 268 and 269:
14. Data source: http://mba.tuck.da
- Page 270 and 271:
Time-Series AnalysisIn finance and
- Page 272 and 273:
There is one end of chapter problem
- Page 274 and 275:
Chapter 8In the preceding program,
- Page 276 and 277:
Function Description Examplesrelati
- Page 278 and 279:
Chapter 8The output is shown here:P
- Page 280 and 281:
ticker='IBM'begdate=(2013,1,1)endda
- Page 282 and 283:
Chapter 8Here, v2(v1) is the second
- Page 284 and 285:
2016-09-01 0.0025 0.0200 -0.0134 0.
- Page 286 and 287:
Chapter 8import numpy as npticker='
- Page 288 and 289:
Chapter 8Some books distinguish the
- Page 290 and 291:
Chapter 8ticker='ibm'begdate=(2013,
- Page 292 and 293:
Chapter 852-week high and low tradi
- Page 294 and 295:
Chapter 8The covariance between the
- Page 296 and 297:
ff=pd.read_pickle('c:/temp/ffDaily.
- Page 298 and 299:
Chapter 8The following Python progr
- Page 300 and 301:
Chapter 8Here, n is the number of o
- Page 302 and 303:
Chapter 8x=np.array(pd.read_csv(pat
- Page 304 and 305:
Chapter 8The output is shown here:[
- Page 306 and 307:
Chapter 8In the preceding example,
- Page 308 and 309:
Chapter 8• George, T.J., and Hwan
- Page 310 and 311:
The key part of the program used to
- Page 312 and 313:
Chapter 8For readers from schools w
- Page 314 and 315:
11. In addition, generate the simil
- Page 316 and 317:
Portfolio TheoryUnderstanding portf
- Page 318 and 319:
Chapter 9Here, is the portfolio var
- Page 320 and 321:
Chapter 9var=w1**2*var1 +w2**2*var2
- Page 322 and 323:
Chapter 9The graph is shown here:To
- Page 324 and 325:
Chapter 9Optimization terminated su
- Page 326 and 327:
Chapter 9Here, U is the utility fun
- Page 328 and 329:
Chapter 9print("meanAnnual, varAnnj
- Page 330 and 331:
[ 303 ]Chapter 9'SCD' 'SEF' 'SI' 'S
- Page 332 and 333:
output.columns=[ticker]return outpu
- Page 334 and 335:
The key command used is ret.T*ret.
- Page 336 and 337:
Chapter 9def sharpe(R,w):var = port
- Page 338 and 339:
2. Code for defining two functions:
- Page 340 and 341:
Chapter 9In one of the previous pro
- Page 342 and 343:
Chapter 9The output is shown here:A
- Page 344 and 345:
Chapter 9ret = sp.array(mean_return
- Page 346 and 347:
Chapter 9x = getData(ticker,begdate
- Page 348 and 349:
Chapter 9Here is the output:Since t
- Page 350 and 351:
Chapter 97. Click CSV on the right-
- Page 352 and 353:
Chapter 90 10006 19570301 198407181
- Page 354 and 355:
Chapter 911. For several stocks suc
- Page 356 and 357:
Chapter 910. Update yanMonthly.pkl,
- Page 358:
Chapter 916. Estimating the Sharpe
- Page 361 and 362:
Options and Futures• Using the bi
- Page 363 and 364:
Options and FuturesTaking the natur
- Page 365 and 366:
Options and Futuress0=1.25rHome=0.0
- Page 367 and 368:
Options and FuturescontractFuturesS
- Page 369 and 370:
Options and FuturesTo create a grap
- Page 371 and 372:
Options and FuturesA graphical repr
- Page 373 and 374:
Options and FuturesEuropean versus
- Page 375 and 376:
Options and FuturesGenerating our o
- Page 377 and 378:
Options and FuturesThe first line o
- Page 379 and 380:
Options and FuturesThe related grap
- Page 381 and 382:
Options and Futuresx3=60; c3=5y1=(a
- Page 383 and 384:
Options and FuturesThe correspondin
- Page 385 and 386:
Options and FuturesNote that in the
- Page 387 and 388:
Options and FuturesOn the other han
- Page 389 and 390:
Options and FuturesThe following co
- Page 391 and 392:
Options and FuturesThe correspondin
- Page 393 and 394:
Options and Futuresplt.figtext(0.75
- Page 395 and 396:
Options and FuturesHere, u is the u
- Page 397 and 398:
Options and Futures# at level 2su=r
- Page 399 and 400:
Options and Futuresd = 1.0 / ua = e
- Page 401 and 402:
Options and FuturesFor the currency
- Page 403 and 404:
Options and FuturesSimilarly, we co
- Page 405 and 406:
Options and FuturesvolHigh=1.0cLow=
- Page 407 and 408:
Options and Futuresmin_value=1000fo
- Page 409 and 410:
Options and FuturesAppendix A - dat
- Page 411 and 412:
Options and FuturesNote that -1 mea
- Page 413 and 414:
Options and Futures16. The current
- Page 416 and 417:
Value at RiskIn finance, implicitly
- Page 418 and 419:
Chapter 11d1=stats.norm.pdf(0,0.1,0
- Page 420 and 421:
Chapter 11s=sp.arange(-10,z,delta)f
- Page 422 and 423:
Chapter 11ret=f(x)plt.plot(x,ret)x2
- Page 424 and 425:
Chapter 11z=norm.ppf(1-confidence_l
- Page 426 and 427:
Chapter 11#print(retNdays.head())po
- Page 428 and 429:
Chapter 11p =getData(ticker, begdat
- Page 430 and 431:
Chapter 11n=5000000#ret = random.no
- Page 432 and 433:
Chapter 11VaR based on sorted histo
- Page 434 and 435:
leftTail=int(n*(1-confidence_level)
- Page 436 and 437:
std=np.std(ret)#n_simulation=5000sp
- Page 438 and 439:
for i in np.arange(n):stock=tickers
- Page 440 and 441:
Chapter 11Again, we expect to see 2
- Page 442 and 443:
Chapter 11#sum=0.0for i in np.arang
- Page 444 and 445:
Chapter 11Estimation of an n-day Va
- Page 446 and 447:
Chapter 1110. When we estimate a Va
- Page 448 and 449:
Monte Carlo SimulationMonte Carlo S
- Page 450 and 451:
Chapter 12This program is equivalen
- Page 452 and 453:
Chapter 12Here, f(x) is the density
- Page 454 and 455:
Chapter 12import matplotlib.pyplot
- Page 456 and 457:
Chapter 12Using simulation to estim
- Page 458 and 459:
Chapter 12Generating random numbers
- Page 460 and 461:
Chapter 12#for i in range(len(nonSt
- Page 462 and 463:
Chapter 12The constraint specified
- Page 464 and 465:
Chapter 12Simulation of stock price
- Page 466 and 467:
Chapter 12Graphical presentation of
- Page 468 and 469:
Chapter 12y2=rho*x1+sp.sqrt(1-rho**
- Page 470 and 471:
dt=T/n_stepscall=sp.zeros([n_simula
- Page 472 and 473:
Chapter 12Based on the preceding re
- Page 474 and 475:
Chapter 12We have the following equ
- Page 476 and 477:
print("max NPV of project=",round(m
- Page 478 and 479:
Chapter 12One alternative is a so-c
- Page 480 and 481:
Chapter 12totalCost=salaryCost + ad
- Page 482 and 483:
[ 455 ]Chapter 12In other words, we
- Page 484 and 485:
Chapter 12Constructing an efficient
- Page 486 and 487:
Chapter 12The graph is shown here:I
- Page 488 and 489:
Chapter 12The following program ref
- Page 490 and 491:
Chapter 12On the other hand, if we
- Page 492 and 493:
4. To convert random numbers from a
- Page 494 and 495:
Credit Risk AnalysisThe objective o
- Page 496 and 497:
Chapter 136 A3 A- A- 1 17 Baa1 BBB+
- Page 498 and 499:
Chapter 13Source: Moody's (2007).No
- Page 500 and 501:
Chapter 13A secured debt is a debt
- Page 502 and 503:
If P is the default probability we
- Page 504 and 505:
14 B2/B 455.800 481.600 505.200 531
- Page 506 and 507:
Chapter 13Using the KMV model to es
- Page 508 and 509:
Chapter 13The following Python prog
- Page 510 and 511:
Chapter 13The related graph is show
- Page 512 and 513:
Chapter 13output.append(round(R,5))
- Page 514 and 515:
Chapter 13CDS P CDS P CDS P CDS P C
- Page 516 and 517:
8. What are uses of the term struct
- Page 518 and 519:
Exotic OptionsIn Chapter 10, Option
- Page 520 and 521:
Chapter 14x=40. # exercise priceT=6
- Page 522 and 523:
Chapter 14Here, call (T) is the cal
- Page 524 and 525:
Chapter 14v[i][j]=max(v1,v2)return
- Page 526 and 527:
Chapter 14n = mu.shape[0]Sigma_det
- Page 528 and 529:
Chapter 14First, we should study th
- Page 530 and 531:
Chapter 14sigma1=0.15sigma2=0.20pri
- Page 532 and 533:
Chapter 14The final price would be
- Page 534 and 535:
Chapter 14total+=sTprice_average=to
- Page 536 and 537:
Chapter 14#if sT<barrier:in_=True#p
- Page 538 and 539:
Chapter 14c=p4f.bs_call(s,x,T,r,sig
- Page 540 and 541:
Chapter 14The Python code for this
- Page 542 and 543:
Chapter 14If we use futures to hedg
- Page 544 and 545:
Chapter 14Here, T is the maturity d
- Page 546:
Chapter 14SummaryThe options we've
- Page 549 and 550:
Volatility, Implied Volatility, ARC
- Page 551 and 552:
Volatility, Implied Volatility, ARC
- Page 553 and 554:
Volatility, Implied Volatility, ARC
- Page 555 and 556:
Volatility, Implied Volatility, ARC
- Page 557 and 558:
Volatility, Implied Volatility, ARC
- Page 559 and 560:
Volatility, Implied Volatility, ARC
- Page 561 and 562:
Volatility, Implied Volatility, ARC
- Page 563 and 564:
Volatility, Implied Volatility, ARC
- Page 565 and 566:
Volatility, Implied Volatility, ARC
- Page 567 and 568:
Volatility, Implied Volatility, ARC
- Page 569 and 570:
Volatility, Implied Volatility, ARC
- Page 571 and 572:
Volatility, Implied Volatility, ARC
- Page 573 and 574:
Volatility, Implied Volatility, ARC
- Page 575 and 576:
Volatility, Implied Volatility, ARC
- Page 578 and 579:
IndexSymbols2-Step Approach 1522-st
- Page 580 and 581:
merging 228-234merging, based on da
- Page 582 and 583:
Monte Carlo Simulationabout 408, 42
- Page 584 and 585:
Python SimPy module 449QQuasi Monte