generating random numbers

generating random numbers

We would like to be able to predict tomorrow's price of a share of stock. We have data on past daily prices. Based on that we will make a prediction. Our plan is to use a weighted average of the 5 most recent prices as the prediction.
We will choose the appropriate weights as the ones that would have best predicted prices in the past. The weights must add up to one to be a weighted average, but some of them may be negative. We will restrict consideration to weights that are chosen from the following 21 values:
-1.0, -0.9, ... , -0.1, 0.0, 0.1, ..., 0.9, 1.0
We define the "error" of a prediction to be the absolute value of the difference between the prediction and the price.We will evaluate a possible weighting by using it to predict each of the known prices (except for the first 5, which don't have enough predecessors). We will then choose the weighting that has the smallest average error for its predictions.
Before we use our weighted averaging scheme to make our fortune on the stock market we need to have some idea of how well it predicted past data. Create a class Predicting that contains a method avgError that will be given a double[] data and will return the average error made by our best weighting.
Definition


Class: Predicting
Method: avgError
Parameters: double[]
Returns: double
Method signature: double avgError(double[] data)
(be sure your method is public)

Notes

- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints

- data will contain between 6 and 50 elements inclusive.
- Each element of data will be between 10.0 and 100.0 inclusive.
Examples

0)

{10,10,10,10,10,10}
Returns: 0.0
A weighting of .2,.2,.2,.2,.2 will exactly predict the only past price that had 5 predecessors.
1)

{50,10,50,10,50,10,50,10,50,10,50,10}
Returns: 0.0
A weighting of -1,0,0,1,1 predicts price correctly every time (in the past). For example, the prediction of the most recent price is -1*50 + 0*10 + 0*50 + 1*10 + 1*50 = 10 which was exactly right.
2)

{50,60,50,60,50,60,60}
Returns: 5.0
The best choice of weights is -1.0,-1.0,1.0,1.0,1.0 which gives a prediction of 50 for the next to the last price (-1*50 + -1*60 + 1*50 + 1*60 + 1*50 = 50) and a prediction of 60 for the last price (-1*60 + -1*50 + 1*60 + 1*50 + 1*60 = 60). So the errors are 10 and 0 for the two predictions with an average error of 5.
3)

{82.9102, 70.6848, 21.503, 61.4588, 54.7789,
48.9889, 57.6766, 91.1859, 26.3674, 55.4601,
53.9357, 87.2005, 78.4771, 65.0102, 18.619,
90.296, 26.3894, 53.8588, 91.8369, 58.8028,
74.0577, 28.2406, 65.609, 59.4867, 27.7544,
54.6992, 69.2428, 22.6264, 87.0083, 58.5116,
60.286, 20.4318, 65.6475, 11.8348, 36.3488,
92.8092, 60.7392, 98.124, 48.1292, 39.5459,
52.2657, 34.3519, 38.9279, 93.0152, 11.3157}
Returns: 22.0175905
View Answers

May 27, 2010 at 5:24 PM

Hi Friend,

Try the following code:

import java.util.*;
class Predicting{

public static double avgError(double []array){
double aa[]=new double[5];
for(int i=0;i<5;i++){
aa[i]=array[i];
}
double max=1.0;
double min= -1.0;
double ran[]=new double[5];
double error;
double sum=0,sum1=0;
for(int i=0;i<5;i++){
ran[i]=(double)(Math.random() * (max - min + 1) ) + min;
}
double[] arr=new double[5];
for(int i=0;i<arr.length;i++){
arr[i]=ran[i]*aa[i];
sum+=arr[i];
}
double last=0;
for(int i=0;i<array.length;i++){
last=array[array.length-1];
}

if(sum==last){
error=sum-last;

}
else {
shiftRight(array,1);
double aaa[]=new double[5];
for(int i=0;i<5;i++){
aaa[i]=array[i];
}

for(int i=0;i<arr.length;i++){
arr[i]=ran[i]*aaa[i];
sum1+=arr[i];
}
double e=last-sum;
double f=last-sum1;
double v=e+f;
error=v/2;

}
return error;

}
public static double[] shiftRight(double[] array, int amount) {
for (int j = 0; j < amount; j++) {
double a = array[array.length - 1];
int i;
for (i = array.length - 1; i > 0; i--)
array[i] = array[i - 1];
array[i] = a;
}
return array;
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter 10 values:");
double arr[]=new double[10];
for(int i=0;i<arr.length;i++){
arr[i]=input.nextDouble();
}
double err=Predicting.avgError(arr);
System.out.println(err);
}
}

Thanks









