Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Possible error in Special.BSpline function #129

Closed
remyzerems opened this issue Aug 25, 2015 · 3 comments
Closed

Possible error in Special.BSpline function #129

remyzerems opened this issue Aug 25, 2015 · 3 comments

Comments

@remyzerems
Copy link
Contributor

It seems that there is an error in the BSpline function in the Special.cs file.
There may be a mismatch between the definition stated here : http://crsouza.com/2010/03/kernel-functions-for-machine-learning-applications/#bspline and the implementation on the Special class.

Original code :

        /// <summary>
        ///   Computes the Basic Spline of order <c>n</c>
        /// </summary>
        public static double BSpline(int n, double x)
        {
            // ftp://ftp.esat.kuleuven.ac.be/pub/SISTA/hamers/PhD_bhamers.pdf
            // http://sepwww.stanford.edu/public/docs/sep105/sergey2/paper_html/node5.html

            if (n == Int32.MaxValue)
                throw new ArgumentOutOfRangeException("n");

            double a = 1.0 / Special.Factorial(n);    // <= preloading the 1/n!... ok
            double c;

            bool positive = true;
            for (int k = 0; k <= n + 1; k++)
            {
                c = Binomial(n + 1, k) * Tools.TruncatedPower(x + (n + 1.0) / 2.0 - k, n);
                a += positive ? c : -c;               // <= Then here you sum the n+1 parts of the formula which shouldn't be added to the 1/n! preload
                positive = !positive;
            }

            return a;           // <= returned "a" which does not correspond to the stated formula
        }

Code should look like this :

        /// <summary>
        ///   Computes the Basic Spline of order <c>n</c>
        /// </summary>
        public static double BSpline(int n, double x)
        {
            // ftp://ftp.esat.kuleuven.ac.be/pub/SISTA/hamers/PhD_bhamers.pdf
            // http://sepwww.stanford.edu/public/docs/sep105/sergey2/paper_html/node5.html

            if (n == Int32.MaxValue)
                throw new ArgumentOutOfRangeException("n");

            double a = 0.0;  // Preload a with 0
            double c;

            bool positive = true;
            for (int k = 0; k <= n + 1; k++)
            {
                c = Binomial(n + 1, k) * Tools.TruncatedPower(x + (n + 1.0) / 2.0 - k, n);
                a += positive ? c : -c;  // Sum terms over k
                positive = !positive;
            }

            return 1.0 / Special.Factorial(n) * a;  // Finally apply the 1/n! factor
        }

What do you think ? I hope I'm not misunderstanding anything...

@cesarsouza
Copy link
Member

Thank you - your observation and your solution were perfectly correct. Apologies for taking this long to be able to integrate it in the framework!

@remyzerems
Copy link
Contributor Author

No worries ! The most important is that it has been fixed ! ;-)

@cesarsouza
Copy link
Member

Integrated in release 3.4.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants