Skip to content

Loops over columns vs loops over rows #20

@cantaro86

Description

@cantaro86

For loops over rows is faster than over columns.

I have already updated the code in commit 8fe948e, where this code:

X = np.zeros((paths, N))
for t in range(0, N - 1):
    X[:, t + 1] = self.theta + np.exp(-self.kappa * dt) * (X[:, t] - self.theta) + std_dt * W[:, t]

was replaced by this:

X = np.zeros((N, paths))
for t in range(0, N - 1):
    X[t + 1, :] = self.theta + np.exp(-self.kappa * dt) * (X[t, :] - self.theta) + std_dt * W[t, :]  

(there was no need to swap rows and cols where the code was vectorized and there are no for loops)

To improve the speed, we should invert rows and columns in each PDE solver. There are many PDEs in this repo, so it is long and hard work. I'm not going to work on this shortly.

In addition to swapping the columns with the rows, we also need to place boundary conditions in different positions inside the matrix.
For instance, for a problem that goes backward in time (such as the BS PDE in notebook 2.1), I used to define the terminal BC in the last column. In the new version, the "terminal BC" should be placed in the last row.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestwontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions