BBC BASIC for Windows
« How to code polynomial Regression »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:10pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: How to code polynomial Regression  (Read 799 times)
CharlesB
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 46
xx How to code polynomial Regression
« Thread started on: Apr 22nd, 2015, 9:03pm »

I have been on P. Lutus' Polynomial Regression page http://www.arachnoid.com/polysolve/

and it returned this equation.

f(x) = -7.2319868443561427e+003 * x^0
+ 3.9441606604906628e+004 * x^1
+ -7.9951577783959365e+004 * x^2
+ 7.1052066233486243e+004 * x^3
+ -2.2965143356506876e+004 * x^4

My math skills are poor, (I assume that "e" represents Euler's no?) and I went into the help files to see how I would code for this.

I recognize that the above is just standard computer floating-point notation, which means that each line has a number with a base-ten exponent that follows the letter "e".

But when I went to the help files I could not make enough sense of how I am to use the LN function.

I also looked under Mathematical functions, and went to the WIKI site to try to find the answer before I posted.

Dr. Lutus (mentioned above) was kind enough (no sarcasm intended) to return my email to him, but was adamant that I program with any other language than BASIC and thus, that was all of the help that I was going to get. undecided

So, again I ask for a kind soul to give me some help. I have no trouble in programming a linear regression, but this polynomial regression works much better for my purposes. I have done about all I know how in trying to code it correctly.

Thank you again to the community here for all of your help.
Charles
« Last Edit: Apr 22nd, 2015, 9:17pm by CharlesB » User IP Logged

rtr2
Guest
xx Re: How to code polynomial Regression
« Reply #1 on: Apr 22nd, 2015, 9:24pm »

on Apr 22nd, 2015, 9:03pm, CharlesB wrote:
So, again I ask for a kind soul to give me some help. I have no trouble in programming a linear regression, but this polynomial regression works much better for my purposes.

Fitting a polynomial function to a set of data points is one of the example programs (POLYFIT) at the BB4W web site:

http://www.bbcbasic.co.uk/bbcwin/examples/polyfit.html

Regardless of what you may have been told, BBC BASIC is extremely well suited to this functionality because of the built-in matrix arithmetic. It can outperform other 'faster' languages at this particular task.

Richard.
« Last Edit: Apr 22nd, 2015, 9:58pm by rtr2 » User IP Logged

CharlesB
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 46
xx Re: How to code polynomial Regression
« Reply #2 on: Apr 23rd, 2015, 12:27am »

Wow, has this given me something to work with. When I first saw "polyfit.bas" in your example files I had no idea of what this program did.

Most of it is rather (far) over my head, but I will re-enter my data on a csv file.

My biggest problem (above) was simply how to code the formula, i.e. . . . "e+003)* x^0"

I mean, in a very utilitarian way, I still would like to know how I would put this in to proper BASIC code

f(x) = -7.2319868443561427e+003 * x^0
+ 3.9441606604906628e+004 * x^1
+ -7.9951577783959365e+004 * x^2
+ 7.1052066233486243e+004 * x^3
+ -2.2965143356506876e+004 * x^4

I can see how, in POLYFIT.BBC you have something like it . . . but without the perplexing "e"

Code:
    print "y = " fnusing("##.###", vector(0)) fnusing("+##.###", vector(1)) " x" \
      \    fnusing("+##.###", vector(2)) " x^2" fnusing("+##.###", vector(3)) " x^3" \
      \    fnusing("+##.###", vector(4)) " x^4" fnusing("+##.###", vector(5)) " x^5" 




Thank you Richard, for all of your help and for just creating this tool.
User IP Logged

rtr2
Guest
xx Re: How to code polynomial Regression
« Reply #3 on: Apr 23rd, 2015, 08:25am »

on Apr 23rd, 2015, 12:27am, CharlesB wrote:
I still would like to know how I would put this in to proper BASIC code

It is already "proper BASIC code". The only specific thing to watch is that the use of lowercase 'e' means that a *lowercase command is necessary to make it work correctly:

Code:
      *lowercase

      x = 0.1234

      f = -7.2319868443561427e+003 * x^0 \
      \ +  3.9441606604906628e+004 * x^1 \
      \ + -7.9951577783959365e+004 * x^2 \
      \ +  7.1052066233486243e+004 * x^3 \
      \ + -2.2965143356506876e+004 * x^4

      PRINT f 

Richard.
User IP Logged

CharlesB
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 46
xx Re: How to code polynomial Regression
« Reply #4 on: Apr 24th, 2015, 4:13pm »

Thank you Richard.
This worked for me!


Also, I read the section on *LOWERCASE and the Star command summary ... learned a few very helpful things.


Further, I downloaded your polyfit.exe and it was a work of beauty. But when I downloaded polyfit.bas and looked into the code, it seems to me that the program looks for a polyfit.csv file in its own directory. I created such a file of about three hundred lines for x,y values. However, when I "Load & plot file" I see no dots, and when I "Fit polynomial," I get very strange results.

I'm not sure what I did wrong, but in any case, I thank you for your help.
User IP Logged

rtr2
Guest
xx Re: How to code polynomial Regression
« Reply #5 on: Apr 24th, 2015, 5:16pm »

on Apr 24th, 2015, 4:13pm, CharlesB wrote:
However, when I "Load & plot file" I see no dots... I'm not sure what I did wrong.

Did you modify the program to account for the hugely reduced number of data points? If you didn't, your data will be squashed into only about 1.5% of the width of the plot and inevitably the fitted polynomial is likely to 'blow up' outside that range.

The code at the website is just a demonstration, and is not designed to accept an arbitrary data file (e.g. it doesn't automatically scale the plot to the number and range of values in the file); it doesn't even allow you to choose the degree of polynomial!

The hope is that the code is structured in such a clear way that it is easy to modify to your own requirements.

Richard.
User IP Logged

CharlesB
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 46
xx Re: How to code polynomial Regression
« Reply #6 on: Apr 24th, 2015, 9:15pm »

Thank you Richard; I knew that it was a demonstration program and I did notice that it was only designed for a five degree polynomial . . . but I'm such a novice that I did not recognize the need to modify the program for the small data file.

It gives me something to learn. You are absolutely right, my regression line was "blown up" outside the range.


User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls