📄 statc.cpp
字号:
&& (!pylist2 || PyList2flist(pylist2, y))) {
double res1, res2;
res1=chisquare(x, pylist2 ? &y : NULL, res2);
return Py_BuildValue("dd", res1, res2);
}
PyErr_Clear();
vector<PyWrapper> wx, wy;
if ( PyList2wlist(pylist1, wx)
&& (!pylist2 || PyList2wlist(pylist2, wy))) {
PyWrapper res1, res2;
res1=chisquare(wx, pylist2 ? &wy : NULL, res2);
return Py_BuildValue("NN", (PyObject *)res1, (PyObject *)res2);
}
}
PYERROR(PyExc_AttributeError, "chisquare: one or two lists expected", PYNULL);
PyCATCH
}
PyObject *py_chisquare2d(PyObject *, PyObject *args)
{ PyTRY
#define VARS prob, cramerV, contingency_coeff
vector<vector<double> > x;
if (args2flist2d(args, x)) {
double chi2, VARS;
int df;
chi2=chisquare2d(x, df, VARS);
return Py_BuildValue("diddd", chi2, df, VARS);
}
PyErr_Clear();
vector<vector<PyWrapper> > wx;
if (args2wlist2d(args, wx)) {
PyWrapper chi2, VARS;
int df;
chi2=chisquare2d(wx, df, VARS);
return Py_BuildValue("NiNNN", (PyObject *)chi2, df, (PyObject *)prob, (PyObject *)cramerV, (PyObject *)contingency_coeff);
}
#undef VARS
PYERROR(PyExc_AttributeError, "chisquare2d: 2d contingency matrix expected", PYNULL);
PyCATCH
}
PyObject *py_anova_rel(PyObject *, PyObject *args)
{ PyTRY
vector<vector<double> > x;
if (args2flist2d(args, x)) {
double F, prob;
int df_bt, df_err;
F = anova_rel(x, df_bt, df_err, prob);
return Py_BuildValue("diid", F, df_bt, df_err, prob);
}
PYERROR(PyExc_AttributeError, "anova_rel: 2d contingency matrix expected", PYNULL);
PyCATCH
}
PyObject *py_friedmanf(PyObject *, PyObject *args)
{ PyTRY
vector<vector<double> > x;
if (args2flist2d(args, x)) {
double F, prob, chi2;
int dfnum, dfden;
F = friedmanf(x, chi2, dfnum, dfden, prob);
return Py_BuildValue("diidd", F, dfnum, dfden, prob, chi2);
}
PYERROR(PyExc_AttributeError, "friedmanf: 2d contingency matrix expected", PYNULL);
PyCATCH
}
#define WRAPTEST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
double res, prob; \
\
vector<double> x, y; \
if (args22listsne(args, x, y)) { \
res=name(x, y, prob); \
return Py_BuildValue("dd", res, prob); \
} \
PyErr_Clear(); \
\
vector<PyWrapper> wx, wy; \
if (args22wlistsne(args, wx, wy)) { \
res=name(wx, wy, prob); \
return Py_BuildValue("dd", res, prob); \
} \
PyErr_Clear(); \
\
PyObject *pylist, *pygroup=NULL, *pycomp=NULL; \
if ( PyArg_ParseTuple(args, "OOO", &pylist, &pygroup, &pycomp) \
&& PyList2wlist(pylist, wx)) { \
res=name(wx, prob, IsTrueCallback(pygroup), LessThanCallback(pycomp)); \
return Py_BuildValue("dd", res, prob); \
} \
\
PYERROR(PyExc_TypeError, #name": two lists or a list with optional group and compare functions expected.", PYNULL); \
PyCATCH \
}
WRAPTEST(mannwhitneyu)
WRAPTEST(ranksums)
#undef WRAPTEST
DOUBLE_DOUBLE_FROM_LIST_LIST(wilcoxont)
/* *********** PROBABILITY CALCULATIONS ************/
T_FROM_T(gammln)
T_FROM_T_T_T(betai)
T_FROM_T_T_T(betacf)
T_FROM_T(zprob)
T_FROM_T(erf)
T_FROM_T(erfc)
T_FROM_T(erfcc)
T_FROM_T_T(chisqprob)
PyObject *py_fprob(PyObject *, PyObject *args)
{
PyTRY
int dfnum, dfden;
double F;
if (!PyArg_ParseTuple(args, "iid:fprob", &dfnum, &dfden, &F))
return NULL;
return PyFloat_FromDouble(fprob(dfnum, dfden, F));
PyCATCH;
}
/* *********** RANDOM NUMBERS ***************/
T_FROM_T_T(gasdev)
/* *********** SUPPORT FUNCTIONS ************/
T_FROM_LIST_optT(sum)
LIST_FROM_LIST_optT(cumsum)
T_FROM_LIST_optT(ss)
T_FROM_LIST_LIST_optT(summult)
T_FROM_LIST_optT(sumsquared)
T_FROM_LIST_LIST_optT(sumdiffsquared)
PyObject *py_shellsort(PyObject *, PyObject *args)
{ PyTRY
vector<double> flist;
if (args2flist(args, flist)) {
vector<int> indices;
vector<double> items;
if (!shellsort(flist, indices, items))
PYERROR(PyExc_AttributeError, "shellsort failed", NULL);
PyObject *pyind=ilist2PyList(indices), *pyitems=flist2PyList(items);
return Py_BuildValue("NN", pyitems, pyind);
}
PyErr_Clear();
PyObject *pylist, *pycomp=NULL;
if ( !PyArg_ParseTuple(args, "O|O", &pylist, &pycomp)
|| !PyList_Check(pylist))
PYERROR(PyExc_AttributeError, "list and optional compare function expected", false)
vector<PyWrapper> wlist;
if (PyList2wlist(pylist, wlist)) {
vector<int> indices;
vector<PyWrapper> items;
if (pycomp ? !shellsort(wlist, indices, items, LessThanCallback(pycomp))
: !shellsort(wlist, indices, items))
PYERROR(PyExc_AttributeError, "shellsort failed", NULL);
PyObject *pyind=ilist2PyList(indices), *pyitems=wlist2PyList(items);
return Py_BuildValue("NN", pyitems, pyind);
}
return PYNULL;
PyCATCH
}
PyObject *py_rankdata(PyObject *, PyObject *args)
{ PyTRY
vector<double> flist, ranks;
if (args2flist(args, flist)) {
if (!rankdata(flist, ranks))
PYERROR(PyExc_SystemError, "rankdata: failed", NULL);
return flist2PyList(ranks);
}
PyErr_Clear();
vector<PyWrapper> wlist;
PyObject *pylist, *pycomp=NULL;
if ( !PyArg_ParseTuple(args, "O|O", &pylist, &pycomp)
|| !PyList_Check(pylist))
PYERROR(PyExc_AttributeError, "rankdata: list and optional compare function expected", PYNULL)
if (pycomp ? !rankdata(wlist, ranks, LessThanCallback(pycomp))
: !rankdata(wlist, ranks))
PYERROR(PyExc_SystemError, "rankdata: failed", NULL);
return flist2PyList(ranks);
return PYNULL;
PyCATCH \
}
/* *********** LOESS ******************************/
int cc_list(PyObject *pylist, void *l)
{
if (!PyList_Check(pylist))
return 0;
vector<double> *lst = (vector<double> *)l;
int len = PyList_Size(pylist);
*lst = vector<double>();
lst->reserve(len);
for(int i = 0; i<len; i++) {
PyObject *asnum = PyNumber_Float(PyList_GET_ITEM(pylist, i));
if (!asnum)
return 0;
lst->push_back(PyFloat_AsDouble(asnum));
Py_DECREF(asnum);
}
return 1;
}
PyObject *list2python(const vector<double> &lst)
{
PyObject *res = PyList_New(lst.size());
int i = 0;
for(vector<double>::const_iterator li(lst.begin()), le(lst.end()); li!=le; li++, i++)
PyList_SetItem(res, i, PyFloat_FromDouble(*li));
return res;
}
typedef void TSampFunc(const vector<double> &, int, vector<double> &);
TSampFunc *sampFuncs[] = {samplingMinimal, samplingFactor, samplingFixed, samplingUniform};
bool getSmootherPars(PyObject *args, vector<pair<double, double> > &points, vector<double> &sampPoints,
float &smoothPar, const char *method)
{
PyObject *pypoints;
int nPoints;
int distMethod;
char buf[20];
vector<double> xpoints;
points.clear();
sampPoints.clear();
if (PyList_Check(PyTuple_GET_ITEM(args, 1))) {
snprintf(buf, 19, "OO&f:%s", method);
if (!PyArg_ParseTuple(args, buf, &pypoints, cc_list, &sampPoints, &smoothPar))
return false;
distMethod = -1;
}
else {
snprintf(buf, 19, "Oif|i:%s", method);
if (!PyArg_ParseTuple(args, "Oif|i:loess", &pypoints, &nPoints, &smoothPar, &distMethod))
return false;
if ((distMethod < DISTRIBUTE_MINIMAL) || (distMethod > DISTRIBUTE_UNIFORM))
PYERROR(PyExc_TypeError, "invalid point distribution method", false);
}
PyObject *iter = PyObject_GetIter(pypoints);
if (!iter)
PYERROR(PyExc_TypeError, "a list (or a tuple) of points expected", false);
PyObject *item;
for (int i = 0; (item = PyIter_Next(iter)) != NULL; i++) {
PyObject *pyx = NULL, *pyy = NULL;
if ( !PyTuple_Check(item)
|| (PyTuple_Size(item)!=2)
|| ((pyx = PyNumber_Float(PyTuple_GetItem(item, 0))) == NULL)
|| ((pyy = PyNumber_Float(PyTuple_GetItem(item, 1))) == NULL)) {
Py_XDECREF(pyx);
Py_DECREF(item);
Py_DECREF(iter);
PyErr_Format(PyExc_TypeError, "invalid point at index %i", i);
return false;
}
points.push_back(pair<double, double>(PyFloat_AsDouble(pyx), PyFloat_AsDouble(pyy)));
if (distMethod != -1)
xpoints.push_back(PyFloat_AsDouble(pyx));
Py_DECREF(pyy);
Py_DECREF(pyx);
Py_DECREF(item);
}
Py_DECREF(iter);
if (distMethod != -1)
sampFuncs[distMethod](xpoints, nPoints, sampPoints);
return true;
}
PyObject *curve2PyCurve(const vector<double> xs, const vector<pair<double, double> > yvars)
{
PyObject *pypoints = PyList_New(xs.size());
int i = 0;
vector<double>::const_iterator xi(xs.begin());
vector<pair<double, double> >::const_iterator yvi(yvars.begin()), yve(yvars.end());
for (; yvi != yve; yvi++, xi++)
PyList_SetItem(pypoints, i++, Py_BuildValue("fff", *xi, (*yvi).first, (*yvi).second));
return pypoints;
}
PyObject *py_loess(PyObject *, PyObject *args)
{ PyTRY
float windowProp;
vector<pair<double, double> > points;
vector<double> sampPoints;
if (!getSmootherPars(args, points, sampPoints, windowProp, "loess"))
return PYNULL;
vector<pair<double, double> > loess_curve;
loess(sampPoints, points, windowProp, loess_curve);
return curve2PyCurve(sampPoints, loess_curve);
PyCATCH
}
PyObject *py_lwr(PyObject *, PyObject *args)
{ PyTRY
float sigmaPercentile;
vector<pair<double, double> > points;
vector<double> sampPoints;
if (!getSmootherPars(args, points, sampPoints, sigmaPercentile, "lwr"))
return PYNULL;
vector<pair<double, double> > lwr_curve;
lwr(sampPoints, points, sigmaPercentile, lwr_curve);
return curve2PyCurve(sampPoints, lwr_curve);
PyCATCH
}
/* *********** COMBINATORIAL FUNCTIONS ************/
#include "lcomb.hpp"
#define DOUBLE_FROM_INT(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
int i; \
if (!PyArg_ParseTuple(args, "i", &i)) \
PYERROR(PyExc_AttributeError, "integer expected", PYNULL) \
double res=name(i); \
return Py_BuildValue("d", res); \
PyCATCH \
}
#define DOUBLE_FROM_INT_INT(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
int i1, i2; \
if (!PyArg_ParseTuple(args, "ii", &i1, &i2)) \
PYERROR(PyExc_AttributeError, "integer expected", PYNULL) \
double res=name(i1, i2); \
return Py_BuildValue("d", res); \
PyCATCH \
}
DOUBLE_FROM_INT(fact)
DOUBLE_FROM_INT_INT(comb)
DOUBLE_FROM_INT_INT(stirling2)
DOUBLE_FROM_INT(bell)
DOUBLE_FROM_INT(logfact)
DOUBLE_FROM_INT_INT(logcomb)
#define PY_SAMPLING(name) \
PyObject *py_sampling##name(PyObject *, PyObject *args) \
{ \
vector<double> points; \
int nPoints; \
\
if (!PyArg_ParseTuple(args, "O&i:sampling" #name, cc_list, &points, &nPoints)) \
return NULL; \
\
vector<double> lst; \
sampling##name(points, nPoints, lst); \
return list2python(lst); \
}
PY_SAMPLING(Factor)
PY_SAMPLING(Fixed)
PY_SAMPLING(Uniform)
PY_SAMPLING(Minimal)
#undef PY_SAMPLING
/* *********** EXPORT DECLARATIONS ************/
#define DECLARE(name) \
{#name, (binaryfunc)py_##name, METH_VARARGS},
PyMethodDef statc_functions[]={
DECLARE(geometricmean)
DECLARE(harmonicmean)
DECLARE(mean)
DECLARE(median)
{"medianscore", (binaryfunc)py_median, METH_VARARGS},
DECLARE(mode)
DECLARE(moment)
DECLARE(variation)
DECLARE(skewness)
DECLARE(kurtosis)
DECLARE(scoreatpercentile)
DECLARE(percentileofscore)
DECLARE(histogram)
DECLARE(cumfreq)
DECLARE(relfreq)
DECLARE(samplevar)
{"samplestd", (binaryfunc)py_samplestdev, METH_VARARGS},
DECLARE(var)
{"std", (binaryfunc)py_stdev, METH_VARARGS},
DECLARE(z)
DECLARE(zs)
DECLARE(sterr)
DECLARE(trimboth)
DECLARE(trim1)
DECLARE(pearsonr)
DECLARE(spearmanr)
DECLARE(pointbiserialr)
DECLARE(kendalltau)
DECLARE(linregress)
DECLARE(ttest_1samp)
DECLARE(ttest_ind)
DECLARE(ttest_rel)
DECLARE(chisquare)
DECLARE(chisquare2d)
DECLARE(anova_rel)
DECLARE(friedmanf)
DECLARE(mannwhitneyu)
DECLARE(ranksums)
DECLARE(wilcoxont)
DECLARE(chisqprob)
DECLARE(zprob)
DECLARE(fprob)
DECLARE(betacf)
DECLARE(betai)
DECLARE(erf)
DECLARE(erfc)
DECLARE(erfcc)
DECLARE(gammln)
DECLARE(sum)
DECLARE(ss)
DECLARE(sumsquared)
DECLARE(summult)
DECLARE(cumsum)
DECLARE(sumdiffsquared)
DECLARE(shellsort)
DECLARE(rankdata)
DECLARE(spearmanr)
DECLARE(gasdev)
DECLARE(fact)
DECLARE(comb)
DECLARE(stirling2)
DECLARE(bell)
DECLARE(logfact)
DECLARE(logcomb)
{"loess", (binaryfunc)py_loess, METH_VARARGS},
{"lwr", (binaryfunc)py_lwr, METH_VARARGS},
DECLARE(samplingFactor)
DECLARE(samplingFixed)
DECLARE(samplingMinimal)
DECLARE(samplingUniform)
{NULL, NULL}
};
#undef T_FROM_LIST
#undef T_FROM_LIST_optT
#undef T_FROM_LIST_LIST
#undef T_FROM_LIST_LIST_optT
#undef LIST_FROM_LIST
#undef LIST_FROM_LIST_optT
#undef T_FROM_LIST_T
#undef T_FROM_LIST_plus
#undef T_FROM_LIST_INT
#undef T_FROM_LIST_DOUBLE
#undef LIST_FROM_LIST_plus
#undef LIST_FROM_LIST_INT
#undef LIST_FROM_LIST_DOUBLE
#undef DOUBLE_DOUBLE_FROM_LIST_LIST
#undef T_T_FROM_LIST_LIST
#undef T_T_FROM_LIST_plus
#undef T_T_FROM_LIST
#undef T_T_FROM_LIST_INT
#undef T_T_FROM_LIST_DOUBLE
#undef DOUBLE_DOUBLE_FROM_LIST_plus
#undef DOUBLE_DOUBLE_FROM_LIST_INT
#undef DOUBLE_DOUBLE_FROM_LIST_DOUBLE
#undef T_T_FROM_LIST_T
#undef T_FROM_T
#undef T_FROM_T_T_T
#undef CALL_HISTOGRAM
#undef CALL_CUMFREQ
#undef CALL_RELFREQ
#undef HISTOSWITCH
#undef WRAPTEST
#undef DECLARE
#undef PyTRY
#undef PyCATCH
#undef PYNULL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -