Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Python.h"
- #include <math.h>
- static PyObject *StatsError;
- double intpow( double base, int exponent)
- {
- int i;
- double out = base;
- for( i=1 ; i < exponent ; i++ )
- {
- out *= base;
- }
- return out;
- }
- double*
- avg_func(PyObject *seq, Py_ssize_t count)
- {
- Py_ssize_t a;
- PyObject *tempp = NULL;
- double *returnp = NULL;
- static double temp_avg;
- double sumall = 0.00;
- PyObject **src; // avg,
- src = PySequence_Fast_ITEMS(seq);
- for (a=0; a != count; a++)
- {
- tempp = src[a];
- Py_INCREF(tempp);
- if (PyInt_Check(tempp) || PyLong_Check(tempp) || PyFloat_Check(tempp))
- {
- sumall = sumall + PyFloat_AsDouble(tempp);
- Py_DECREF(tempp);
- }
- else {
- Py_DECREF(tempp);
- return returnp;
- }
- }
- // tempp = PySequence_Fast_GET_ITEM(seq,a);
- // if (PyInt_Check(tempp) || PyLong_Check(tempp) || PyFloat_Check(tempp)){
- // sumall += PyFloat_AsDouble(tempp);
- // }
- // else {
- // return returnp;
- // }
- // }
- temp_avg = sumall / count;
- returnp = &temp_avg;
- return returnp;
- };
- double*
- var(PyObject *seq, Py_ssize_t count, double *avg, int samp, int moment)
- {
- Py_ssize_t a;
- PyObject *tempp = NULL;
- static double temp_r = 0.00;
- double sumall = 0.00;
- double tempxx = 0.00;//tempx,
- double tempx = 0.00;
- //static double test_var;
- // PyObject **src; // avg,
- // src = PySequence_Fast_ITEMS(seq);
- // for (a=0; a != count; a++) {
- // tempp = src[a];
- // Py_INCREF(tempp);
- // tempx = PyFloat_AsDouble(tempp);
- // Py_DECREF(tempp);
- // tempxx = tempx - *avg;
- // sumall += intpow(tempxx,moment);
- // }
- for (a = 0; a != count; a++)
- {
- tempp = PySequence_Fast_GET_ITEM(seq,a);
- if (tempp == NULL)
- {
- PyErr_SetString(StatsError, "Problem");
- return NULL;
- }
- tempx = PyFloat_AsDouble(tempp);
- tempxx = tempx - *avg;
- sumall = sumall + intpow(tempxx,moment);//pow(tempxx,moment);
- //test_var = (double) moment;
- //break;
- }
- temp_r = sumall / (count - samp);
- return &temp_r;
- };
- int
- sample(PyObject *pytrue)
- {
- if (pytrue == Py_True)
- {
- return 1;
- }
- return 0;
- }
- double*
- stat_avgcheck(PyObject *avgp, PyObject *seq, Py_ssize_t count)
- {
- double *returnp = NULL;
- static double temp;
- Py_ssize_t a;
- PyObject *tempp;
- if (avgp == NULL)
- {
- returnp = (double *) avg_func(seq, count);
- if (returnp == NULL)
- {
- PyErr_SetString(StatsError, "Must use real numbers");
- return NULL;
- }
- return returnp;
- }
- else
- {
- if (PyInt_Check(avgp) || PyLong_Check(avgp) || PyFloat_Check(avgp))
- {
- temp = PyFloat_AsDouble(avgp);
- returnp = &temp;
- for (a=0; a != count; a++)
- {
- tempp = PySequence_Fast_GET_ITEM(seq,a);
- if (PyInt_Check(tempp) || PyLong_Check(tempp) || PyFloat_Check(tempp))
- {
- continue;
- }
- else
- {
- PyErr_SetString(StatsError, "Must use real numbers");
- return NULL;
- }
- }
- return returnp;
- }
- else {
- PyErr_SetString(StatsError, "avg must be a real number");
- return NULL;
- }
- }
- };
- static PyObject*
- stat_avg(PyObject *self, PyObject *list)
- {
- PyObject *obj = NULL;
- PyObject *seq = NULL;
- double *returnp = NULL;
- Py_ssize_t count;
- double avg = 0.00;
- if (!PyArg_ParseTuple(list, "O", &obj))
- return NULL;
- seq = PySequence_Fast(obj, "Expected a Sequence");
- if (seq == NULL)
- {
- PyErr_SetString(StatsError, "Problem with the sequence #1");
- return NULL;
- }
- count = PySequence_Fast_GET_SIZE(seq);
- if (count < 0)
- {
- Py_DECREF(seq);
- PyErr_SetString(StatsError, "Problem with returning the count of the sequence");
- return NULL;
- }
- if (count == 0)
- {
- Py_DECREF(seq);
- PyErr_SetString(StatsError, "Can not have an empty sequence");
- return NULL;
- }
- returnp = (double *) avg_func(seq, count);
- if (returnp == NULL)
- {
- Py_DECREF(seq);
- PyErr_SetString(StatsError, "Must use real numbers");
- return NULL;
- }
- avg = (double) *returnp;
- Py_DECREF(seq);
- free(returnp);
- return PyFloat_FromDouble(avg);
- };
- static PyObject*
- stat_variance(PyObject *self, PyObject *list, PyObject *keywds)
- {
- PyObject *obj = NULL;
- PyObject *seq = NULL;
- PyObject *pytrue = Py_True;
- double *return_avgp = NULL;
- double *return_varp = NULL;
- PyObject *avgp = NULL;
- int samp = 0;
- Py_ssize_t count;
- //double variance = 0.00;//avg,
- static char *kwlist[] = {"list", "avg", "sample", NULL};
- if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
- return NULL;
- if (PyBool_Check(pytrue))
- {
- samp = sample(pytrue);
- }
- else{
- PyErr_SetString(StatsError, "sample arg not of the form True / False");
- return NULL;
- }
- seq = PySequence_Fast(obj, "Expected a Sequence");
- if (seq == NULL)
- return NULL;
- count = PySequence_Fast_GET_SIZE(seq);
- if (count < 0)
- return NULL;
- if (count == 0)
- {
- PyErr_SetString(StatsError, "Can not have an empty sequence");
- return NULL;
- }
- return_avgp = (double *) stat_avgcheck(avgp, seq, count);
- if (return_avgp == NULL)
- {
- return NULL;
- }
- return_varp = var(seq, count, return_avgp, samp, 2);
- if (return_varp == NULL)
- {
- return NULL;
- }
- Py_DECREF(seq);
- free(return_avgp);
- return PyFloat_FromDouble(*return_varp);
- };
- static PyObject*
- stat_stdev(PyObject *self, PyObject *list, PyObject *keywds)
- {
- PyObject *obj = NULL;
- PyObject *seq = NULL;
- PyObject *pytrue = Py_True;
- PyObject *avgp = NULL;
- //double *returnp = NULL;
- double *return_avgp = NULL;
- double *return_varp = NULL;
- Py_ssize_t count;
- double stdev = 0.00;//avg,variance,
- int samp = 0;
- static char *kwlist[] = {"list", "avg", "sample", NULL};
- if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
- return NULL;
- if (PyBool_Check(pytrue))
- {
- samp = sample(pytrue);
- }
- else{
- PyErr_SetString(StatsError, "sample arg not of the form True / False");
- return NULL;
- }
- seq = PySequence_Fast(obj, "Expected a Sequence");
- if (seq == NULL)
- return NULL;
- count = PySequence_Fast_GET_SIZE(seq);
- if (count < 0)
- return NULL;
- if (count == 0)
- {
- PyErr_SetString(StatsError, "Can not have an empty sequence");
- return NULL;
- }
- return_avgp = (double *) stat_avgcheck(avgp, seq, count);
- if (return_avgp == NULL)
- {
- return NULL;
- }
- //avg = *return_avgp;
- return_varp = var(seq, count, return_avgp, samp, 2);
- stdev = sqrt(*return_varp);
- free(return_avgp);
- Py_DECREF(seq);
- return PyFloat_FromDouble(stdev);
- };
- static PyObject*
- stat_skew(PyObject *self, PyObject *list, PyObject *keywds)
- {
- PyObject *obj = NULL;
- PyObject *seq = NULL;
- PyObject *pytrue = Py_True;
- PyObject *avgp = NULL;
- double sampx = 0.00;
- //double avg = 0;
- double *return_avgp = NULL;
- double *return_varp = NULL;
- double *third_moment = NULL;
- Py_ssize_t count = 0;
- double second_moment, skew = 0.00;//, avg, third_moment,variance,
- int samp = 0;
- static char *kwlist[] = {"list", "avg", "sample", NULL};
- if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
- return NULL;
- seq = PySequence_Fast(obj, "Expected a Sequence");
- if (seq == NULL)
- return NULL;
- count = PySequence_Fast_GET_SIZE(seq);
- if (count < 0)
- return NULL;
- if (count == 0)
- {
- PyErr_SetString(StatsError, "Can not have an empty sequence");
- return NULL;
- }
- if (PyBool_Check(pytrue))
- {
- samp = sample(pytrue);
- if (samp == 1)
- {
- sampx = sqrt(count*(count-1))/(count-2);
- }
- else {
- sampx = 1;
- }
- }
- else{
- PyErr_SetString(StatsError, "sample arg not of the form True / False");
- return NULL;
- }
- return_avgp = (double *) stat_avgcheck(avgp, seq, count);
- if (return_avgp == NULL)
- {
- return NULL;
- }
- //avg = *return_avgp;
- samp = 0;
- return_varp = var(seq, count, return_avgp, samp, 2);
- double temp_second = *return_varp;
- third_moment = var(seq, count, return_avgp, 0, 3);
- second_moment = pow(temp_second, 1.5);
- skew = (*third_moment / second_moment)*sampx;
- free(return_avgp);
- Py_DECREF(seq);
- return PyFloat_FromDouble(skew);
- };
- static PyObject*
- stat_kurt(PyObject *self, PyObject *list, PyObject *keywds)
- {
- PyObject *obj = NULL;
- PyObject *seq = NULL;
- PyObject *pytrue = Py_True;
- PyObject *avgp = NULL;
- double sampx = 0.00;
- double *return_avgp = NULL;
- double *return_varp = NULL;
- double *fourth_moment = NULL;
- Py_ssize_t count;
- double second_moment, kurt = 0.00;//, avg, fourth_moment,variance,
- int samp = 0;
- static char *kwlist[] = {"list", "avg", "sample", NULL};
- if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
- return NULL;
- seq = PySequence_Fast(obj, "Expected a Sequence");
- if (seq == NULL)
- return NULL;
- count = PySequence_Fast_GET_SIZE(seq);
- if (count < 0)
- return NULL;
- if (count == 0) {
- PyErr_SetString(StatsError, "Can not have an empty sequence");
- return NULL;
- }
- if (PyBool_Check(pytrue))
- {
- samp = sample(pytrue);
- if (samp == 1)
- {
- sampx = (count-1)/((count-2)*(count-3));
- //=(J7-1)/((J7-2)*(J7-3))
- }
- }
- else{
- PyErr_SetString(StatsError, "sample arg not of the form True / False");
- return NULL;
- }
- return_avgp = (double *) stat_avgcheck(avgp, seq, count);
- if (return_avgp == NULL)
- {
- return NULL;
- }
- return_varp = var(seq, count, return_avgp, 0, 2);
- fourth_moment = var(seq, count, return_avgp, 0, 4);
- second_moment = intpow(*return_varp, 2);
- kurt = (*fourth_moment/second_moment) - 3;
- double btwo = 0.00;
- if (samp ==1)
- {
- btwo = ((count+1)*kurt)+6;
- kurt = btwo*sampx;
- }
- free(return_avgp);
- Py_DECREF(seq);
- return PyFloat_FromDouble(kurt);
- };
- static PyMethodDef stat_funcs[] = {
- {"mean", stat_avg, METH_VARARGS},
- {"var", (PyCFunction)stat_variance, METH_VARARGS | METH_KEYWORDS, "finds population and sample variance"},
- {"stdev", (PyCFunction)stat_stdev, METH_VARARGS | METH_KEYWORDS, "finds population and sample standard deviation"},
- {"skew", (PyCFunction)stat_skew, METH_VARARGS | METH_KEYWORDS, "finds population and sample skewness"},
- {"kurt", (PyCFunction)stat_kurt, METH_VARARGS | METH_KEYWORDS, "finds population and sample excess Kurtosis"},
- {NULL, NULL, 0, NULL}
- };
- // PyDoc_STRVAR(module_doc,
- // "This is a module to hold common tools used in descriptive statistics");
- // static struct PyModuleDef stat_funcs_mod = {
- // PyModuleDef_HEAD_INIT,
- // "stats",
- // module_doc,
- // -1,
- // stat_funcs,
- // NULL,
- // NULL,
- // NULL,
- // NULL
- // };
- PyMODINIT_FUNC initstats(void)
- {
- PyObject *m;
- m = Py_InitModule3("stats", stat_funcs,"statistics module");
- if (StatsError == NULL) {
- StatsError = PyErr_NewException("stats.StatsError", NULL, NULL);
- if (StatsError == NULL)
- return;
- }
- Py_INCREF(StatsError);
- PyModule_AddObject(m, "StatsError", StatsError);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement