Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Running and graphing a series of experiments We now want to run the same number

ID: 3790007 • Letter: R

Question

Running and graphing a series of experiments

We now want to run the same number of experiments on an increasingly larger set, and display the result on a graph.

We are going to modify the class BirthdayParadox to have it run the experiment on an increasingly larger set. By default it should run the experiments 1,000 times each time on a set of size 100, then on a set of size 200, and so on until the size of the set is 10000. So the following command will run 1,000 experiment on each of 100 sets (1,000 experiments on a set of size 100, 1,000 experiments on a set of size 200, etc., and 1,000 experiments on a set of size 10000):

> java BirthdayParadox

These default values should be overwritten when 3 parameters are passed on through the command line. The first parameter is the size by which the set will increment each time (as well as the initial set size), the second parameter is the maximum size for the set, and the third parameter is the number of experiments to run each time.

So the following command will run 10,000 experiments on a set of size 500, 10,000 experiments on a set of size 1000, etc., and 10,000 experiments on a set of size 5,000):

> java BirthdayParadox 500 5000 10000

In every case, we are going to graph the mean and the standard deviation obtained for each set size. We are going to use the library JFreeChart for this. To simplify our work, a class named ITI1121Chart has been prepared and packaged into the file ITI1121.jar.

An instance of the class ITI1121Chart can be created, passing the title of the graph as the parameter of the constructor.

The method addDataPoint can then be used to add a point to the graph. This method has 3 parameters, namely x, y, and the standard deviation around y.

Once all the data points have been added, the method render can be used to display the graph.

Once the graph is being displayed, one more question can be answered: what simple function seems to behave more or less like the result of our experiments? Luckily, the class ITI1121Chart has another method, addPolynome. This method takes one parameter k as input, and will superimpose to the graph the polynomial y = xk.

By trial and error, we want to add 3 curves to the graph: one curve that seem to grow “like” our experiment, another curve that grows slightly faster, and another one that grows slightly slower.

To make things a little cleaner, we will use three subdirectories: lib contains the jar files, src contains the java files, and classes the compiled class files.

Q2.zip is a zip file that contains the directory structure and the jar files in the lib subdirectory. The file HowTo.txt in the root directory contains compiling and running intructions

Javadoc for the class ITI1121Chart

The actual class ITI1121Chart.java has been included into the directory lib of the zip file if you want to have a look at it. This is not required to complete this question.

You have to implement this new feature and experiment with the results until you find suitable polynomials. The file you submit should automatically add the 3 polynomial to the drawing.

import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.data.xy.XYDataset;

import java.util.ArrayList;
import java.util.List;


public class PolyDataset extends AbstractXYDataset implements XYDataset {


private List<Double> polyList;
private List<Double> itemList;

public PolyDataset(){
super();
polyList = new ArrayList<Double>();
}

public void addPolynome(double poly) {
polyList.add(poly);
}

public void setItemList(List<Double> list){
itemList = list;
}
/**
* Returns the x-value for the specified series and item. Series are
* numbered 0, 1, ...
*
* @param series the index (zero-based) of the series.
* @param item the index (zero-based) of the required item.
*
* @return the x-value for the specified series and item.
*/
public Number getX(int series, int item) {
// return new Double( item);
return new Double( itemList.get(item));
}

/**
* Returns the y-value for the specified series and item. Series are
* numbered 0, 1, ...
*
* @param series the index (zero-based) of the series.
* @param item the index (zero-based) of the required item.
*
* @return the y-value for the specified series and item.
*/
public Number getY(int series, int item) {
return new Double(Math.pow(itemList.get(item),polyList.get(series)));
}

/**
* Returns the number of series in the dataset.
*
* @return the number of series in the dataset.
*/
public int getSeriesCount() {
return polyList.size();
}

/**
* Returns the key for a series.
*
* @param series the index (zero-based) of the series.
*
* @return The key for the series.
*/
public Comparable getSeriesKey(int series) {
return "k = " + polyList.get(series);
}

/**
* Returns the number of items in the specified series.
*
* @param series the index (zero-based) of the series.
* @return the number of items in the specified series.
*
*/
public int getItemCount(int series) {
return itemList.size();
}

}

Explanation / Answer

import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.data.xy.XYDataset;

import java.util.ArrayList;
import java.util.List;


public class PolyDataset extends AbstractXYDataset implements XYDataset {


private List<Double> polyList;
private List<Double> itemList;

public PolyDataset(){
super();
polyList = new ArrayList<Double>();
}

public void addPolynome(double poly) {
polyList.add(poly);
}

public void setItemList(List<Double> list){
itemList = list;
}
/**
* Returns the x-value for the specified series and item. Series are
* numbered 0, 1, ...
*
* @param series the index (zero-based) of the series.
* @param item the index (zero-based) of the required item.
*
* @return the x-value for the specified series and item.
*/
public Number getX(int series, int item) {
// return new Double( item);
return new Double( itemList.get(item));
}

/**
* Returns the y-value for the specified series and item. Series are
* numbered 0, 1, ...
*
* @param series the index (zero-based) of the series.
* @param item the index (zero-based) of the required item.
*
* @return the y-value for the specified series and item.
*/
public Number getY(int series, int item) {
return new Double(Math.pow(itemList.get(item),polyList.get(series)));
}

/**
* Returns the number of series in the dataset.
*
* @return the number of series in the dataset.
*/
public int getSeriesCount() {
return polyList.size();
}

/**
* Returns the key for a series.
*
* @param series the index (zero-based) of the series.
*
* @return The key for the series.
*/
public Comparable getSeriesKey(int series) {
return "k = " + polyList.get(series);
}

/**
* Returns the number of items in the specified series.
*
* @param series the index (zero-based) of the series.
* @return the number of items in the specified series.
*
*/
public int getItemCount(int series) {
return itemList.size();
}

}