Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Regression Date: Mon, 27 Oct 2014 11:17:01 -0400 Organization: A noiseless patient Spider Lines: 155 Message-ID: References: <2a713db5-9dbe-4325-9dc5-c781649861ce@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 27 Oct 2014 15:16:56 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="27228"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/X/EZ50/DB9zm5toHENpW9" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <2a713db5-9dbe-4325-9dc5-c781649861ce@googlegroups.com> Cancel-Lock: sha1:4ShJTQkscvP7usVxAfV24iU5EiE= Xref: csiph.com comp.lang.java.programmer:30210 On 10/27/2014 10:57 AM, gottshalk.j@gmail.com wrote: > Cant seem to figure out the correlation portion and the standard deviation portion either. Could use some help. Thank you all very much. > > > import java.io.File; > import java.io.FileNotFoundException; > import java.util.Arrays; > import java.util.Scanner; > > > public class Regression { > > public static double findingMin (double [] a ) > { > double min = a[0]; > for (int i = 1; i < a.length; i++) > { > if (min < a[i]) > min = a[i]; > } > return min; > } > > public static double findingMax ( double [] b ) > { > double max = b[0]; > for (int i = 1; i < b.length; i++ ) > { > if (max > b[i]) > max = b[i]; > } > return max; > } In light of what they actually do, you've chosen some very peculiar names for these methods ... > public static void main(String[] args) throws FileNotFoundException { > > Scanner input = new Scanner(new File("/home/fjgottshalk/Downloads/cricket.txt")); > > int numb = 15; > double [] chirp = new double[numb]; > double [] temp = new double[numb]; > > for(int i = 0; i < numb; i++) > { > > chirp[i] = input.nextDouble(); > temp[i] = input.nextDouble(); > > } > double xMin = findingMin(chirp); > double xMax = findingMax(chirp); > double yMin = findingMin(temp); > double yMax = findingMax(temp); > > StdDraw.setPenRadius(0.01); > StdDraw.setXscale(xMin, xMax); > StdDraw.setYscale(yMin, yMax); > > for (int i = 0; i < numb; i++) > { > StdDraw.point(chirp[i], temp[i]); > } > > } > > public static double mean(double[] chirp) > { > double sum = 0; > for (int i = 0; i < chirp.length; i++) > { > sum += chirp[i]; > > } > > return sum / chirp.length; > } > > public static double meanagain(double[] temp) > { > double sum = 0; > for (int i = 0; i < temp.length; i++) > { > sum += temp[i]; > > } > > return sum / temp.length; > } Why write the same method twice? If there's any difference between mean() and meanagain(), other than their names, I'm not seeing it. > public static void stdDeviation (double [] chirp) > { > double sum = 0; > > for (int i = 0; i < chirp.length; i++) { > > double currentElement = chirp[i]; > sum = sum + (currentElement - mean(chirp) )*Math.exp(2); > } > System.out.println(sum/chirp.length); > } There are several things wrong here. First, tacking `*Math.exp(2)' on the end of an expression does not mean "square this thing," but "multiply this thing by the exponential of two," that is, "multiply this thing by 7.389 or so." If you want to square something, multiply it by itself. Second, calculating and re-calculating and re-re-calculating the mean() over and over and over again is just a waste of effort. You'll get the same result every time, so calculate it once and remember it. Third, the method name is (once again) strange, because if you fix the squaring problem the quantity you'll be calculating is not the standard deviation, but the sample variance. > public static void Deviation (double [] temp) > { > double sum = 0; > > for (int i = 0; i < temp.length; i++) { > > double currentElement = temp[i]; > sum = sum + (currentElement - meanagain(temp))*Math.exp(2); > } > System.out.println(sum/temp.length); > } Again, the same method written twice for no apparent reason (and with all the misteaks of the first one, too). You seem shaky in several areas (no shame there; we were all novices once), but one in particular that I think you should review is the notion of a "method" (or "subroutine" or "function," in other programming languages) and the relationship between an "argument" and a "parameter." If your program has an array named chirp[] and another named temp[] and you want to make the same calculation on both, you do not need to write the calculation twice in two methods: You can write it once in one method and call that one method twice, passing a different array as the argument each time. (Inside the method, you can call the array zaphod[] if you like; the name of the parameter has nothing at all to do with somebody else's name for the array -- indeed, it's possible for *one* array to have *twenty* names.) Keep at it. Confusion reigns at first, but clarity will come. -- esosman@comcast-dot-net.invalid