(与此问题有点相关Linear fit including all errors with NumPy/SciPy,并从此Linear fitting in python with uncertainty in both x and y coordinates 借用代码)
我使用y=a*x+b
使用scipy.odr(代码如下)使用x,y
中的固定错误拟合线性模型(Parameters (a, b): [ 5.21806759 -4.08019995]
Standard errors: [ 0.83897588 2.33472161]
Squared diagonal covariance: [ 1.06304228 2.9582588 ]
),我得到:
a, b
拟合Squared diagonal covariance
参数的正确标准偏差值是多少?我假设必须从Standard errors
值获取这些值,但这些值与ali_m
如何相关?
添加
正如np.sqrt(np.diag(out.cov_beta * out.res_var))
对How to compute standard error from ODR results?的回答所述,这显然与a bug in scipy.odr有关。如果使用
np.sqrt(np.diag(out.cov_beta))
(即:将协方差乘以残差方差)而不仅仅是
out.sd_beta
结果现在与(a, b)
重合。
所以现在我的问题是:哪个是拟合参数out.sd_beta
的正确标准偏差?是np.sqrt(np.diag(out.cov_beta * out.res_var))
(相当于:np.sqrt(np.diag(out.cov_beta))
)还是import numpy as np
from scipy.odr import Model, RealData, ODR
import random
random.seed(9001)
np.random.seed(117)
def getData(c):
"""Initiate random data."""
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([i**2 + random.random() for i in x])
xerr = c * np.array([random.random() for i in x])
yerr = c * np.array([random.random() for i in x])
return x, y, xerr, yerr
def linear_func(p, x):
"""Linear model."""
a, b = p
return a * x + b
def fitModel(x, y, xerr, yerr):
# Create a model for fitting.
linear_model = Model(linear_func)
# Create a RealData object using our initiated data from above.
data = RealData(x, y, sx=xerr, sy=yerr)
# Set up ODR with the model and data.
odr = ODR(data, linear_model, beta0=[0., 1.])
# Run the regression.
out = odr.run()
# Estimated parameter values
beta = out.beta
print("Parameters (a, b): {}".format(beta))
# Standard errors of the estimated parameters
std = out.sd_beta
print("Standard errors: {}".format(std))
# Covariance matrix of the estimated parameters
cov = out.cov_beta
stddev = np.sqrt(np.diag(cov))
print("Squared diagonal covariance: {}".format(stddev))
# Generate data and fit the model.
x, y, xerr, yerr = getData(1.)
fitModel(x, y, xerr, yerr)
?
#ifndef ADD_PARAMETERS_GROUPBOX
#define ADD_PARAMETERS_GROUPBOX
#include <QGroupBox>
#include <QAbstractTableModel>
namespace Ui {
class AdditionalParameters;
}
class AdditionalParameters : public QGroupBox
{
Q_OBJECT
public:
explicit AdditionalParameters(QWidget *parent = 0);
~AdditionalParameters();
private:
Ui::AdditionalParameters *ui;
signals:
void stateChanged(int state);
private slots:
void PARAMTER_SEL(QModelIndex box);
};
#endif // ADD_PARAMETERS_GROUPBOX
#ifndef ADD_PARAMETERS_SELECTION_TABLE
#define ADD_PARAMETERS_SELECTION_TABLE
#include <QAbstractTableModel>
#include <QString>
class MainWindow;
const int ROWS = 63;
const int COLS = 5;
class ParametersTable : public QAbstractTableModel
{
Q_OBJECT
public:
ParametersTable(QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const ;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
//bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::CheckStateRole);
Qt::ItemFlags flags(const QModelIndex & index) const;
private:
//QString m_gridData[ROWS][COLS]; //holds text entered into QTableView
//bool m_gridData[ROWS][COLS]; //holds state entered into QTableView
signals:
void editCompleted(const QString &);
void stateChanged(int state);
private slots:
//void updateTable();
//void PARAMTER_SEL(QModelIndex box);
};
#include "add_parameters_groupbox.h"
#include "ui_add_parameters_groupbox.h"
#include "add_parameters_selection_table.h"
#include <QTableView>
#include <QAbstractTableModel>
#include <QList>
#include <QTimer>
#include <QDebug>
#include <qcheckbox.h>
QStringList paraHold;
QStringList paraSymbols;
QString Parameters[500][5];
QAbstractTableModel *paraTable;
QItemSelectionModel *selectionModel;
bool m_gridData[ROWS][COLS];
AdditionalParameters::AdditionalParameters(QWidget *parent) :
QGroupBox(parent),
ui(new Ui::AdditionalParameters)
{
ui->setupUi(this);
paraTable = new ParametersTable(this);
ui->AddPara_tableView->setModel(paraTable);
connect(ui->AddPara_tableView,SIGNAL(clicked(QModelIndex)),
this, SLOT(PARAMTER_SEL(QModelIndex)));
paraHold
<< "60 xxxx"
<< "lines"
<< "of strings";
paraSymbols
<< "60+/-"
<< "lines"
<< "of strings";
for(int i = 0; i<(paraHold.size()); i++)
{
Parameters[i][0] = paraHold.at(i);
m_gridData[i][0] = false;
// qDebug() << "m_gridData[i][0]:" << m_gridData[i][0];
if((!Parameters[i][0].contains("T",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("FET",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("NT",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("LT",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("TAT",Qt::CaseInsensitive))){
Parameters[i][1] = "Current/Voltage";
m_gridData[i][1] = true;
// qDebug() << "m_gridData[i][1]:" << m_gridData[i][1];
}
if(!((Parameters[i][0].contains("V",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("I_",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("cell",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("FET",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("NT",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("LT",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("_SCD",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("_CFA",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("_OWD",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("_CHG",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("_DSG",Qt::CaseInsensitive))
|| (Parameters[i][0].contains("OC",Qt::CaseInsensitive)))){
Parameters[i][2] = "Tempurature";
m_gridData[i][2] = true;
// qDebug() << "m_gridData[i][2]:" << m_gridData[i][2];
}
Parameters[i][3] = paraSymbols.at(i);
}
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), ui->AddPara_tableView, SLOT(doItemsLayout()));
// connect(timer, SIGNAL(timeout()), ui->AddPara_tableView, SLOT(resizeColumnsToContents()));
// QTimer::singleShot(20000, ui->AddPara_tableView, SLOT(doItemsLayout()));
QTimer::singleShot(1000, ui->AddPara_tableView, SLOT(resizeColumnsToContents()));
timer->start(1000);
}
AdditionalParameters::~AdditionalParameters()
{
delete ui;
}
ParametersTable::ParametersTable(QObject *parent)
:QAbstractTableModel(parent)
{
}
int ParametersTable::rowCount(const QModelIndex & /*parent*/) const
{
//int pararow = Parameters.size();
//return pararow;
return 62;
}
int ParametersTable::columnCount(const QModelIndex & /*parent*/) const
{
//int paracol;
return 5;
}
QVariant ParametersTable::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return QString("Parameter");
case 1:
return QString("Current/Voltage Chart");
case 2:
return QString("Tempurature Chart");
case 3:
return QString("Symbol");
case 4:
return QString("Color");
}
}
}
return QVariant();
}
QVariant ParametersTable::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
switch(role){
case Qt::DisplayRole:
return QString("%1")
.arg(Parameters[row][col]);
break;
case Qt::FontRole:
if (Parameters[row][col] != "_")
{
QFont boldFont;
boldFont.setBold(true);
return boldFont;
}
break;
case Qt::BackgroundRole:
if(col == 4)
{
if (Parameters[row][0].contains("cell1",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::blue);
return redBackground;
}
if (Parameters[row][0].contains("cell2",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::cyan);
return redBackground;
}
if (Parameters[row][0].contains("cell3",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::magenta);
return redBackground;
}
if (Parameters[row][0].contains("cell4",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::yellow);
return redBackground;
}
if (Parameters[row][0].contains("cell5",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkGray);
return redBackground;
}
if (Parameters[row][0].contains("cell6",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::red);
return redBackground;
}
if (Parameters[row][0].contains("cell7",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkGreen);
return redBackground;
}
if (Parameters[row][0].contains("I_",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::green);
return redBackground;
}
if ((Parameters[row][0].contains("CH",Qt::CaseInsensitive))
|| (Parameters[row][3].contains("+",Qt::CaseInsensitive)))
{
QBrush redBackground(Qt::green);
return redBackground;
}
if ((Parameters[row][0].contains("DS",Qt::CaseInsensitive))
|| (Parameters[row][3].contains("-",Qt::CaseInsensitive)))
{
QBrush redBackground(Qt::red);
return redBackground;
}
if (Parameters[row][0].contains("V_Pack",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkRed);
return redBackground;
}
if (Parameters[row][0].contains("T_ISL",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkMagenta);
return redBackground;
}
if (Parameters[row][0].contains("T_HS",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkYellow);
return redBackground;
}
if (Parameters[row][0].contains("T_LS",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkCyan);
return redBackground;
}
if (Parameters[row][0].contains("T_B",Qt::CaseInsensitive))
{
QBrush redBackground(Qt::darkBlue);
return redBackground;
}
}
break;
case Qt::TextAlignmentRole:
if (col == 3) //change text alignment only for colum 3
{
return Qt::AlignCenter;//+ Qt::AlignVCenter;
}
break;
case Qt::CheckStateRole:
if((col == 0) && (m_gridData[row][0] == false)){ //add a checkbox to all rows in colum 0
// qDebug() << "m_gridData[" << row << "][0]:" << m_gridData[row][0];
return Qt::Unchecked;
}
else if((col == 0) && (m_gridData[row][0] == true)){
// qDebug() << "m_gridData[" << row << "][0]:" << m_gridData[row][0];
return Qt::Checked;
}
if((Parameters[row][1].contains("Current/Voltage",Qt::CaseSensitive)
&& (col == 1)) && (m_gridData[row][1] == true)){
// qDebug() << "m_gridData[" << row << "][1]:" << m_gridData[row][1];
return Qt::Checked;
}
else if((Parameters[row][1].contains("Current/Voltage",Qt::CaseSensitive) && (col == 1))
&& (m_gridData[row][1] == false)){
// qDebug() << "m_gridData[" << row << "][1]:" << m_gridData[row][1];
return Qt::Unchecked;
}
if(Parameters[row][2].contains("Tempurature",Qt::CaseSensitive)
&& (col == 2) && (m_gridData[row][2] == true)){
// qDebug() << "m_gridData[" << row << "][2]:" << m_gridData[row][2];
return Qt::Checked;
}
else if((Parameters[row][2].contains("Tempurature",Qt::CaseSensitive)
&& (col == 2)) && (m_gridData[row][2] == false)){
// qDebug() << "m_gridData[" << row << "][2]:" << m_gridData[row][2];
return Qt::Unchecked;
}
}
return QVariant();
}
Qt::ItemFlags ParametersTable::flags(const QModelIndex & /*index*/) const
{
return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
}
void AdditionalParameters::PARAMTER_SEL(QModelIndex Box)
{
int row = Box.row();
int col = Box.column();
if(!Parameters[row][col].isEmpty()){
m_gridData[row][col] = (!(m_gridData[row][col]));
qDebug() << "PARA_SEL @ m_gridData[row][col]:(" << row << "," << col << "):" << m_gridData[row][col];
}
emit stateChanged(m_gridData[row][col]);
}
答案 0 :(得分:1)
是的,out.sd_beta
包含估计参数的标准偏差,它们等于参数协方差矩阵中对角线项的平方根。
如上所述,scipy.odr
中存在一个错误,即您必须将out.cov_beta
乘以剩余方差out.res_var
才能得出实际的协方差矩阵。参数。