Related Tutorials/Questions & Answers:
Generating random numbers in a range with Java
Generating random numbers in a range with Java  Generating random numbers in a range with Java
generating random numbers - Java Beginners
generating random numbers  We would like to be able to predict tomorrow's price of a share of stock. We have data on past daily prices. Based on that we will make a prediction. Our plan is to use a weighted average of the 5 most
Advertisements
Generating Random Numbers to Fill array. Java Beginner needing help!
Generating Random Numbers to Fill array. Java Beginner needing help!  ... a program that produces random permutations of the numbers 1 to 10. eg... the permuted numbers. Make a second array and fill it with the numbers 1 to 10
random numbers
random numbers  hi.. i am creating a website and on feedback form to authenticate user i want to generate a random number on label.if user types that number correctly in textbox then he is allowed to submit feedback otherwise
random numbers
random numbers  Hi I need some help with the code for the this program. This program should randomly choose a number between 1 and 1000, and then challenge the user to guess what it is, giving hints ('too high!' or 'too low
Generate Random Numbers
Generate Random Numbers  hi,,, Please give me answer with example How do you generate random numbers within a given limit with actionscript... This function generate random numbers with in given limit
random numbers - Java Beginners
random numbers  write a program to accept 50 numbers and display 5 numbers randomly  Hi Friend, Try the following code: import...(); System.out.println("Random numbers are: "); int random[]=new int[5]; for(int i=0;i
random numbers - Java Beginners
random numbers  Hello i need this code to be modified for me to display the random numbers, but not twice or more. I mean i need a number...("Random numbers are: "); int random[]=new int[5]; for(int i=0;i  Hi
Random numbers - Development process
Random numbers  hi, How to generate unique random numbers between range like(10 to 50)  Hi friend, class RandomNumber { public static void main(String[] args) { //set the variable aNumber
Generating Random Number
Generating Random Number       In many places you need random numbers to fulfill your requirements. Java provides classes to help you in generation random numbers
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  ... of Scala programming and it generates random integer. Following program
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  ... of Scala programming and it generates random integer. Following program
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  ... of Scala programming and it generates random integer. Following program
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
generate random numbers and display the largest
generate random numbers and display the largest  Hi, I am using... creates an array and fills it with random ints, prints the contents... ArrayRandom{ public static void main(String args[]){ Random r = new Random
Generate random numbers from 1 to 100
Generate random numbers from 1 to 100  1)A class Called: RandomNumberGenerator that generate random numbers from 1 to 100 2)A class Test that tests... an object from the Random NumberGenerator class to generate your input test data
Generate random numbers in Java
Generate random numbers in Java - How to use the java.util.Random class... or a set of random numbers in your program. We are using the java.util.Random class for generating the random number. Random class is an utility class
Generate array of random numbers without repetition
Generate array of random numbers without repetition In the previous section, you have learnt how to generate an array of random numbers from the given array... created an application that will generate the array of random numbers without
Generate array of random numbers
Generate array of random numbers You can generate a random number either...() but it is better to use Random class. If you want to generate a series of random numbers...(); } System.out.println(); System.out.println("Random numbers are: "); int random
Random in jsp
Random in jsp          Random numbers are the numbers that are determined entirely by chance. User does not have any control over the working of random numbers
How do I generate random number?
there is a requirement of generating random numbers between two given numbers. How do I write code for generating the random number? Thanks   Hi, The java.util.Random class can be used to generate the random numbers. Check the tutorial
How to generate random number in java
type numbers. Random Numbers in Java with Example As we discussed that to create... five random numbers. If you are using java Math.random  it will generate...How to generate random number in java In this section you will learn how
Javascript random number - Design concepts & design patterns
are retreived from database.I have to generate random numbers corresponding to every row.But problem is one constraint is there while generating random numbers.If... one group there will be 10 rows and their random numbers will be same.Then next
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
Generating report in java
Generating report in java  How can I generate a report in java
java programming:generating series
java programming:generating series  WAP to print series: | + || + ||| + |||| + .......... n terms
Generating pdf in Spring
Generating pdf in Spring  Sir/Madam, I need your help in generating a pdf by fetching the data form database and by using unicode in spring framework
Javascript random number - Design concepts & design patterns
are retreived from database.I have to generate random numbers corresponding to every row.But problem is one constraint is there while generating random numbers.If... group there will be 10 rows and their random numbers will be same.Then next 10
getting random number in java
getting random number in java  getting random number in java Hi everyone, i am trying to get a random number in a particular range. for example random numbers between 1 to 100 in Java. Though i m new to Java, i have tried many
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to use a File object and print even numbers from two to ten. Then using RandomAccessFile write numbers from 1 to 5. Then using seek () print only the last 3 digits
random number
random number  Sir could u please send me the code registration form of roseindia.net with image varification
random number
random number   Please How do I generate a program that gives me random integer from 115 to 250? Using java.util.random. Thank you very much!  ... main(String[] args){ int min = 115; int max = 250; Random random
How to create a random matrix in r?
random numbers can be generated with the help of function rnorm(n, mean = , sd...How to create a random matrix in r?  Hi, I want to create random matrix in R programming language. How to create a random matrix in r? Explain me
Java programming: generating series
Java programming: generating series  Write a program to accept a string using buffered reader and replace character sequence 'cat' with 'dog'   Here is a java code that accept the string from the user using
java programming:generating series
java programming:generating series  Write a program to generate series: 12345 1234 123 12 1 1 12 123 1234 12345 12345   Here is a code that displays the following pattern: 12345 1234 123 12 1 1 12 123 1234
java programming:generating series
java programming:generating series  12345 1234 123 12 1 1 12 123 1234 12345   Here is a code that displays the following pattern: 12345 1234 123 12 1 1 12 123 1234 12345 class Pattern { public
Generating dynamic fields in struts2
Generating dynamic fields in struts2  Hi, I want generate a web page which should have have some struts 2 tags in a group and a "[+]" button for the group. On click of this button one more group of fields should be generated
java programming:generating series
java programming:generating series  1234554321 1234 4321 123 321 12 21 1 1   Here is a java code that displays the following pattern 1234554321 1234 4321 123 321 12 21 1 1 class Pattern
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link:ADS_TO_REPLACE_1 Automorphic numbers Thanks
Pick at Random
the entry from the array before selecting another name at random
Generating bill in servlet - Development process
Generating bill in servlet  I want to generate the bill using servlet for the resturant so any one please send me code
Random Access File.
Random Access File.  Explain Random Access File
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
numbers
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents

Ads