Skip to content

refactor: cache fit_all results on instance and remove fit_results parameter from best_fit #153

Description

@MAfarrag

Problem

best_fit has a fit_results parameter so users can pass pre-computed fit_all results to avoid re-fitting. This is an internal optimization concern leaking into the public API. It forces the user to manually wire data between the two methods:

results = dist.fit_all()
best_name, best_info = dist.best_fit(fit_results=results)  # manual plumbing

The parameter also creates two calling modes (with/without fit_results), making the method signature harder to understand: method and distributions are silently ignored when fit_results is provided, which is not obvious to the caller.

Proposed Solution

  1. fit_all stores its results on self._fit_results after computation.
  2. best_fit reads from self._fit_results automatically. If no cached results exist, it calls fit_all internally.
  3. Remove the fit_results parameter from best_fit entirely.

Before

dist = Distributions(data=data)
results = dist.fit_all()
best_name, best_info = dist.best_fit(fit_results=results)

After

dist = Distributions(data=data)
dist.fit_all()                              # cached on self
best_name, best_info = dist.best_fit()      # uses cache automatically

# or skip fit_all entirely:
best_name, best_info = dist.best_fit()      # calls fit_all internally, caches

Considerations

  • Calling fit_all again with different parameters (e.g., different method or distributions) should overwrite the cache. This is the expected behavior.
  • best_fit should still accept method, distributions, and criterion parameters. If best_fit is called and no cache exists, it calls fit_all(method=method, distributions=distributions) to populate the cache.
  • Initialize self._fit_results = None in __init__.

Scope

  • Add self._fit_results = None to __init__
  • fit_all: store results in self._fit_results before returning
  • best_fit: remove fit_results parameter, read from self._fit_results
  • Update tests (remove all fit_results= usage, test caching behavior)
  • Update docstring examples

Depends On

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpythonPull requests that update python code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions