package statistics;
import java.util.ArrayList;
import lipstone.joshua.parser.exceptions.ParserException;
import lipstone.joshua.parser.exceptions.PluginConflictException;
import lipstone.joshua.parser.plugin.ParserPlugin;
import lipstone.joshua.parser.plugin.helpdata.Operation;
import lipstone.joshua.parser.plugin.helpdata.Parameter;
import lipstone.joshua.parser.plugin.types.OperationPlugin;
import lipstone.joshua.parser.types.BigDec;
import lipstone.joshua.parser.util.ConsCell;
import lipstone.joshua.parser.util.ConsType;
import sets.NumberSet;
public final class StatisticsPlugin extends ParserPlugin implements OperationPlugin {
@Override
public ConsCell runOperation(String operation, ConsCell input) throws ParserException {
BigDec extra = null;
if (operation.equals("zScore")) {
ArrayList<ConsCell> parts = input.splitOnIdentifier(";");
if (parts.size() != 2)
throw new ParserException("The input for the zScore function did not include a valid comparator.", this);
input = parts.get(0);
extra = new BigDec(parser.run(parts.get(1)).toString());
}
ArrayList<ConsCell> list = input.splitOnSeparator();
BigDec[] items = new BigDec[list.size()];
for (int i = 0; i < items.length; i++)
items[i] = new BigDec(parser.run(list.get(i)).toString());
NumberSet set = new NumberSet(items);
try {
if (operation.equals("mean"))
return new ConsCell(set.getMean(), ConsType.NUMBER);
if (operation.equals("median"))
return new ConsCell(set.getMedian(), ConsType.NUMBER);
if (operation.equals("mode"))
return new ConsCell(set.getMode(), ConsType.NUMBER);
if (operation.equals("stdev"))
return new ConsCell(set.getSTDEV(1), ConsType.NUMBER);
if (operation.equals("zScore"))
return new ConsCell(set.getzScore(extra), ConsType.NUMBER);
if (operation.equals("iqr"))
return new ConsCell(set.getIQR(), ConsType.NUMBER);
}
catch (ParserException e) {
e.setThrower(this);
throw e;
}
return null;
}
@Override
public void loadOperations() throws PluginConflictException {
ArrayList<Parameter> params = new ArrayList<Parameter>();
params.add(new Parameter("Set", "Any comma-separated set of numbers, algebraic expressions, scripts, etc. provided that they evaluate to real numbers.", false));
addOperation(new Operation("mean", params, "The arithmatic mean of the set of numbers, defined as the sum of all the numbers in the set devided by the size of the set.", "This uses definition taught in K-12 schools.", this));
addOperation(new Operation("median", params, "The median of the set of numbers, defined as the middle number in the set.", "This uses definition taught in K-12 schools.", this));
addOperation(new Operation("mode", params, "The mode of the set of numbers, defined as the most commonly occuring number in the set.", "This uses definition taught in K-12 schools.", this));
addOperation(new Operation("stdev", params, "The standard deviation of the set of numbers.", "This uses definition taught in K-12 schools.", this));
addOperation(new Operation("iqr", params, "The interquartile range of the set of numbers.", "This uses definition taught in K-12 schools.", this));
params.remove(0);
params.add(new Parameter("Set", "Any comma-separated set of numbers, algebraic expressions, scripts, etc. provided that they evaluate to real numbers followed by a ; and the number to get the z-Score of.\n" +
"If the number is not in the set, it will automatically be added to the set.", false));
addOperation(new Operation("zScore", params, "The z-score of the set of numbers.", "This uses definition taught in K-12 schools.", this));
}
@Override
public void unloadOperations() throws PluginConflictException {/*Nothing to do here*/}
}