Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curve analysis with uncertainties package #551

Conversation

nkanazawa1989
Copy link
Collaborator

@nkanazawa1989 nkanazawa1989 commented Dec 2, 2021

Summary

This is followup of #481 . This PR offloads error computation currently manually coded (sometime with error) to uncertainties package.

Details and comments

Usage of FitVal object is removed from experiment library / framework module and replaced with UFloat object. Both FitVal and UFloat are numerical object consisting of the nominal value and standard error. UFloat supports numerical operation with error propagation while keeping parameter correlation, so this will give us more precise estimate of error values that is computed based on fit values.

We need minor code update for many analysis modules, because of property name difference:

FitVal.value -> UFloat.nominal_value
FitVal.stddev -> UFloat.std_dev

However we can remove code that manually computes error propagation (without considering parameter correlation).

Effect of parameter correlation

In current Experiments curve fit code, pcov (covariance matrix) returned from curve_fit is just ignored. So parameter values are (written with ufloat object in uncertainties package, but same implementation with current code)

fit_params = [ufloat(n, e) for n, e in zip(popt, np.sqrt(np.diag(pcov)))]

on the other hand, uncertainties provides a convenient function that generates parameter set with covariance matrix.

fit_params = correlated_values(nom_values=popt, covariance_mat=pcov)

I tested the difference with T1 experiment with not enough scan range (so that error is amplified).

Without correlation
image

With correlation
image

# param_keys
['amp', 'base', 'tau']

# popt
array([9.62219381e-01, 3.90678859e-02, 2.36014395e-05])

# perr := np.sqrt(np.diag(pcov)), i.e. diagonal part. no correlation
array([3.51308248e-02, 3.79792306e-02, 1.80316371e-06])

# pcov
array([[ 1.23417485e-03, -1.31405326e-03,  5.94960145e-08],
       [-1.31405326e-03,  1.44242196e-03, -6.68205862e-08],
       [ 5.94960145e-08, -6.68205862e-08,  3.25139937e-12]])

Note that non-diagonal part (correlation) is not negligible in such configuration, and seems like we have been overestimating the confidence interval.

(note)
This PR doesn't update data representation in ExperimentData. This means once analysis complete the outcome is converted into FitVal anyways and we cannot compute error propagation anymore. Same for data loaded from the database. To support error propagation with loaded entries, we need to replace FitVal with UFloat in db service module. But this change will add more and more files to review, so will be done (if we want) in follow-up PR.

TODO

  • update tutorials

@nkanazawa1989 nkanazawa1989 force-pushed the upgrade/curve_analysis_uncertainties branch from f2c531b to d9364b3 Compare December 2, 2021 18:04
@nkanazawa1989 nkanazawa1989 force-pushed the upgrade/curve_analysis_uncertainties branch from d9364b3 to 2ad7511 Compare December 2, 2021 18:06
@nkanazawa1989 nkanazawa1989 changed the title [WIP] Curve analysis with uncertainties package Curve analysis with uncertainties package Dec 2, 2021
@nkanazawa1989 nkanazawa1989 added this to the Release 0.2 milestone Dec 2, 2021
Copy link
Contributor

@eggerdj eggerdj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Minor suggestions.

qiskit_experiments/curve_analysis/curve_data.py Outdated Show resolved Hide resolved
qiskit_experiments/curve_analysis/curve_fit.py Outdated Show resolved Hide resolved
qiskit_experiments/curve_analysis/fit_function.py Outdated Show resolved Hide resolved
qiskit_experiments/database_service/db_fitval.py Outdated Show resolved Hide resolved
qiskit_experiments/framework/base_analysis.py Outdated Show resolved Hide resolved
releasenotes/notes/upgrade-curve-fit-4dc01b1db55ee398.yaml Outdated Show resolved Hide resolved
@nkanazawa1989
Copy link
Collaborator Author

I also added 3-sigma interval according to https://en.wikipedia.org/wiki/68–95–99.7_rule

In the empirical sciences, the so-called three-sigma rule of thumb expresses a conventional heuristic that nearly all values are taken to lie within three standard deviations of the mean, and thus it is empirically useful to treat 99.7% probability as near certainty

image

CI with parameter correlation nicely explains relation between sampling error and fit error.

Copy link
Contributor

@eggerdj eggerdj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost good. One minor naming concern.

qiskit_experiments/curve_analysis/utils.py Outdated Show resolved Hide resolved
@chriseclectic chriseclectic modified the milestones: Release 0.2, Release 0.3 Dec 8, 2021
@chriseclectic chriseclectic added the on hold On hold until something else is done. label Dec 8, 2021
@nkanazawa1989
Copy link
Collaborator Author

nkanazawa1989 commented Jan 29, 2022

Closed since this is merged into #564 for review with full functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on hold On hold until something else is done.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants