Previous Next

Build a Robo Advisor with Python (From Scratch) Automate your financial and investment decisions (Rob Reider, Alex Michalka)(Z-Library)

Author: Rob Reider, Alex Michalka

代码

Take control of your wealth management by building your own reliable, effective, and automated financial advisor tool. Automated digital financial advisors—also called robo-advisors—manage billions of dollars in assets. Follow the step-by-step instructions in this hands-on guide, and you’ll learn to build your robo-advisor capable of managing a real investing strategy. In Build a Robo-Advisor with Python (From Scratch) you’ll learn how to: • Measure returns and estimate the benefits of robo-advisors • Use Monte Carlo simulations to build and test financial planning tools • Construct diversified, efficient portfolios using optimization and other methods • Implement and evaluate rebalancing methods to track a target portfolio over time • Decrease taxes through tax-loss harvesting and optimized withdrawal sequencing • Use reinforcement learning to find the optimal investment path up to, and after, retirement Automated “robo-advisors” are commonplace in financial services, thanks to their ability to give high-quality investment advice at a fraction of the cost of human advisors. Build a Robo-Advisor with Python (From Scratch) teaches you to develop one of these powerful, flexible tools using popular and free Python libraries. You’ll master practical Python skills in demand in financial services, and financial planning skills that will help you take the best care of your money. All examples are accompanied by working Python code, and are easy to adjust for investors anywhere in the world. What's inside • Advanced portfolio construction techniques • Tax-loss harvesting, sequencing of retirement withdrawals, and asset location • Financial planning using AI and Monte Carlo simulations • Rebalancing methods to track a portfolio over time About the reader Accessible to anyone with a basic knowledge of Python and finance—no special skills required.

