[SciPy-Dev] Function to check if sparse matrix is symmetric

Saullo Castro saullogiovani at gmail.com
Fri Oct 10 14:07:48 EDT 2014


If the community says "yes, why not..." I can submit a push request with
the "is_Hermitian" function... please, let me know your opinion....

Saullo

2014-10-10 20:05 GMT+02:00 <scipy-dev-request at scipy.org>:

> Send SciPy-Dev mailing list submissions to
>         scipy-dev at scipy.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://mail.scipy.org/mailman/listinfo/scipy-dev
> or, via email, send a message with subject or body 'help' to
>         scipy-dev-request at scipy.org
>
> You can reach the person managing the list at
>         scipy-dev-owner at scipy.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of SciPy-Dev digest..."
>
>
> Today's Topics:
>
>    1. Re: Function to check if sparse matrix is symmetric       (Saullo
>       Castro) (Saullo Castro)
>    2. Re: [pystatsmodels] Re: Bootstrap confidence limits code
>       (Clark Fitzgerald)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 10 Oct 2014 20:00:57 +0200
> From: Saullo Castro <saullogiovani at gmail.com>
> Subject: Re: [SciPy-Dev] Function to check if sparse matrix is
>         symmetric       (Saullo Castro)
> To: Scipy - Dev <scipy-dev at scipy.org>
> Message-ID:
>         <
> CAHbwRz4gFGPZFX2huOyfYDuLMtkvCpyPzuARgaxGEAY4A6Vdvw at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> A check "is_Hermitian" would involve basically the same function, adding
> something like:
>
> m = m.real
>
> and checking if the diagonal elements are all real...
>
> Saullo
>
> 2014-10-10 19:00 GMT+02:00 <scipy-dev-request at scipy.org>:
>
> > Send SciPy-Dev mailing list submissions to
> >         scipy-dev at scipy.org
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >         http://mail.scipy.org/mailman/listinfo/scipy-dev
> > or, via email, send a message with subject or body 'help' to
> >         scipy-dev-request at scipy.org
> >
> > You can reach the person managing the list at
> >         scipy-dev-owner at scipy.org
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of SciPy-Dev digest..."
> >
> >
> > Today's Topics:
> >
> >    1. Function to check if sparse matrix is symmetric (Saullo Castro)
> >    2. Re: Function to check if sparse matrix is symmetric (Nils Wagner)
> >
> >
> > ----------------------------------------------------------------------
> >
> > Message: 1
> > Date: Fri, 10 Oct 2014 12:00:06 +0200
> > From: Saullo Castro <saullogiovani at gmail.com>
> > Subject: [SciPy-Dev] Function to check if sparse matrix is symmetric
> > To: Scipy - Dev <scipy-dev at scipy.org>
> > Message-ID:
> >         <CAHbwRz46P8zbNP=
> > i5VH_fbMtJq0E_UDhLfEqQxNeq3c10-7cEQ at mail.gmail.com>
> > Content-Type: text/plain; charset="utf-8"
> >
> > I developed a function (shown below) to check if a sparse matrix is
> > symmetric and would like to know if the community is interested to
> include
> > in scipy.sparse.
> >
> > Regards,
> > Saullo
> >
> > def is_symmetric(m):
> >     """Check if a sparse matrix is symmetric
> >
> >     Parameters
> >     ----------
> >     m : array or sparse matrix
> >         A square matrix.
> >
> >     Returns
> >     -------
> >     check : bool
> >         The check result.
> >
> >     """
> >     if m.shape[0] != m.shape[1]:
> >         raise ValueError('m must be a square matrix')
> >
> >     if not isinstance(m, coo_matrix):
> >         m = coo_matrix(m)
> >
> >     r, c, v = m.row, m.col, m.data
> >     tril_no_diag = r > c
> >     triu_no_diag = c > r
> >
> >     if triu_no_diag.sum() != tril_no_diag.sum():
> >         return False
> >
> >     rl = r[tril_no_diag]
> >     cl = c[tril_no_diag]
> >     vl = v[tril_no_diag]
> >     ru = r[triu_no_diag]
> >     cu = c[triu_no_diag]
> >     vu = v[triu_no_diag]
> >
> >     sortl = np.lexsort((cl, rl))
> >     sortu = np.lexsort((ru, cu))
> >     vl = vl[sortl]
> >     vu = vu[sortu]
> >
> >     check = np.allclose(vl, vu)
> >
> >     return check
> > -------------- next part --------------
> > An HTML attachment was scrubbed...
> > URL:
> >
> http://mail.scipy.org/pipermail/scipy-dev/attachments/20141010/b4027017/attachment-0001.html
> >
> > ------------------------------
> >
> > Message: 2
> > Date: Fri, 10 Oct 2014 12:49:51 +0200
> > From: Nils Wagner <nils106 at googlemail.com>
> > Subject: Re: [SciPy-Dev] Function to check if sparse matrix is
> >         symmetric
> > To: SciPy Developers List <scipy-dev at scipy.org>
> > Message-ID:
> >         <CAE7vhgPNEkB6fHfg=v7CSUTK9n91Ath9uUSJo20b7=
> > ysMfFmcw at mail.gmail.com>
> > Content-Type: text/plain; charset="utf-8"
> >
> > IMHO "is_Hermitian" is more general.
> >
> > Nils
> >
> >
> > On Fri, Oct 10, 2014 at 12:00 PM, Saullo Castro <saullogiovani at gmail.com
> >
> > wrote:
> >
> > > I developed a function (shown below) to check if a sparse matrix is
> > > symmetric and would like to know if the community is interested to
> > include
> > > in scipy.sparse.
> > >
> > > Regards,
> > > Saullo
> > >
> > > def is_symmetric(m):
> > >     """Check if a sparse matrix is symmetric
> > >
> > >     Parameters
> > >     ----------
> > >     m : array or sparse matrix
> > >         A square matrix.
> > >
> > >     Returns
> > >     -------
> > >     check : bool
> > >         The check result.
> > >
> > >     """
> > >     if m.shape[0] != m.shape[1]:
> > >         raise ValueError('m must be a square matrix')
> > >
> > >     if not isinstance(m, coo_matrix):
> > >         m = coo_matrix(m)
> > >
> > >     r, c, v = m.row, m.col, m.data
> > >     tril_no_diag = r > c
> > >     triu_no_diag = c > r
> > >
> > >     if triu_no_diag.sum() != tril_no_diag.sum():
> > >         return False
> > >
> > >     rl = r[tril_no_diag]
> > >     cl = c[tril_no_diag]
> > >     vl = v[tril_no_diag]
> > >     ru = r[triu_no_diag]
> > >     cu = c[triu_no_diag]
> > >     vu = v[triu_no_diag]
> > >
> > >     sortl = np.lexsort((cl, rl))
> > >     sortu = np.lexsort((ru, cu))
> > >     vl = vl[sortl]
> > >     vu = vu[sortu]
> > >
> > >     check = np.allclose(vl, vu)
> > >
> > >     return check
> > >
> > >
> > > _______________________________________________
> > > SciPy-Dev mailing list
> > > SciPy-Dev at scipy.org
> > > http://mail.scipy.org/mailman/listinfo/scipy-dev
> > >
> > >
> > -------------- next part --------------
> > An HTML attachment was scrubbed...
> > URL:
> >
> http://mail.scipy.org/pipermail/scipy-dev/attachments/20141010/34aab777/attachment-0001.html
> >
> > ------------------------------
> >
> > _______________________________________________
> > SciPy-Dev mailing list
> > SciPy-Dev at scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-dev
> >
> >
> > End of SciPy-Dev Digest, Vol 132, Issue 8
> > *****************************************
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://mail.scipy.org/pipermail/scipy-dev/attachments/20141010/5b6a3a8e/attachment-0001.html
>
> ------------------------------
>
> Message: 2
> Date: Fri, 10 Oct 2014 11:05:00 -0700
> From: Clark Fitzgerald <clarkfitzg at gmail.com>
> Subject: Re: [SciPy-Dev] [pystatsmodels] Re: Bootstrap confidence
>         limits code
> To: pystatsmodels at googlegroups.com
> Cc: SciPy Developers List <scipy-dev at scipy.org>,        Constantine Evans
>         <cevans at evanslabs.org>
> Message-ID:
>         <CAE_rN=3-PsEDVHzpA=
> rKqxX7jHe8f6yhpbwVBuzJOn8jENXJCQ at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Thanks for the link. It looks like you've put a lot of thought into the
> design. I'll give it a try next time I need to use bootstrap.
>
> On Thu, Oct 9, 2014 at 7:41 PM, Kevin Sheppard <kevin.k.sheppard at gmail.com
> >
> wrote:
>
> > I have an almost complete bootstrap here:
> >
> > https://github.com/bashtage/arch/blob/bootstrap/arch/bootstrap/base.py
> >
> > Just needs me to finish BCA, add docs on how to use and merge.
> >
> >
> > On Saturday, October 4, 2014 1:16:39 PM UTC-4, Clark Fitzgerald wrote:
> >>
> >> Discovered this thread after searching for statistical bootstrap in
> >> Python. It would be nice to have the capability to see a general
> bootstrap
> >> either in statsmodels or scipy.stats. Are people interested in this?
> >>
> >> Here's an object oriented approach that I took for it-
> >> https://github.com/clarkfitzg/stat-bootstrap/blob/master/bootstrap.py
> >>
> >> Regards,
> >> Clark Fitzgerald
> >>
> >>
> >> On Wednesday, August 8, 2012 11:57:20 AM UTC-7, jseabold wrote:
> >>>
> >>> On Wed, Aug 8, 2012 at 2:38 PM, Constantine Evans <
> cev... at evanslabs.org>
> >>> wrote:
> >>>
> >>>> Hello everyone,
> >>>>
> >>>>
> >>> Hi,
> >>>
> >>>
> >>>> A few years ago I implemented a scikit for bootstrap confidence limits
> >>>> (https://github.com/cgevans/scikits-bootstrap). I didn?t think much
> >>>> about it after that until recently, when I realized that some people
> >>>> are actually using it, and that there?s apparently been some talk
> >>>> about implementing this functionality in either scipy.stats or
> >>>> statsmodels (I should thank Randal Olson for discussing this and
> >>>> bringing it to my attention).
> >>>>
> >>>> As such I?ve rewritten most of the code, and written up some
> >>>> docstrings. The current code can do confidence intervals with basic
> >>>> percentile interval, bias-corrected accelerated, and approximate
> >>>> bootstrap confidence methods, and can also provide bootstrap and
> >>>> jackknife indexes. Most of it is implemented from the descriptions in
> >>>> Efron and Tibshirani?s Introduction to the Bootstrap, but the ABC code
> >>>> at the moment is a port from the modified-BSD-licensed bootstrap
> >>>> package for R (not the boot package) as I?m not entirely confident in
> >>>> my understanding of the method.
> >>>>
> >>>> And so, I have a few questions for everyone:
> >>>>
> >>>> * Is there any interest in including this sort of code in either
> >>>> scipy.stats or statsmodels? If so, where do people think would be the
> >>>> better place? The code is relatively small; at the moment it is less
> >>>> than 200 lines, with docstrings probably making up 100 of those lines.
> >>>>
> >>>
> >>> I think it would be great to have this in statsmodels. I filed an
> >>> enhancement ticket about it this morning (also brought to my attention
> by
> >>> Randy's blog post).
> >>>
> >>> https://github.com/statsmodels/statsmodels/issues/420
> >>>
> >>>
> >>>> * Also, if so, what would need to be changed, added, and improved
> >>>> beyond what is mentioned in the Contributing to Scipy part of the
> >>>> reference guide? I?m never a fan of my own code, and imagine quite a
> >>>> bit would need to be fixed; I know tests will need to be added too.
> >>>>
> >>>>
> >>> We can discuss further on the statsmodels mailing list (cc'd) unless
> >>> someone feels strongly that this should go into scipy. I'm not sure
> about
> >>> API yet so that it can be general and used across all the models in
> >>> statsmodels. It's one of the reasons I've put off incorporating code
> like
> >>> this for so long.
> >>>
> >>>
> >>>> In addition, I have a few questions about what would be better
> >>>> practice for the API, and I haven?t really found a guide on best
> >>>> practices for Scipy:
> >>>>
> >>>
> >>>> * When I started writing the code, I wrote a single function ci for
> >>>> confidence intervals, with a method argument to choose the method.
> >>>> This is easy for users, especially so that they don?t have to look
> >>>> through documentation to realize that BCA is the most generally useful
> >>>> method (at least from everything I?ve read) and that there really
> >>>> isn?t any reason to use many of the simpler methods. However, ABC
> >>>> takes different paramenters, and needs a statistic function that takes
> >>>> weights, which makes this single-function organization trickier. At
> >>>> the moment, I have a separate function for ABC. Would it be better to
> >>>> split up all the methods to their own functions?
> >>>>
> >>>
> >>> I think this might be preferable.
> >>>
> >>>
> >>>> * ABC requires a statistic function that takes weights. I?ve noticed
> >>>> that things like np.average takes a weights= argument. Would it be
> >>>> better to require input of a stat(data,weights) function, or input of
> >>>> a stat(data,weights=) with weights as a named argument? The latter
> >>>> would be nice in terms of allowing the same function to be used for
> >>>> all methods, but would make it impossible to use a lambda for the
> >>>> function. Is there some other method of doing this entirely?
> >>>> * Are there any missing features that anyone thinks should be added?
> >>>>
> >>>> I apologize if much of this is answered elsewhere, I just haven?t
> >>>> found any of it; I also apologize if this is far too long-winded and
> >>>> confusing!
> >>>>
> >>>> Regards,
> >>>> Constantine Evans
> >>>> _______________________________________________
> >>>> SciPy-Dev mailing list
> >>>> SciP... at scipy.org
> >>>> http://mail.scipy.org/mailman/listinfo/scipy-dev
> >>>>
> >>>
> >>>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://mail.scipy.org/pipermail/scipy-dev/attachments/20141010/41d62c4f/attachment.html
>
> ------------------------------
>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>
>
> End of SciPy-Dev Digest, Vol 132, Issue 9
> *****************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20141010/b80293c1/attachment.html>


More information about the SciPy-Dev mailing list