Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Solving linear equations using SciPy


Assume that we have the following three equations:

We define A and B as follows:

The solution is as follows:

The related Python code is as follows:

>>>import scipy as sp
>>>import numpy as np
>>>A=sp.mat('[1 2 5; 2 5 1; 2 3 8]')
>>>b = sp.mat('[10;8;5]')
>>>A.I*b      
Matrix([-22.45, 10.09, 2.45]
>>>np.linalg.solve(A,b)  # offer the same solution