📄 File Format: PDF
💾 File Size: 19.1 MB
7
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
M A N N I N G Rob Reider Alex Michalka Automate your financial and investment decisions FROMSCRATCH with Python BUILD A
📄 Page 2
(This page has no text content)
📄 Page 3
Build a Robo-Advisor with Python (From Scratch) Rob Reider Alex Michalka M A N N I N G SHELTER ISLAND
📄 Page 4
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2025 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. ∞ Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editor: Doug Rudder Technical editor: Marcus Young Review editor: Aleksandar Dragosavljević Production editor: Deirdre Hiam Copy editor: Tiffany Taylor Proofreader: Mike Beady Typesetter: Ammar Taha Mohamedy Cover designer: Marija Tudor ISBN 9781633439672 Printed in the United States of America
📄 Page 5
brief contents Part 1 Basic tools and building blocks .............................1 1 The rise of robo-advisors 3 2 An introduction to portfolio construction 13 3 Estimating expected returns and covariances 31 4 ETFs: The building blocks of robo-portfolios 53 Part 2 Financial planning tools ...................................... 67 5 Monte Carlo simulations 69 6 Financial planning using reinforcement learning 96 7 Measuring and evaluating returns 118 8 Asset location 130 9 Tax-efficient withdrawal strategies 143 Part 3 Portfolio construction ...................................... 163 10 Optimization and portfolio construction 165 11 Asset allocation by risk: Introduction to risk parity 191 12 The Black-Litterman model 214 v
📄 Page 6
vi BRIEF CONTENTS Part 4 Portfolio management ........................................ 233 13 Rebalancing: Tracking a target portfolio 235 14 Tax-loss harvesting: Improving after-tax returns 273
📄 Page 7
contents preface xii acknowledgments xiii about this book xiv about the authors xvii about the cover illustration xviii Part 1 Basic tools and building blocks ............................ 1 1 The rise of robo-advisors 3 1.1 What are robo-advisors? 3 Key features of robo-advisors 4 Comparison of robo-advisors 5 Things robo-advisors don’t do 5 1.2 Advantages of robo-advisors 6 Low fees 6 Tax savings 6 Avoiding behavioral biases 7 Saving time 8 1.3 Example: Social Security benefits 8 1.4 Python and robo-advising 10 1.5 Who might be interested in learning about robo-advising? 11 2 An introduction to portfolio construction 13 2.1 A simple example with three assets 14 2.2 Computing a portfolio’s expected return and standard deviation 15 2.3 An illustrationwith randomweights 18 vii
📄 Page 8
viii CONTENTS 2.4 Introducing a risk-free asset 21 2.5 Risk tolerance 23 3 Estimating expected returns and covariances 31 3.1 Estimating expected returns 32 Historical averages 32 CAPM 34 Adjusting historical returns for changes in valuation 39 Capital market assumptions from asset managers 45 3.2 Estimating variances and covariances 45 Using historical returns 45 GARCH models 47 Other approaches 50 Subjective estimates 51 4 ETFs: The building blocks of robo-portfolios 53 4.1 ETF basics 54 ETF strategies 54 ETF pricing: Theory 55 ETF pricing: Reality 56 Costs of ETF investing 57 4.2 ETFs vs. mutual funds 58 Tradability 58 Costs and minimums 59 Tax efficiency 59 The verdict on mutual funds vs. ETFs 60 4.3 Total cost of ownership 61 Cost components 61 4.4 Beyond standard indices 62 Smart beta 63 Socially responsible investing 64 Part 2 Financial planning tools ....................................... 67 5 Monte Carlo simulations 69 5.1 Simulating returns in Python 71 5.2 Arithmetic vs. geometric average returns 74 5.3 Simple vs. continuously compounded returns 76 5.4 Geometric Brownian motion 77 5.5 Estimating the probability of success 78 5.6 Dynamic strategies 80 5.7 Inflation risk 82 5.8 Fat tails 87 5.9 Historical simulations and booststrapping 88 5.10 Longevity risk 90 5.11 Flexibility ofMonte Carlo simulations 93
📄 Page 9
CONTENTS ix 6 Financial planning using reinforcement learning 96 6.1 A goals-based investing example 97 6.2 An introduction to reinforcement learning 97 Solution using dynamic programming 100 Solution using Q-learning 105 6.3 Utility function approach 108 Understanding utility functions 108 Optimal spending using utility functions 110 6.4 Longevity risk 114 6.5 Other extensions 116 7 Measuring and evaluating returns 118 7.1 Time-weighted vs. dollar-weighted returns 119 Time-weighted returns 120 Dollar-weighted returns 120 7.2 Risk-adjusted returns 122 Sharpe ratio 122 Alpha 124 Evaluating an ESG fund’s performance 125 Which is better, alpha or Sharpe ratio? 128 8 Asset location 130 8.1 A simple example 131 8.2 The tax efficiency of various assets 135 8.3 Adding a Roth account 137 A simple example with three types of accounts 138 An example with optimization 139 8.4 Additional considerations 141 9 Tax-efficient withdrawal strategies 143 9.1 The intuition behind tax-efficient strategies 143 Principle 1: Deplete less tax-efficient accounts first 144 Principle 2: Keep tax brackets stable over time 144 9.2 Examples of sequencing strategies 145 Starting assumptions 145 Tax-sequencing code 146 Strategy 1: IRA first 149 Strategy 2: Taxable first 150 Strategy 3: Fill lower tax brackets 151 Strategy 4: Roth conversions 153 9.3 Additional complications 155 Required minimum distributions 155 Inheritance 156 Capital gains taxes 158 State taxes 160 Putting it all together 161
📄 Page 10
x CONTENTS Part 3 Portfolio construction ....................................... 163 10 Optimization and portfolio construction 165 10.1 Convex optimization in Python 166 Basics of optimization 166 Convexity 168 Python libraries for optimization 171 10.2 Mean-variance optimization 173 The basic problem 173 Adding more constraints 174 10.3 Optimization-based asset allocation 176 Minimal constraints 177 Enforcing diversification 182 Creating an efficient frontier 187 Building an ESG portfolio 188 11 Asset allocation by risk: Introduction to risk parity 191 11.1 Decomposing portfolio risk 192 Risk contributions 192 Risk concentration in a “diversified” portfolio 192 Risk parity as an optimal portfolio 193 11.2 Calculating risk-parity weights 195 Naive risk parity 195 General risk parity 195 Weighted risk parity 196 Hierarchical risk parity 200 11.3 Implementation of risk-parity portfolios 211 Applying leverage 212 12 The Black-Litterman model 214 12.1 Equilibrium returns 215 Reverse optimization 215 Understanding equilibrium 217 12.2 Conditional probability andBayes’ rule 218 12.3 Incorporating investor views 220 Expected returns as random variables 221 Expressing views 221 Updating equilibrium returns 222 Assumptions and parameters 223 12.4 Examples 224 Example: Sector selection 224 Example: Global allocation with cryptocurrencies 228 Part 4 Portfolio management ....................................... 233 13 Rebalancing: Tracking a target portfolio 235 13.1 Rebalancing basics 235 The need for rebalancing 236 Downsides of rebalancing 237 Dividends and deposits 237
📄 Page 11
CONTENTS xi 13.2 Simple rebalancing strategies 239 Fixed-interval rebalancing 239 Threshold-based rebalancing 239 Other considerations 240 Final thoughts 243 13.3 Optimizing rebalancing 243 Variables 243 Inputs 244 Formulating the problem 251 Running an example 253 13.4 Comparing rebalancing approaches 256 Implementing rebalancers 256 Building the backtester 261 Running backtests 268 Evaluating results 270 14 Tax-loss harvesting: Improving after-tax returns 273 14.1 The economics of tax-loss harvesting 274 Tax deferral 274 Rate conversion 276 When harvesting doesn’t help 277 14.2 The wash-sale rule 277 Wash-sale basics 278 Wash sales with Python 281 14.3 Deciding when to harvest 293 Trading costs 293 Opportunity cost 294 End-to-end evaluation 299 14.4 Testing a TLH strategy 304 Backtester modifications 304 Choosing ETFs 305 index 307
📄 Page 12
preface We hope readers will learn about both finance and Python by reading the book. It isn’t intended to teach either of those topics from the ground up—we expect that readers will have a basic understanding of probability and statistics, financial concepts, and Python programming—but accessibility is important to us. The balance of theory and implementation varies by chapter. Some chapters are very financially focused, and the Python content is limited to showing how a few functions from existing libraries can be used to perform certain calculations or accomplish desired tasks. In other chapters, there aren’t any existing Python libraries we can use. These chapters are much more code-heavy, and we essentially build new Python libraries implementing the concepts they cover. We know reading code isn’t always easy, so we show example usages of new code whenever possible to aid understanding. Much of the content related to laws or regulations, specific financial products, or types of retirement accounts is United States–focused. We’ve spent our careers in the United States, and the specifics we cover are what we know. However, the concepts we discuss should apply no matter where you live. Often there are non-US equivalents of topics we discuss—for example, a self-invested pension plan (SIPP) is essentially the United Kingdom’s version of a self-directed IRA in the United States. Your feedback is essential, and we invite you to send us an email at pythonroboad- visor@gmail.com or leave comments in the LiveBook discussion forum. Also, Rob plans to periodically write blog posts on the intersection of finance and Python on his website, pynancial.com. He looks forward to seeing comments and questions, sharing ideas, and collaborating there as well. We appreciate your interest and have done our best to produce the best book possible for you. xii
📄 Page 13
acknowledgments Writing this book was far more difficult and time-consuming than either of us expec- ted. We each have many people to thank. Alex thanks Rob for conceiving the book and asking him to coauthor it, Kyle for her understanding while he spent his nights and weekends at his desk, and his Wealthfront colleagues for their support initially and along the way. Rob thanks Jonathan Larkin (and many others at Quantopian) for igniting his interest in Python, Scott Feier for scrutinizing the manuscript and catching numerous errors, and Marty Reider for helping him set up and maintain the GitHub repository. We both thank the entireManning team, especially Doug Rudder for his incredible patience and helpful suggestions. Thank you also to our project manager Deirdre Hiam, our copy editor Tiffany Taylor, our proofreader Mike Beady, and Marcus Young for his technical proofing. We would also like to thank all of our reviewers: Ashutosh Sanzgiri, Christopher Kottmyer, Claudiu Schiller, Dan Sheikh, David Cronkite, David Patschke, Eli Mayost, Keerthi Shetty, Krzysztof Kamyczek, Laud Bentil, Laurens Meulman, Marco Carnini, Marco Seguri, Marcus H. Young, Maxim Volgin, Mohana Krishna, Oliver Korten, Oren Zeev-Ben-Mordehai, Philip Patterson, Rani Sharim, Richard Vaughan, Salil Athalye, Seung-jin Kim, Simone Sguazza, Sriram Macharla, and Vatsal Desai, your suggestions helped to make this a better book. xiii
📄 Page 14
about this book In Build a Robo-Advisor with Python (From Scratch), you’ll design and develop a working financial advisor that can manage a real investing strategy. You’ll add new features to your advisor chapter by chapter, including determining the optimal weight of cryptocurrency in your portfolio, rebalancing to keep your investments on target while minimizing taxes, and using reinforcement learning to find a “glide path” that can maximize how long your money will last in retirement. Best of all, the skills you learn in reinforcement learning, convex optimization, and Monte Carlo methods can be applied to numerous lucrative fields beyond the domain of finance. WHO SHOULD READ THIS BOOK Our target audience is anyone who is interested in finance and investments, who has some familiarity with Python, and who is interested in learning about how Python can be used to automate investment processes. You may be looking to apply the tools you’ll learn for your own finances or professionally, whether you’re interested in jobs in this area or you’re a financial advisor who wants to automate parts of your business. HOW THIS BOOK IS ORGANIZED: A ROADMAP The book is organized into four parts. Part One: Basic Tools and Building Blocks starts in chapter 1 by covering the robo-advisory landscape and what robo-advisors do. We then introduce some of the basic tools and concepts used in the financial industry: plots of risk versus (expected) reward and the efficient frontier (chapter 2); methods for estimating expected future returns, volatilities, and correlations (chapter 3); and evaluating the exchange-traded funds that are typically used to construct a portfolio of assets (chapter 4). xiv
📄 Page 15
ABOUT THIS BOOK xv Part Two: Financial Planning Tools shows how to automate some of the finan- cial planning services offered by advisors. Chapter 5 introduces Monte Carlo simulations and how to model various sources of risk to estimate the probability of running out of money in retirement. In chapter 6, we describe reinforcement learning and demonstrate, through several examples, how it can be applied to solving financial planning problems. Next, chapter 7 cover various methods for measuring returns when there are inflows and outflows and how to use risk- adjusted returns to evaluate the performance of investment managers. Chapters 8 and 9 discuss methods to reduce taxes. The first, asset location, involves strate- gically placing different types of assets in specific types of accounts to optimize tax efficiency. The second method analyzes various strategies for sequencing withdrawals during the decumulation phase of retirement when investors must draw down their savings to pay for expenses. Part Three: Portfolio Construction teaches methods for determining portfolio weights. In chapter 10, we show how to use inputs like expected returns, volati- lities, and correlations, as well as constraints or other considerations, to build “optimal” portfolios. We also highlight some of the pitfalls associated with using optimization to build portfolios. Then we discuss two methodologies designed to address these pitfalls: risk parity (chapter 11) and the Black–Litterman model (chapter 12). Part Four: Portfolio Management discusses how to manage a real portfolio over time after the target weights have been determined. Chapter 13 details several approaches to portfolio rebalancing (making trades to bring a portfolio’s weights in line with their targets), ranging from very simple to (somewhat) complex. Finally, in chapter 14, we discuss tax-loss harvesting, a way for investors to lower their taxes by opportunistically selling assets that have declined in value. Parts 2–4 can be read in any order, but we recommend starting with part 1. ABOUT THE CODE This book contains many examples of source code both in numbered listings and inline with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code. In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers ( ). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts. All the chapters contain examples in Python, and many include function and class definitions. These can be found on the book’s website (www.manning.com/bo- oks/build-a-robo-advisor-with-python-from-scratch) and the GitHub repository for
📄 Page 16
xvi ABOUT THIS BOOK this book (https://github.com/robreider/robo-advisor-with-python). Filenames cor- respond to chapter numbers. In each chapter, later code builds on earlier code. For example, a code section in the middle of a chapter may rely on a function defined earlier or a package imported earlier. Additionally, class definitions with many methods may be broken into multiple code sections. We recommend copying or importing the code from the website or GitHub rather than straight from the text if you are working in Python while reading an electronic version of the book. You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/build-a-robo-advisor-with-python- from-scratch. The complete code for the examples in the book is available for down- load from the Manning website at https://www.manning.com/books/build-a-robo- advisor-with-python-from-scratch. LIVEBOOK DISCUSSION FORUM Purchase of Build a Robo-Advisor with Python (From Scratch) includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach comments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical qu- estions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/book/build-a-robo-advisor-with-python-from- scratch/discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/discussion. Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contributions to the forum remain voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
📄 Page 17
about the authors Rob Reider has been a quantitative hedge fund portfolio manager for over 15 years. He holds a PhD in finance from The Wharton School and is an adjunct professor at NYU, where he teaches a graduate course in the Math-Finance department called “Time series analysis and statistical arbi- trage.” He has built asset-allocation models, financial plan- ning tools, and optimal tax strategies for a robo-advisor. Rob has given numerous lectures that combine Python with finance and has deve- loped an online course entitled “Time series analysis in Python.” As a hedge fund manager, Rob has been involved in all aspects of the investment process, from disco- vering new trading strategies to backtesting, executing, and managing risk. Alex Michalka has worked in finance and technology since 2006. He began his career developing weather deriva- tive pricing models at Weatherbill, spent six years conduc- ting research on quantitative equity portfolio construction at AQR Capital Management, and currently leads the in- vestments research group at Wealthfront. He holds a BA in applied mathematics from UC Berkeley and a PhD in operations research from Columbia University. xvii
📄 Page 18
about the cover illustration The figure on the cover of Build a Robo-Advisor with Python (From Scratch) is “Homme de schevelingen pres de la haye” or “Schevelingen man near the Hague,” taken from a nineteenth-century edition of Sylvain Maréchal’s four-volume compendium, Costumes Civils Actuels de Tous les Peuples Connus. Each illustration is finely drawn and colored by hand. In those days, it was easy to identify where people lived and what their trade or station in life was just by their dress. Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional culture centuries ago, brought back to life by pictures from collections such as this one. xviii
📄 Page 19
Part 1 Basic tools and building blocks Ourbook begins with a discussion of what robo-advisors do, both generally and through a comparison of some of the best-known robo-advisors in the market. We also outline some of the advantages of robo-advising, including low fees, tax savings, and avoiding behavioral biases. Chapter 2 explains some of the basic tools and concepts used in the financial industry. We show how to construct a portfolio of assets and how some asset allocations can give higher expected returns for the same amount of risk, which leads to the concept of the efficient frontier. The chapter ends by showing some of the questions robo-advisors use to help guide their clients into an appropriate portfolio. Chapter 3 discusses how to estimate some important quantities: the expected returns and volatilities of individual assets and the correlations between pairs of assets. These are essential for calculating the expected return and volatility of a portfolio and for building portfolios using mathematical optimization. Chapter 4 covers exchange-traded funds (ETFs). We’ll discuss how ETFs work, why they’re widely preferred over mutual funds by robo-advisors, and how to evaluate multiple ETFs competing in the same market segment.
📄 Page 20
(This page has no text content)
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List