10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

output.columns=[ticker]

return output

final=ret_f(s[0])

for i in sp.arange(1,n_stocks):

ret=ret_f(s[i])

final=pd.merge(final,ret,left_index=True, right_index=True)

Chapter 9

To randomly choose m stocks from a set of existing available stocks (n of them), see

the command of scipy.random.uniform(low=1,high=len(x4),size=n_stocks).

Since n_stocks has a value of 10, we choose 10 stocks from len(x4). The output is

shown here:

IO A AA KB DELL IN

\

DATE

20110930 -0.330976 -0.152402 -0.252006 -0.206395 -0.048679 -0.115332

20111031 0.610994 0.185993 0.124464 0.192002 0.117690 0.237730

20111130 -0.237533 0.011535 -0.066794 -0.106274 -0.002616 -0.090458

20111230 0.055077 -0.068422 -0.135992 -0.102006 -0.072131 -0.065395

20120131 0.212072 0.215972 0.173964 0.209317 0.178092 0.230321

INF IBM SKK BC

DATE

20110930 -0.228456 0.017222 0.227586 -0.116382

20111031 0.142429 0.055822 -0.305243 0.257695

20111130 -0.038058 0.022314 -0.022372 0.057484

20111230 0.059345 -0.021882 -0.024262 -0.030140

20120131 0.079202 0.047379 -0.142131 0.182020

In finance, constructing an efficient frontier is always a challenging job. This is

especially true with real-world data. In this section, we discuss the estimation of a

variance-covariance matrix and its optimization, finding an optimal portfolio, and

constructing an efficient frontier with stock data downloaded from Yahoo! Finance.

When a return matrix is given, we could estimate its variance-covariance matrix. For

a given set of weights, we could further estimate the portfolio variance. The formula

to estimate the variance and standard deviation for returns from a single stock are

given as follows:

[ 305 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!