Can scipy be useful for economics and social sciences? The answer, of course, is yes.
One of the best known methods of minimising risk is insurance. However, it came into existence in the 1600's. Economic theory, like insurance, is heavily based on statistics. Economic models are not perfect as the global recession in the last few years have shown. It is not that modelling is wrong. Rather, more complex models are needed. Human behaviour cannot be ignored.
As models get more complex, spreadsheets with macros will not be adequate and more complex tools will be needed. An example of the possibilities is an interesting commercial product Resolver One which blends a spreadsheet-like interface with the ability to program in python(ironpython) and includes some support for NumPy.
It is likely that scipy will find proponents among social scientists as well. You can be among the early converts. Explore a few trivial examples in order to get an idea of the tools available.
The risk for an investment can be estimated by the standard deviation of the result. Consider the following options
Suppose a fraction p is invested in Investment 1 and the remaining (1-p) in Investment 2. What would be the resulting standard deviation? The textbook formula is
You can use the matplotlib to plot this equation for several values of correlation and visualise it. The plot is shown in Figure 1.
The safest investment has a mixture of both and gives a better return than the safer of the two.
The best proportion to use will be the one which minimises the standard deviation. It is easy to get that value. Just add the following code:
The function fmin in the optimize module accepts a function, std_dev in your case, as a parameter and a starting guess. Additional parameters for the std_dev function are passed using the args parameter. It then finds the optimal value. This should give you the result that you should have about 80% in first investment and 20% in the second as the safest option in case the investments are independent:
You know that the investments are usually correlated. So, you may want to examine visually how the result varies depending upon the correlation. Add the following code to compute the optimum proportion of Investment 1 for a list of correlations and plot it:
The result is shown in Figure 2. If both investments are perfectly correlated, then you are better with everything in Investment 1. However, if they are have a perfect negative correlation, the best option is to invest only 70% in Investment 1 and remaining in Investment 2. An example of negative correlation is the frequently heard advice: “When stock markets are booming, interest rates fall.”
If only finding the correct model in economics were easier!
Suppose the government can spend a 100 rupees on increasing consumption and production of food. It estimates that each rupee spent on Rural Employment will increase consumption by a rupee. This increase must be matched by an increase in production. It estimates that each rupee spent on infrastructure will increase production by 2 rupees. So, you can formulate the problem as:
You can use the scipy's linalg module to solve this problem as follows, where the four columns are c, p, r, i.
Suppose government can also waste money, w. That is
The first array in the result contains the recommended values for the 5 parameters. The solution is undesirable, even if realistic. It suggests that 59% of the money be wasted! Your model will need to minimise wastage.
Scipy as yet does not include a module for optimising a utility function subject to constraints. One project extending scipy for this functionality is OpenOpt. OpenOpt uses python-cvxopt as one of the solvers. So, you will need to install python-cvxopt, whichis available in the Fedora or Ubuntu repository. OpenOpt can be installed from http://openopt.org/.
You define the optimisation problem using an lp example described on the OpenOpt site, as follows:
If everything goes well, you should see the result that wastage should be zero.
You can explore alternate optimisations, e.g. maximize c (minimise -c) or minimise (w – c), etc. Your constraints can include inequalities as well.
OpenOpt includes non-linear optimisation tools as well.
Scipy and extensions make it easy to formulate and explore solutions of the problems. Matplotlib makes it easy to visualise the results. So, you can focus on the creative and hard part of defining the models and prevent any future crises in economics!
You can learn a lot from the courses available online. For example,
Python for Research >