Comments (13)
@lucascolley I will do it later in my spare time.
from scipy.
@Linfye thank you for your response. Unfortunately, this code still leads to an AssertionError, even using a fresh setup.
I think the frozen property of scipy distributions leads to this behavior. The scipy.stats framework and the current implementation of using differently parametrized stable distributions are incompatible. As far as I found out, this affects any computation of levy_stable, not just the sampling. So everything is computed in "S1" and trying to set the distribution instance or class to "S0" is ignored all along.
I'm going to restate the issue and try to fix it in the future, but right now, I have no resources to do so.
from scipy.
@Linfye I'm sorry, I just didn't see your PR earlier. That should definitely do the trick for now. Thank you!
from scipy.
From #21332 (comment), it seems the documented way to change the parameterization (by setting levy_stable.parameterization = …
) only works if you also call the methods via the levy_stable
object.
If you call the methods from a “frozen” distribution object, i.e. one fixed with shape, location, and scale parameters, then you’ll have to modify the dist
attribute of the frozen object instead. For example, the following code works:
import numpy as np
from scipy.stats import levy_stable
stable_distribution = levy_stable(alpha=1.9, beta=0.1, loc=0.0, scale=1.0)
stable_distribution.dist.parameterization = "S0"
samples_s0 = stable_distribution.rvs(size=(1000,1), random_state=49)
stable_distribution.dist.parameterization = "S1"
samples_s1 = stable_distribution.rvs(size=(1000,1), random_state=49)
assert not np.all(np.isclose(samples_s0, samples_s1)) # passes without error
from scipy.
🤷
Could modify __setattr__
(tricky to get right; adds overhead). Could add a new setter set(attribute_name, value)
method that the user must use if they want the attributes to work on frozen distributions (requires users to change behavior). Could document the implementation details (not great practice). Could close as wontfix (doesn't fix the problem). There's a downside to everything. Tough to decide what is "best". But since we're working with existing code that is very complicated, rigid, and fragile, the repetitive solution here might be the least worst. I won't stop it if others decide to go a different direction, though. Just trying to close the issue.
from scipy.
I think I solve the pronlem. In the Scipy doc, it suggests to use levy_stable.parameterization
to set the parameterization because it is class attribute instead of instance attribute. So the code should be like this:
import numpy as np
from scipy.stats import levy_stable
stable_distribution = levy_stable(alpha=1.9, beta=0.1, loc=0.0, scale=1.0)
levy_stable.parameterization = "S0"
samples_s0 = stable_distribution.rvs(size=(1000,1), random_state=49)
levy_stable.parameterization = "S1"
samples_s1 = stable_distribution.rvs(size=(1000,1), random_state=49)
assert not np.all(np.isclose(samples_s0, samples_s1))
In the Scipy source code, it uses wrong self.parameterization
in the function so I correct it. And now it works fine.
from scipy.
@bewit In my output, there's no AssertionError:
And the data in samples_s0 and samples_s1 are indeed different.
Could you please tell me more about your steps of running the modified code?
from scipy.
@Linfye Now I'm really puzzled 😄 Your screenshot looks just like I what I would expect the samples to look like, slightly shifted.
I opened up a fresh Google-Colab Notebook and copy-pasted your code. But here it still throws an AssertionError and the samples are identical. But I can try it again on a local machine (maybe using different versions).
Wondering if this might be due to setup differences? I'm not that knowledgeable about the scipy code base, just working a lot with the alpha-stable distribution lately. If anybody with more experience might have a look into this or could give a hint, I'm happy to do what I can.
from scipy.
One way to check is to add the check as a regression test in your PR @Linfye , and see what CI thinks. Mind doing that?
from scipy.
@bewit You should modify the Scipy source code first like this:
And then paste my code:
import numpy as np
from scipy.stats import levy_stable
stable_distribution = levy_stable(alpha=1.9, beta=0.1, loc=0.0, scale=1.0)
levy_stable.parameterization = "S0"
samples_s0 = stable_distribution.rvs(size=(1000,1), random_state=49)
levy_stable.parameterization = "S1"
samples_s1 = stable_distribution.rvs(size=(1000,1), random_state=49)
assert not np.all(np.isclose(samples_s0, samples_s1))
In my code, I change your code stable_distribution.parameterization
to levy_stable.parameterization
.
Hope this would help.
from scipy.
I could make some additions to the doc to clarify the usage of parameterization (and other attributes that modify the numerical aspects). There are three ways to set the attributes:
- By modifying the class attribute
levy_stable_gen.attr
. This affects all objects except frozen objects created before that havedist.attr
already set. - By modifying the instance attribute
levy_stable.attr
. This only affects methods called via this instance; in particular, it has no effect on frozen objects. - By modifying
dist.attr
of a frozen object. This affects only that frozen object.
On the other hand, I’m not super keen to advertise these “tricks”, in light of the upcoming reworked infrastructure which has native support for different parameterizations and algorithm parameters.
@mdhaber Do you think it’s worthwhile to modify the current doc as above? Or do you prefer to supersede it with the new infrastructure instead?
from scipy.
I don't think we need to modify the docs. I think we can just subclass rv_continuous_frozen
for levy_stable
and give it an @property
named parameterization
with a setter that modifies self.dist.parameterization
. I'll open a PR.
from scipy.
I don't think we need to modify the docs. I think we can just subclass
rv_continuous_frozen
forlevy_stable
and give it an@property
namedparameterization
with a setter that modifiesself.dist.parameterization
. I'll open a PR.
That would work too. But levy_stable
has a bunch of other parameters that work in the same fashion as parameterization
, so it looks a bit tedious to also support them via rv_frozen
…
from scipy.
Related Issues (20)
- ENH: optimize.bisect: improve worst case performance
- BUG: error in `optimize.minimize(..., method='L-BFGS-B', ...)` with fixed `bounds` and `callback`
- BUG: `scipy.test()` fails with "Could not invoke sanity test executable" HOT 2
- ENH: spatial: `Rotation.__mul__`: handle non-`Rotation` typed `other` HOT 7
- CI: free-threaded job failing HOT 3
- BUG: interpolate: TypeError for `import BSpline` HOT 2
- STY/DEV: fix and enable lint rule UP038 HOT 4
- QUERY: interpolate.interp1d: `ValueError: Expect x to not have duplicates` HOT 4
- ENH: signal.correlate: specify specific lags or range of lags
- ENH: optimize.minimize: Expose LINCOA, BOBYQA, NEWUOA, and UOBYQA HOT 52
- QUERY: linalg.ldl: clarify docs? HOT 16
- DOC: Lack of table of contents in cluster HOT 5
- BUG: building current SciPy main with MSVC leads to broken install HOT 3
- BUG: Windows Arm build fails HOT 4
- ENH: stats.percentileofscore: 2D array input should be fixed or forbidden HOT 5
- DOC: stats.pearsonr: error in notes of API reference HOT 1
- RFC: spatial.distance: a roadmap for Boolean distance metrics HOT 1
- DOC: interpolate.interp1d: clarify status and alternatives HOT 2
- ENH: signal.ellip: return poles in a nice order HOT 5
- DOC: building: specify `.ps1` for windows example HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scipy.