I'm experimenting with OLSMultipleLinearRegression and I wonder which of my parameters has most impact on the prediction. I do not understand which method in OLSMultipleLinearRegression that will return that information.
static void test() {
double y[] = {110, 120, 135, 140};
double data[][] = {
{9, 100},
{21, 260},
{29, 490},
{41, 650}
};
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.newSampleData(y, data);
double[] beta = regression.estimateRegressionParameters();
for(int i = 0;i<beta.length;i++) {
System.out.printf("b%d = %.3f\n",i, beta[i]);
}
int ROW = 3;
double value = y[ROW];
double predict = beta[0] + beta[1] * data[ROW][0] + beta[2] * data[ROW][1];
System.out.printf("y=%.3f + %.3f * %.3f + %.3f * %.3f\n", beta[0],beta[1],data[ROW][0],beta[2],data[ROW][1]);
System.out.printf("predict=%.3f value=%.3f\n", predict, value );
}
Related
My logic seems to work for every one of the examples, but when I try to submit it, it comes up as wrong because there is one test input (which is not revealed) that somehow results in my code spitting out "24hours and 10min" which is wrong and that the answer should be "0hours and 10min".
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int xminutes = sc.nextInt();
int y = sc.nextInt();
int yminutes = sc.nextInt();
int xm = x\*60 + xminutes;
if (y\<=x)y+=24;
int ym = y\*60 + yminutes;
System.out.println("O JOGO DUROU "+((ym-xm)/60)+" HORA(S) E "+ ((ym-xm)%60) +" MINUTO(S)");
}
}
Let's assume the times are begin=9:10 end=9:20. The correct answer is 10 minutes, obviously.
Then x = y = 9 and you execute if (y<=x)y+=24, which is where the extra 24 comes from.
You need to consider the whole times (xm and ym) in order to decide whether the 'end' is the next day after 'begin', and you must add 24 hours (or 24 * 60 minutes).
Your code will fail for input "7 30 7 40", because you only compare the hours when checking the day-rollover. I suggest to first convert the input into "minute of the day", then calculate the difference. If negative, add 24 hours.
Such exercices are best solved with (JUnit) unit tests and pure functions which allow you to test automatically without having to run the program manually and provide input.
package example;
import org.junit.jupiter.api.Test;
import java.util.Scanner;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int startHour = sc.nextInt();
final int startMinutes = sc.nextInt();
final int endHour = sc.nextInt();
final int endMinutes = sc.nextInt();
originalCode(startHour, startMinutes, endHour, endMinutes);
newCode(startHour, startMinutes, endHour, endMinutes);
}
private static void originalCode(final int startHour, final int startMinutes, int endHour, final int endMinutes) {
int xm = startHour * 60 + startMinutes;
if (endHour <= startHour) {
endHour += 24;
}
int ym = endHour * 60 + endMinutes;
System.out.println("O JOGO DUROU " + (ym - xm) / 60 + " HORA(S) E " + (ym - xm) % 60 + " MINUTO(S)");
}
private static void newCode(final int startHour, final int startMinutes, int endHour, final int endMinutes) {
final int diff = getDiffInMinutes(startHour, startMinutes, endHour, endMinutes);
System.out.println("Duration is " + hoursOfMinutes(diff) + ":" + minutesOfMinutes(diff));
}
private static int getDiffInMinutes(
final int startHour,
final int startMinutes,
final int endHour,
final int endMinutes) {
final int diff = toMinutes(endHour, endMinutes) - toMinutes(startHour, startMinutes);
return diff <= 0
? diff + 24 * 60
: diff;
// or (difficult to understand, not recommended):
// return (diff + 24 * 60 - 1) % (24 * 60) + 1;
}
private static int toMinutes(final int startHour, final int startMinutes) {
return startHour * 60 + startMinutes;
}
private static int minutesOfMinutes(final int minutes) { // that's a weird name ...
return minutes % 60;
}
private static int hoursOfMinutes(final int minutes) {
return minutes / 60; // integer division
}
#Test
void testHoursOfMinutes() {
assertEquals(0, hoursOfMinutes(0));
assertEquals(0, hoursOfMinutes(59));
assertEquals(1, hoursOfMinutes(60));
assertEquals(1, hoursOfMinutes(61));
assertEquals(1, hoursOfMinutes(90));
assertEquals(23, hoursOfMinutes(1400));
assertEquals(24, hoursOfMinutes(1440));
}
#Test
void testMinutesOfMinutes() {
assertEquals(0, minutesOfMinutes(0));
assertEquals(59, minutesOfMinutes(59));
assertEquals(0, minutesOfMinutes(60));
assertEquals(1, minutesOfMinutes(61));
assertEquals(30, minutesOfMinutes(90));
assertEquals(20, minutesOfMinutes(1400));
assertEquals(0, minutesOfMinutes(1440));
}
#Test
void testToMinutes() {
assertEquals(0, toMinutes(0, 0));
assertEquals(30, toMinutes(0, 30));
assertEquals(60, toMinutes(1, 0));
assertEquals(90, toMinutes(1, 30));
assertEquals(1440, toMinutes(24, 0));
}
#Test
void testDiffInMinutes() {
assertEquals(40, getDiffInMinutes(7, 20, 8, 0));
assertEquals(10, getDiffInMinutes(8, 20, 8, 30));
assertEquals(23 * 60 + 20, getDiffInMinutes(9, 0, 8, 20));
assertEquals(23 * 60 + 50, getDiffInMinutes(10, 20, 10, 10));
// minimum 1 minute, maximum 24 hours
assertEquals(24 * 60, getDiffInMinutes(11, 0, 11, 0));
}
#Test
void runOriginal() {
originalCode(7, 20, 8, 0); // 00:40
originalCode(8, 20, 8, 30); // 00:10
originalCode(9, 0, 8, 20); // 23:20
originalCode(10, 20, 10, 10); // 23:50
}
#Test
void runNew() {
newCode(7, 20, 8, 0); // 00:40
newCode(8, 20, 8, 30); // 00:10
newCode(9, 0, 8, 20); // 23:20
newCode(10, 20, 10, 10); // 23:50
}
}
Of course, nothing prevents you from calculating the difference for hours and minutes separately, but this forces you to keep track of the carry.
As you can see from above, each simple functionality is covered by a unit test. You are safe to refactor and not break anything. Smaller things are then combined into more complicated things. Unit tests let you sleep well and allow you to build the solution step by step.
I have 2 vectors with 8 float variables.
Each vector has different values.
What is the efficient way to compare those 2 vectors and return an overall percentage of similarity? (for example ~80%)
float[] emotion1 = {0.050, 0.200, 0.025, 0.225, 0.075, 0.175, 0.0125, 0.2375}; //sum = 1.00
float[] emotion2 = {0.10, 0.150, 0.175, 0.075, 0.225, 0.025, 0.0125, 0.2375}; //sum = 1.00
double[] emotion1 = {0.05, 0.2, 0.025, 0.225, 0.075, 0.175, 0.0125, 0.2375}; // sum = 1,0
double[] emotion2 = {0.1, 0.15, 0.125, 0.125, 0.175, 0.075, 0.1125, 0.1375};
Set<Double> floatSet = new HashSet<>();
for (double value : emotion1) {
floatSet.add(value);
}
for (double value : emotion2) {
floatSet.add(value);
}
int arraysLength = emotion1.length + emotion2.length;
int totalElementsNumber = floatSet.size();
double percentage = 100 - ((totalElementsNumber * 100) / arraysLength);
System.out.println("similarity: " + percentage + " %");
I'm trying to create a method that searches the volume array for the largest value and returns the value in the corresponding position of the note array.
Also, separately, how could I modify it so that it only plays the loudest note
import arb.soundcipher.*;
SoundCipher midi;
String[] note = {"C", "C#", "D", "D#", "E", "F", "F#",
"G", "G#", "A", "A#", "B"};
float[] volume = {80, 100, 75, 43, 40, 81, 100,
60, 90, 30, 75, 52};
float[] duration = {1.3, 2, 0.5, 3, 0.9, 1, 0.25,
0.6, 1.5, 3, 1.25, 2};
void setup() {
size(200,200);
midi = new SoundCipher(this);
int i = (int) random(note.length);
midi.playNote(MIDIValue(note[i]),volume[i],duration[i]);
}
float MIDIValue(String aNote) {
float noteMIDIValue = 0.0;
int i;
for (i =0; i < note.length && !note[i].equals(aNote) ; i++);
if(i < note.length) {
noteMIDIValue = i+60;
}
return noteMIDIValue;
}
traverse the volume array and keep track of maximum volume and id(index). And finally, return the note corresponding to the maximum element id (node[id]).
Method implementation ...
String findLargestVolumeNote() {
int maxId = 0;
float maxVol = volume[0];
// searching array for loudest volume
for(int i = 0; i < volume.length; i++) {
if(volume[i] > maxVol) {
maxId = i;
maxVol = volume[i];
}
}
return note[maxId]; // returning corrsponding note
}
I'm currently trying to add some JUnit tests to my pathdrawing system in order to check if the calculations are right...
Currently I have:
Class to Test(MapRouteDrawer.java):
protected static double[] getCoords(PolynomialSplineFunction curve, double[] index) {
final double detailLevel = 1.0;//Tweak this value in order to achieve good rendering results
final double defaultCoordSize = index[index.length - 1];
final double[] coords = new double[(int)Math.round(detailLevel * defaultCoordSize) + 1];
final double stepSize = curve.getKnots()[curve.getKnots().length - 1] / coords.length;
double curValue = 0;
for (int i = 0; i < coords.length; i ++) {
coords[i] = curve.value(curValue);//gets y value of specified x value
curValue += stepSize;
}
return coords;
}
protected static double[] getIndex(Point[] points) {
final double[] index = new double[points.length];
if(index.length > 0){
index[0] = 0;
}
for (int i = 1; i < points.length; i++) {
index[i] = index[i - 1] + Math.sqrt(points[i - 1].distance(points[i]));
}
return index;
}
TestClass:
private Point[] dummyPoints = new Point[]{new Point(0,0), new Point(100,0), new Point(0,100)};//Some Points for Testing
//This returns a so called index - the squareroot distance between addedn on top of each other
private double[] dummyIndex = MapRouteDrawer.getIndex(dummyPoints);
#Test
public void testCurve(){
final double[] testYValues = new double[]{20, 40, 90};
final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
final double factor = testFunction.getKnots()[testFunction.getKnots().length - 1] / coords.length;
assertEquals(testYValues[0] * factor, coords[(int)Math.round(dummyIndex[0])], 1);//Check if the coordinates are equal
assertEquals(testYValues[1] * factor, coords[(int)Math.round(dummyIndex[1])], 1);
assertEquals(testYValues[2] * factor, coords[(int)Math.round(dummyIndex[2])], 1);
}
This is working fine, but if you are familiar with JUnit you may notice the delta of 1 which is neccesary in order for this Test to work...
What I'm trying to achieve is this:
#Test
public void testCurve(){
final double[] testYValues = new double[]{20, 40, 90};
final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
final double factor;//Unknown... should be dynamic, so it does not matter which detail level I chose
assertEquals(testYValues[0], coords[(int)Math.round(dummyIndex[0])] * factor, 0);//Check if the coordinates are equal
assertEquals(testYValues[1], coords[(int)Math.round(dummyIndex[1])] * factor, 0);//e.g. coords[(int)Math.round(dummyIndex[0])] * factor == 20 etc.
assertEquals(testYValues[2], coords[(int)Math.round(dummyIndex[2])] * factor, 0);
}
So that the first value in assertEquals() is 20, 40, 90 etc. and not a weird 21.39587576787686 (or similar) number and the delta is 0 (or 0.01 if there is no other way) and not 1 which I'm currently using
Because I'm using a detail level in my getCoords() method, which should be able to be adjusted without having to change the test, there is a need to make the "factor" in my test related to the coords-size.
How would I calculate the factor in order for the Test to succeed?
Any help is would be very much appreciated
I am trying to create a visual grid of this http://www.ibm.com/developerworks/library/j-coordconvert/ -Military Grid Reference System. I have the latitude/longitude to UTM and also to MGRS...which ar
17 T 330649 4689666
17TLG3064989666
But when going from MGRS to latitude I get the following:
[D#18e3f02a
public class CoordinateConversion {
public static void main(String args[]) {
CoordinateConversion test = new CoordinateConversion();
CoordinateConversion test2 = new CoordinateConversion();
test.latLon2UTM(35.58, 82.56);
System.out.println(test.latLon2UTM(42.340837, -83.055821));
System.out.println();
test2.latLon2UTM(35.58, 82.56);
System.out.println(test2.latLon2MGRUTM(42.340837, -83.055821));
CoordinateConversion test3 = new CoordinateConversion();
test3.latLon2UTM(35.58, 82.56);
//System.out.print(test3.mgrutm2LatLong(42.340837, -83.055821));
//System.out.println(test3.mgrutm2LatLong("02CNR0634657742"));
MGRUTM2LatLon mg = new MGRUTM2LatLon();
//mg.convertMGRUTMToLatLong("02CNR0634657742");
String MGRUTM = "17TLG3064989666";
System.out.println(mg.convertMGRUTMToLatLong(MGRUTM));
//for loop to be developed
}
public double[] utm2LatLon(String UTM) {
UTM2LatLon c = new UTM2LatLon();
return c.convertUTMToLatLong(UTM);
}
public double[] mgrutm2LatLon(String MGRUTM) {
MGRUTM2LatLon c = new MGRUTM2LatLon();
return c.convertMGRUTMToLatLong(MGRUTM);
}
}
and from this class:
public double[] convertMGRUTMToLatLong(String mgrutm) {
double[] latlon = {0.0, 0.0};
// 02CNR0634657742
int zone = Integer.parseInt(mgrutm.substring(0, 2));
String latZone = mgrutm.substring(2, 3);
String digraph1 = mgrutm.substring(3, 4);
String digraph2 = mgrutm.substring(4, 5);
easting = Double.parseDouble(mgrutm.substring(5, 10));
northing = Double.parseDouble(mgrutm.substring(10, 15));
LatZones lz = new LatZones();
double latZoneDegree = lz.getLatZoneDegree(latZone);
double a1 = latZoneDegree * 40000000 / 360.0;
double a2 = 2000000 * Math.floor(a1 / 2000000.0);
Digraphs digraphs = new Digraphs();
double digraph2Index = digraphs.getDigraph2Index(digraph2);
double startindexEquator = 1;
if ((1 + zone % 2) == 1) {
startindexEquator = 6;
}
double a3 = a2 + (digraph2Index - startindexEquator) * 100000;
if (a3 <= 0) {
a3 = 10000000 + a3;
}
northing = a3 + northing;
zoneCM = -183 + 6 * zone;
double digraph1Index = digraphs.getDigraph1Index(digraph1);
int a5 = 1 + zone % 3;
double[] a6 = {16, 0, 8};
double a7 = 100000 * (digraph1Index - a6[a5 - 1]);
easting = easting + a7;
setVariables();
double latitude = 0;
latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / Math.PI;
if (latZoneDegree < 0) {
latitude = 90 - latitude;
}
double d = _a2 * 180 / Math.PI;
double longitude = zoneCM - d;
if (getHemisphere(latZone).equals("S")) {
latitude = -latitude;
}
latlon[0] = latitude;
latlon[1] = longitude;
return latlon;
}
I am trying not to get into a large library where I will have to learn things that may be time consuming.
So I am trying to loop so I go east (easting) and north (northing) and cannot get past the point where I have one point - latitude/longitude.
Hope I have asked my question clearly without stating too much.
Any help will be appreciated.
Thanks,
-Terry
Your result from convertMGRUTMToLatLong() is an array of doubles, and by default, arrays are converted to String in a rather unreadable format in Java. That's where the [D#18e3f02a comes from. Try System.out.println(Arrays.toString(mg.convertMGRUTMToLatLong(MGRUTM))); and you'll get a more readable output.
In the method convertMGRUTMToLatLong(String s) you are returning an array latlon (i.e an object).
It returns its hashcode which is probably u dont want.
You want to print the array values. So in your main method you replace below line;
System.out.println(mg.convertMGRUTMToLatLong(MGRUTM));
with
double[] a = mg.convertMGRUTMToLatLong(MGRUTM) ;
System.out.println(a[0]+" " + a[1] );
Hope that helps!