Hands-On Python for Finance by Krish Naik
Author:Krish Naik
Language: eng
Format: epub
Tags: COM051360 - COMPUTERS / Programming Languages / Python, COM018000 - COMPUTERS / Data Processing, COM062000 - COMPUTERS / Data Modeling and Design
Publisher: Packt Publishing
Published: 2019-03-29T17:45:31+00:00
portfolio_returns=[]
portfolio_volatilities=[]
for x in range(1000):
weights=np.random.random(num_assets)
weights/=np.sum(weights)
portfolio_returns.append(np.sum(weights*log_returns.mean())*250)
portfolio_volatilities.append(np.sqrt(np.dot(weights.T,np.dot(log_returns.cov(),weights))))
portfolio_returns,portfolio_volatilities
The output generated is shown in the following screenshot:
In the loop, we are generating two weights, whose sums equal one. We need two weights, because, as we already mentioned, the portfolios are composed of two assets. As the number of assets increases, the number of weights also increases. We are using the append method, which will add each newly-generated portfolio return value to the list of portfolio returns. This operation will be repeated for each pass of the loop, 1,000 times until the portfolio returns list accumulates 1,000 observations. We repeat the same procedure for the portfolio volatilities. We will use the append method and apply the formula for standard deviation. The output of the preceding code is a list and is hard to manipulate, so we convert it into a NumPy array, as follows:
In[37]:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Access | Data Mining |
Data Modeling & Design | Data Processing |
Data Warehousing | MySQL |
Oracle | Other Databases |
Relational Databases | SQL |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8300)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6741)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6716)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6593)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6377)
Driving Data Quality with Data Contracts by Andrew Jones(6326)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(6093)
Learning SQL by Alan Beaulieu(5995)
Weapons of Math Destruction by Cathy O'Neil(5779)
Big Data Analysis with Python by Ivan Marin(5363)
Data Engineering with dbt by Roberto Zagni(4361)
Solidity Programming Essentials by Ritesh Modi(4009)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3870)
Pandas Cookbook by Theodore Petrou(3578)
Blockchain Basics by Daniel Drescher(3294)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2905)
Feature Store for Machine Learning by Jayanth Kumar M J(2814)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2796)
Mastering Python for Finance by Unknown(2744)
