Skip to content

Solving the linear system with scipy.sparse#5

Closed
fnattino wants to merge 2 commits intomainfrom
scipy-sparse-fn
Closed

Solving the linear system with scipy.sparse#5
fnattino wants to merge 2 commits intomainfrom
scipy-sparse-fn

Conversation

@fnattino
Copy link
Contributor

@fnattino fnattino commented Jun 24, 2025

I have worked out an example similar to what illustrated in https://github.com/MotionbyLearning/example_scripts/blob/main/solution_large_design_matrix/dask_array_sparse_mat.ipynb, but using only numpy and/or scipy. If I am not missing anything, by using scipy.sparse one seems to be able to solve a system of the same size in <3 sec (EDIT: "a few seconds" - if the tolerance is lowered in order to get equal accuracy to the "dense" counterpart, this takes ~5 secs).

@fnattino
Copy link
Contributor Author

Small setup differences with respect to https://github.com/MotionbyLearning/example_scripts/blob/main/solution_large_design_matrix/dask_array_sparse_mat.ipynb :

  • when setting up $A$, I made sure that the start point and the end point of each arc are not the same point (am I correct in this assumption @rogerkuou?)
  • when setting up $y$, I have set the probability of 0, 1 and -1 to 90%, 5% and 5%, respectively - are these reasonable @rogerkuou ?

@fnattino
Copy link
Contributor Author

fnattino commented Jun 24, 2025

scipy.sparse.linalg.lsqr, as np.linalg.lstsq only accepts $A$ and $x$, so more advanced optimizations (e.g. including weights) should be implemented outside these functions. It would be great to have some data/test examples where these are needed to check what can be done to implement them.

@fnattino
Copy link
Contributor Author

@rogerkuou I have updated the notebook, lowering the tolerance parameters in the optimization, so that the sparse approach gives identical results (e.g. results that are np.allclose) to the "dense" counterpart.

Concerning the direct comparison of A @ x to y, using the fitted x: I am not sure whether this actually makes sense. The problem that we are looking at looks more like a classification problem, with the A matrix also representing categorical values, right? So some transformations would need to be applied to A @ x in order to recover a matrix that look like the integer array y...

@rogerkuou
Copy link
Contributor

@fnattino Thanks a lot for the nice solution! As we already discussed, the x estimator does not make sense for now since we are randomly simutaing y in this example.

I created #6 , inheriting all your work. In that PR I made another solution notebook in ith a more sensibly simultade arcs. Now we can conclude that this solution does give mathematically sensible solution for both weight and non-weighted y, and is computationally efficient.

I will merge #6 and close this one

@rogerkuou rogerkuou closed this Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants