Show More
@@ -841,13 +841,13 b' The following sample file illustrating how to use the embedding' | |||
|
841 | 841 | functionality is provided in the examples directory as example-embed.py. |
|
842 | 842 | It should be fairly self-explanatory: |
|
843 | 843 | |
|
844 | .. literalinclude:: ../../examples/core/example-embed.py | |
|
844 | .. literalinclude:: ../../../examples/core/example-embed.py | |
|
845 | 845 | :language: python |
|
846 | 846 | |
|
847 | 847 | Once you understand how the system functions, you can use the following |
|
848 | 848 | code fragments in your programs which are ready for cut and paste: |
|
849 | 849 | |
|
850 | .. literalinclude:: ../../examples/core/example-embed-short.py | |
|
850 | .. literalinclude:: ../../../examples/core/example-embed-short.py | |
|
851 | 851 | :language: python |
|
852 | 852 | |
|
853 | 853 | Using the Python debugger (pdb) |
@@ -1111,7 +1111,7 b' want to continue, you simply execute the next block of the demo. The' | |||
|
1111 | 1111 | following listing shows the markup necessary for dividing a script into |
|
1112 | 1112 | sections for execution as a demo: |
|
1113 | 1113 | |
|
1114 | .. literalinclude:: ../../examples/lib/example-demo.py | |
|
1114 | .. literalinclude:: ../../../examples/lib/example-demo.py | |
|
1115 | 1115 | :language: python |
|
1116 | 1116 | |
|
1117 | 1117 | In order to run a file as a demo, you must first make a Demo object out |
@@ -103,7 +103,7 b' Map results are iterable!' | |||
|
103 | 103 | When an AsyncResult object has multiple results (e.g. the :class:`~AsyncMapResult` |
|
104 | 104 | object), you can actually iterate through results themselves, and act on them as they arrive: |
|
105 | 105 | |
|
106 | .. literalinclude:: ../../examples/parallel/itermapresult.py | |
|
106 | .. literalinclude:: ../../../examples/parallel/itermapresult.py | |
|
107 | 107 | :language: python |
|
108 | 108 | :lines: 20-67 |
|
109 | 109 |
@@ -81,7 +81,7 b' The code to generate the simple DAG:' | |||
|
81 | 81 | For demonstration purposes, we have a function that generates a random DAG with a given |
|
82 | 82 | number of nodes and edges. |
|
83 | 83 | |
|
84 | .. literalinclude:: ../../examples/parallel/dagdeps.py | |
|
84 | .. literalinclude:: ../../../examples/parallel/dagdeps.py | |
|
85 | 85 | :language: python |
|
86 | 86 | :lines: 20-36 |
|
87 | 87 | |
@@ -140,7 +140,7 b' These objects store a variety of metadata about each task, including various tim' | |||
|
140 | 140 | We can validate that the dependencies were respected by checking that each task was |
|
141 | 141 | started after all of its predecessors were completed: |
|
142 | 142 | |
|
143 | .. literalinclude:: ../../examples/parallel/dagdeps.py | |
|
143 | .. literalinclude:: ../../../examples/parallel/dagdeps.py | |
|
144 | 144 | :language: python |
|
145 | 145 | :lines: 64-70 |
|
146 | 146 |
@@ -99,7 +99,7 b' compute the two digit counts for the digits in a single file. Then in a final' | |||
|
99 | 99 | step the counts from each engine will be added up. To perform this |
|
100 | 100 | calculation, we will need two top-level functions from :file:`pidigits.py`: |
|
101 | 101 | |
|
102 | .. literalinclude:: ../../examples/parallel/pi/pidigits.py | |
|
102 | .. literalinclude:: ../../../examples/parallel/pi/pidigits.py | |
|
103 | 103 | :language: python |
|
104 | 104 | :lines: 47-62 |
|
105 | 105 | |
@@ -186,78 +186,6 b' compared to the 10,000 digit calculation.' | |||
|
186 | 186 | |
|
187 | 187 | .. image:: figs/two_digit_counts.* |
|
188 | 188 | |
|
189 | ||
|
190 | Parallel options pricing | |
|
191 | ======================== | |
|
192 | ||
|
193 | An option is a financial contract that gives the buyer of the contract the | |
|
194 | right to buy (a "call") or sell (a "put") a secondary asset (a stock for | |
|
195 | example) at a particular date in the future (the expiration date) for a | |
|
196 | pre-agreed upon price (the strike price). For this right, the buyer pays the | |
|
197 | seller a premium (the option price). There are a wide variety of flavors of | |
|
198 | options (American, European, Asian, etc.) that are useful for different | |
|
199 | purposes: hedging against risk, speculation, etc. | |
|
200 | ||
|
201 | Much of modern finance is driven by the need to price these contracts | |
|
202 | accurately based on what is known about the properties (such as volatility) of | |
|
203 | the underlying asset. One method of pricing options is to use a Monte Carlo | |
|
204 | simulation of the underlying asset price. In this example we use this approach | |
|
205 | to price both European and Asian (path dependent) options for various strike | |
|
206 | prices and volatilities. | |
|
207 | ||
|
208 | The code for this example can be found in the :file:`docs/examples/parallel/options` | |
|
209 | directory of the IPython source. The function :func:`price_options` in | |
|
210 | :file:`mckernel.py` implements the basic Monte Carlo pricing algorithm using | |
|
211 | the NumPy package and is shown here: | |
|
212 | ||
|
213 | .. literalinclude:: ../../examples/parallel/options/mckernel.py | |
|
214 | :language: python | |
|
215 | ||
|
216 | To run this code in parallel, we will use IPython's :class:`LoadBalancedView` class, | |
|
217 | which distributes work to the engines using dynamic load balancing. This | |
|
218 | view is a wrapper of the :class:`Client` class shown in | |
|
219 | the previous example. The parallel calculation using :class:`LoadBalancedView` can | |
|
220 | be found in the file :file:`mcpricer.py`. The code in this file creates a | |
|
221 | :class:`LoadBalancedView` instance and then submits a set of tasks using | |
|
222 | :meth:`LoadBalancedView.apply` that calculate the option prices for different | |
|
223 | volatilities and strike prices. The results are then plotted as a 2D contour | |
|
224 | plot using Matplotlib. | |
|
225 | ||
|
226 | .. literalinclude:: ../../examples/parallel/options/mcpricer.py | |
|
227 | :language: python | |
|
228 | ||
|
229 | To use this code, start an IPython cluster using :command:`ipcluster`, open | |
|
230 | IPython in the pylab mode with the file :file:`mckernel.py` in your current | |
|
231 | working directory and then type: | |
|
232 | ||
|
233 | .. sourcecode:: ipython | |
|
234 | ||
|
235 | In [7]: run mcpricer.py | |
|
236 | ||
|
237 | Submitted tasks: 30 | |
|
238 | ||
|
239 | Once all the tasks have finished, the results can be plotted using the | |
|
240 | :func:`plot_options` function. Here we make contour plots of the Asian | |
|
241 | call and Asian put options as function of the volatility and strike price: | |
|
242 | ||
|
243 | .. sourcecode:: ipython | |
|
244 | ||
|
245 | In [8]: plot_options(sigma_vals, strike_vals, prices['acall']) | |
|
246 | ||
|
247 | In [9]: plt.figure() | |
|
248 | Out[9]: <matplotlib.figure.Figure object at 0x18c178d0> | |
|
249 | ||
|
250 | In [10]: plot_options(sigma_vals, strike_vals, prices['aput']) | |
|
251 | ||
|
252 | These results are shown in the two figures below. On our 15 engines, the | |
|
253 | entire calculation (15 strike prices, 15 volatilities, 100,000 paths for each) | |
|
254 | took 37 seconds in parallel, giving a speedup of 14.1x, which is comparable | |
|
255 | to the speedup observed in our previous example. | |
|
256 | ||
|
257 | .. image:: figs/asian_call.* | |
|
258 | ||
|
259 | .. image:: figs/asian_put.* | |
|
260 | ||
|
261 | 189 | Conclusion |
|
262 | 190 | ========== |
|
263 | 191 |
General Comments 0
You need to be logged in to leave comments.
Login now