|
| 1 | +""" |
| 2 | +Utility functions used in CompEcon |
| 3 | +
|
| 4 | +Based routines found in the CompEcon toolbox by Miranda and Fackler. |
| 5 | +
|
| 6 | +References |
| 7 | +---------- |
| 8 | +Miranda, Mario J, and Paul L Fackler. Applied Computational Economics |
| 9 | +and Finance, MIT Press, 2002. |
| 10 | +
|
| 11 | +""" |
| 12 | +from functools import reduce |
| 13 | +import numpy as np |
| 14 | +import torch |
| 15 | + |
| 16 | +def _gridmake2(x1, x2): |
| 17 | + """ |
| 18 | + Expands two vectors (or matrices) into a matrix where rows span the |
| 19 | + cartesian product of combinations of the input arrays. Each column of the |
| 20 | + input arrays will correspond to one column of the output matrix. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + x1 : np.ndarray |
| 25 | + First vector to be expanded. |
| 26 | +
|
| 27 | + x2 : np.ndarray |
| 28 | + Second vector to be expanded. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + out : np.ndarray |
| 33 | + The cartesian product of combinations of the input arrays. |
| 34 | +
|
| 35 | + Notes |
| 36 | + ----- |
| 37 | + Based of original function ``gridmake2`` in CompEcon toolbox by |
| 38 | + Miranda and Fackler. |
| 39 | +
|
| 40 | + References |
| 41 | + ---------- |
| 42 | + Miranda, Mario J, and Paul L Fackler. Applied Computational Economics |
| 43 | + and Finance, MIT Press, 2002. |
| 44 | +
|
| 45 | + """ |
| 46 | + if x1.ndim == 1 and x2.ndim == 1: |
| 47 | + return np.column_stack([np.tile(x1, x2.shape[0]), |
| 48 | + np.repeat(x2, x1.shape[0])]) |
| 49 | + elif x1.ndim > 1 and x2.ndim == 1: |
| 50 | + first = np.tile(x1, (x2.shape[0], 1)) |
| 51 | + second = np.repeat(x2, x1.shape[0]) |
| 52 | + return np.column_stack([first, second]) |
| 53 | + else: |
| 54 | + raise NotImplementedError("Come back here") |
| 55 | + |
| 56 | + |
| 57 | +def _gridmake2_torch(x1: torch.Tensor, x2: torch.Tensor) -> torch.Tensor: |
| 58 | + """ |
| 59 | + PyTorch version of _gridmake2. |
| 60 | +
|
| 61 | + Expands two tensors into a matrix where rows span the cartesian product |
| 62 | + of combinations of the input tensors. Each column of the input tensors |
| 63 | + will correspond to one column of the output matrix. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + x1 : torch.Tensor |
| 68 | + First tensor to be expanded. |
| 69 | +
|
| 70 | + x2 : torch.Tensor |
| 71 | + Second tensor to be expanded. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + out : torch.Tensor |
| 76 | + The cartesian product of combinations of the input tensors. |
| 77 | +
|
| 78 | + Notes |
| 79 | + ----- |
| 80 | + Based on original function ``gridmake2`` in CompEcon toolbox by |
| 81 | + Miranda and Fackler. |
| 82 | +
|
| 83 | + References |
| 84 | + ---------- |
| 85 | + Miranda, Mario J, and Paul L Fackler. Applied Computational Economics |
| 86 | + and Finance, MIT Press, 2002. |
| 87 | +
|
| 88 | + """ |
| 89 | + if x1.dim() == 1 and x2.dim() == 1: |
| 90 | + # tile x1 by x2.shape[0] times, repeat_interleave x2 by x1.shape[0] |
| 91 | + first = x1.tile(x2.shape[0]) |
| 92 | + second = x2.repeat_interleave(x1.shape[0]) |
| 93 | + return torch.column_stack([first, second]) |
| 94 | + elif x1.dim() > 1 and x2.dim() == 1: |
| 95 | + # tile x1 along first dimension |
| 96 | + first = x1.tile(x2.shape[0], 1) |
| 97 | + second = x2.repeat_interleave(x1.shape[0]) |
| 98 | + return torch.column_stack([first, second]) |
| 99 | + else: |
| 100 | + raise NotImplementedError("Come back here") |
0 commit comments