I have a dataset of daily prices for a portfolio (I do not have details about the constituents, simply the total daily price). My goal is to select n funds from a pool of funds and create a new portfolio with daily returns as close as possible compared to the original portfolio. I came up with the following formula to describe the problem:
with Yi the original portfolio returns, π the fund weights and X the fund returns.
Additionally, I defined 3 constraints:
To solve this I need to use ojAlgo. I tried the following but I almost instantly got stuck because of 2 issues.
public static void main(String[] [] args){
ExpressionsBasedModel model = new ExpressionsBasedModel();
float[] x1 = getDailyReturns("fund1");
float[] x2 = getDailyReturns("fund2");
float[] x3 = getDailyReturns("fund3");
Variable pi1 = new Variable("pi1");
Variable pi2 = new Variable("pi2");
Variable pi3 = new Variable("pi3");
List<Variable> variables = Arrays.asList(w1, w2, w3);
model.addVariables(variables);
Expression expr = model.addExpression("objective");
expr.setLinearFactor(pi1, x1);
expr.setLinearFactor(pi2, x2);
expr.setLinearFactor(pi3, x3);
expr.weight(1.0);
Optimisation.Result result = model.minimise();
}
First of all, I have X as a vector while .setLinearFactor can only use Number. Secondly I obviously missed something on how I should define the Expression.
Do you know where I could find examples helping me understanding how I am supposed to use the ExpressionBasedModel() for my problem?
Thank you
Related
So I have text files that I can parse with the code I have and make the calculations for some atoms in space. Thing is I have to make the program parallel using threads and I don't know how I am supposed to pass the text file in every thread.
If I try to do something like double kx = console.nextDouble(); it doesn't recognize what console is because its Scanner object inside of main(). What am I missing?
(Note: it's not strictly necessary to use threads directly; I could use fork-join pool, etc.)
public class NBodyBH {
//Set value to simulate <execTime> seconds of the bodies
static final int execTime = 100;
static final boolean draw = false;
public static void main(String[] args) throws FileNotFoundException {
String fname = args[0];
FileInputStream is = new FileInputStream(new File(fname));
System.setIn(is);
Scanner console = new Scanner(System.in);
final double dt = 0.1; // time quantum
int N = console.nextInt(); // number of particles
double radius = console.nextDouble(); // radius of universe
//BELOW HERE IS THE PART I WANT TO MAKE PARALLEL
Body[] bodies = new Body[N]; // array of N bodies
for (int i = 0; i < N; i++) {
double px = console.nextDouble();
double py = console.nextDouble();
double vx = console.nextDouble();
double vy = console.nextDouble();
double mass = console.nextDouble();
int red = console.nextInt();
int green = console.nextInt();
int blue = console.nextInt();
Color color = new Color(red, green, blue);
bodies[i] = new Body(px, py, vx, vy, mass, color);
}
}
}
I believe what you're looking for is AsynchronousFileChannel. It was introduced in Java 7 for async operations on files.
Although, you're saying you'll run a complex calculation so maybe the time intensive task is the calculation itself instead of the file reading, you could read the file in your main thread and use parallelism to run the complex calculations.
List<Integer> calculationResults = bodies.parallelStream()
.map(doComplexCalculation)
.collect(toList())
In the example I'm assuming the calculation returns an Integer and that bodies is a Collection like a List, but adapt it as you see fit.
Passing the file to each thread sounds like a bad idea. Then you'd have to coordinate which thread reads which data, which would be unnecessarily complicated, or (more likely), each thread needs all the data, and making each thread re-read the file is inefficient.
First, read all the data. Then pass each thread the data it needs.
Don't assign your new FileInputStream to System.in. Just create a Scanner with it directly:
Scanner input = new Scanner(Paths.get(arg[0]));
...
List<Body> bodies = new ArrayList<>(N);
for (int i = 0; i < N; ++i) {
...
bodies.add(new Body(...));
}
for (int step = 0; step < 10; ++step) { /* Simulate 10 time steps, or whatever. */
List<Body> next = bodies.parallelStream() /* Here's your threads. */
.map(body -> step(body, dt, bodies))
.collect(Collectors.toList());
/* Render new state */
...
bodies = next; /* Throw away your old state. */
}
Here, step() is a function you need to write. If I've guessed what you are doing correctly, it will create a clone of the body, with a new position and velocity, based on the positions of the others. If you can modify the code of the Body class, this method should probably be a member of that class.
You must be careful not to modify the "current" bodies, but only create replacement for each. This upholds the "non-interference" requirement of Stream parallelism. Each time step will take O(N2) time to compute, since each new state of each body (N) depends on the state of every other body (× N).
I've tried to create a basic NN using the book
"Make Your Own Neural Network" by Tariq Rashid
and using the coding train videos:
https://www.youtube.com/watch?v=XJ7HLz9VYz0&list=PLRqwX-V7Uu6aCibgK1PTWWu9by6XFdCfh
and the nn.js class on the coding train git as a reference
https://github.com/shiffman/Neural-Network-p5/blob/master/nn.js
I writing the NN network in java, and I tried just like in the playlist to train the network on XOR after I succeeded in doing a single perceptron.
but for some reason, even though my code is similar to what the book is doing and same in the videos (except in the videos he using JS).
when I train the network for around 500000 times with randomized data set of XOR inputs (total of 4 input [1,0] [0,1] [0,0] [1,1]).
when I giving it to guess after the training the all the 4 options I get results closer to 0.5 than to 1,1,0,0 (the order of the inputs in the test are [1,0] [0,1] [0,0] [1,1])
this is my training function:
public void train(double [] inputs, double[] target) {
//generates the Hidden layer values
this.input = Matrix.fromArrayToMatrix(inputs);
feedForward(inputs);
//convert to matrices
Matrix targets = Matrix.fromArrayToMatrix(target);
//calculate the output error
Matrix outputErrors = Matrix.subtract(targets, output);
//calculate the Gradient
Matrix outputGradient = Matrix.map(output, NeuralNetwork::sigmoidDerivative);
outputGradient = Matrix.matrixMultiplication(outputGradient, outputErrors);
outputGradient.multiply(this.learningRate);
//adjust the output layer bias
this.bias_Output.add(outputGradient);
//calculate the hidden layer weights delta
Matrix hiddenT = Matrix.Transpose(hidden);
Matrix hiddenToOutputDelta = Matrix.matrixMultiplication(outputGradient, hiddenT);
//adjust the hidden layer weights
this.weightsHiddenToOutput.add(hiddenToOutputDelta);
//calculate the hidden layer error
Matrix weightsHiddenToOutputT = Matrix.Transpose(weightsHiddenToOutput);
Matrix hiddenErrors = Matrix.matrixMultiplication(weightsHiddenToOutputT, outputErrors);
//calculate the hidden gradient
Matrix hiddenGradient = Matrix.map(this.hidden, NeuralNetwork::sigmoidDerivative);
hiddenGradient = Matrix.matrixMultiplication(hiddenGradient, hiddenErrors);
hiddenGradient.multiply(this.learningRate);
//adjust the hidden layer bias
this.bias_Hidden.add(hiddenGradient);
//calculate the input layer weights delta
Matrix inputT = Matrix.Transpose(this.input);
Matrix inputToHiddenDelta = Matrix.matrixMultiplication(hiddenGradient, inputT);
//adjust the hidden layer weights
this.weightsInputToHidden.add(inputToHiddenDelta);
}
those are the sigmoid functions:
private static double sigmoid(double x) {
return 1d / (1d+ Math.exp(-x));
}
private static double sigmoidDerivative(double x) {
return (x * (1d - x));
}
I'm using this method to calculate the derivative because the network already getting the sigmoid function on the feed-forward process so all I do is calculate the derivative like that.
and this is my guess/ feedforward function:
public double[] feedForward(double [] inputs) {
double[] guess;
//generates the Hidden layer values
input = Matrix.fromArrayToMatrix(inputs);
hidden = Matrix.matrixMultiplication(weightsInputToHidden, input);
hidden.add(bias_Hidden);
//activation function
hidden.map(NeuralNetwork::sigmoid);
//Generates the output layer values
output = Matrix.matrixMultiplication(weightsHiddenToOutput, hidden);
output.add(bias_Output);
//activation function
output.map(NeuralNetwork::sigmoid);
guess = Matrix.fromMatrixToArray(output);
return guess;
}
this is in the main class the data set I'm giving him:
NeuralNetwork nn = new NeuralNetwork(2,2,1);
double [] label0 = {0};
double [] label1 = {1};
Literal l1 = new Literal(label1,0,1);
Literal l2 = new Literal(label1,1,0);
Literal l3 = new Literal(label0,0,0);
Literal l4 = new Literal(label0,1,1);
Literal[] arr = {l1, l2, l3, l4};
Random random = new Random();
for(int i = 0 ; i<500000 ; i++) {
Literal l = arr[i%4];
nn.train(l.getTruthValue(), l.getLabel());
}
System.out.println(Arrays.toString(nn.feedForward(l1.getTruthValue())));
System.out.println(Arrays.toString(nn.feedForward(l2.getTruthValue())));
System.out.println(Arrays.toString(nn.feedForward(l3.getTruthValue())));
System.out.println(Arrays.toString(nn.feedForward(l4.getTruthValue())));
but for some reason the outputs look like that:
[0.47935468493879807]
[0.5041956026507048]
[0.4575246472403595]
[0.5217568912941623]
I've tried changing it to subtract instead of add-on every bias and weights update (cause you need the negative gradient although both in the book and in the videos they use add instead of subtract) meaning changing those 4 lines to subtract:
this.bias_Output.subtract(outputGradient);
this.weightsHiddenToOutput.subtract(hiddenToOutputDelta);
this.bias_Hidden.subtract(hiddenGradient);
this.weightsInputToHidden.subtract(inputToHiddenDelta);
those are the 2 main outputs I get:
[0.9999779359460259]
[0.9999935716126019]
[0.9999860145346924]
[0.999990155468117]
or
[1.7489664881918983E-5]
[6.205315404676972E-6]
[8.41530873105465E-6]
[1.1853929628341918E-5]
I'm pretty sure the problem isn't in my Matrix class that I've created because I checked it before and all the add, subtract, multiply, transpose and it worked fine.
I would really appreciate if someone could look at this code and help me to figure out the problem
I am new to Java, and I am reading a book on it now. The book does not give me the answer. I am using the following code:
package loanpayments;
public class LoanPayments {
public static void main(String[] args) {
double years = Double.parseDouble(args[0]);
double P = Double.parseDouble(args[1]);
double r = Double.parseDouble(args[2]);
double R = r / 100;
double A = P*(Math.E*Math.exp(R*years));
System.out.println(A);
}
}
I am testing the code with the following values:
years = 3
P = 2340
r = 3.1
First I have to divide r by 100 to get a correct value (in this case it becomes 0.031). The new value of 0.031 becomes capitalized R. Then I use the formula to find A.
I am getting an incorrect output of ~6980.712, when the output should instead be ~2568.060.
I am thinking that I put in the formula wrong, it should be this:
Pe^R(years)
In this case e is Euler's number (~2.71828)
If anyone could advise me on how to fix the formula, or some other mistake, I would much appreciate it, thanks.
Not needed to multiply with another e because Math.exp() is already the exponential function.
I have implemented Logistic Regression with Gradient Descent in Java. It doesn't seem to work well (It does not classify records properly; the probability of y=1 is a lot.) I don't know whether my implementation is correct.I have gone through the code several times and i am unable to find any bug. I have been following Andrew Ng's tutorials on Machine learning on Course Era. My Java implementation has 3 classes. namely :
DataSet.java : To read the data set
Instance.java : Has two members : 1. double[ ] x and 2. double label
Logistic.java : This is the main class that implements Logistic Regression with Gradient Descent.
This is my cost function:
J(Θ) = (- 1/m ) [Σmi=1 y(i) log( hΘ( x(i) ) ) + (1 - y(i) ) log(1 - hΘ (x(i)) )]
For the above Cost function, this is my Gradient Descent algorithm:
Repeat ( Θj := Θj - α Σmi=1 ( hΘ( x(i)) - y(i) ) x(i)j
(Simultaneously update all Θj )
)
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Logistic {
/** the learning rate */
private double alpha;
/** the weight to learn */
private double[] theta;
/** the number of iterations */
private int ITERATIONS = 3000;
public Logistic(int n) {
this.alpha = 0.0001;
theta = new double[n];
}
private double sigmoid(double z) {
return (1 / (1 + Math.exp(-z)));
}
public void train(List<Instance> instances) {
double[] temp = new double[3];
//Gradient Descent algorithm for minimizing theta
for(int i=1;i<=ITERATIONS;i++)
{
for(int j=0;j<3;j++)
{
temp[j]=theta[j] - (alpha * sum(j,instances));
}
//simulataneous updates of theta
for(int j=0;j<3;j++)
{
theta[j] = temp[j];
}
System.out.println(Arrays.toString(theta));
}
}
private double sum(int j,List<Instance> instances)
{
double[] x;
double prediction,sum=0,y;
for(int i=0;i<instances.size();i++)
{
x = instances.get(i).getX();
y = instances.get(i).getLabel();
prediction = classify(x);
sum+=((prediction - y) * x[j]);
}
return (sum/instances.size());
}
private double classify(double[] x) {
double logit = .0;
for (int i=0; i<theta.length;i++) {
logit += (theta[i] * x[i]);
}
return sigmoid(logit);
}
public static void main(String... args) throws FileNotFoundException {
//DataSet is a class with a static method readDataSet which reads the dataset
// Instance is a class with two members: double[] x, double label y
// x contains the features and y is the label.
List<Instance> instances = DataSet.readDataSet("data.txt");
// 3 : number of theta parameters corresponding to the features x
// x0 is always 1
Logistic logistic = new Logistic(3);
logistic.train(instances);
//Test data
double[]x = new double[3];
x[0]=1;
x[1]=45;
x[2] = 85;
System.out.println("Prob: "+logistic.classify(x));
}
}
Can anyone tell me what am I doing wrong?
Thanks in advance! :)
As I am studying logistic regression, I took the time to review your code in detail.
TLDR
In fact, it appears the algorithm is correct.
The reason you had so much false negatives or false positives is, I think, because of the hyper parameters you choose.
The model was under-trained so the hypothesis was under-fitting.
Details
I had to create the DataSet and Instance classes because you did not publish them, and set up a train data set and a test data set based on the Cryotherapy dataset.
See http://archive.ics.uci.edu/ml/datasets/Cryotherapy+Dataset+.
Then, using your same exact code (for the logistic regression part) and by choosing an alpha rate of 0.001 and a number of iterations of 100000, I got a precision rate of 80.64516129032258 percent on the test data set, which is not so bad.
I tried to get a better precision rate by tweaking manualy those hyper parameters but could not obtain any better result.
At this point, an enhancement would be to implement regularization, I suppose.
Gradient descent formula
In Andrew Ng's video about the the cost function and gradient descent, it is correct that the 1/m term is omitted.
A possible explanation is that the 1/m term is included in the alpha term.
Or maybe it's just an oversight.
See https://www.youtube.com/watch?v=TTdcc21Ko9A&index=36&list=PLLssT5z_DsK-h9vYZkQkYNWcItqhlRJLN&t=6m53s at 6m53s.
But if you watch Andrew Ng's video about regularization and logistic regression you'll notice that the term 1/m is clearly present in the formula.
See https://www.youtube.com/watch?v=IXPgm1e0IOo&index=42&list=PLLssT5z_DsK-h9vYZkQkYNWcItqhlRJLN&t=2m19s at 2m19s.
I have started how to learn Java programming and I a got a problem while doing an asignment:
ps this is a c programming assignment, using it to do a java programming:
Consider a (potentially large commercial) room that has floor dimensions length len and width wid. The area of the floor is given by multiplying len by wid.
Carpet tiles are provided as individual squares (2 feet by 2 feet). Write, compile and run a C program, carpet.c, that
-calculates the minimum number of carpet tiles that are needed to cover a room whose dimensions (len and wid) are input from the terminal (measurements are in inches). NB: floor tiles can be cut to fit at the edges of the room - try to minimise waste.
-calculates how many carpet-tile packs are needed. The supplier only provides carpet tiles in packs of ten (i.e. individual tiles cannot be purchased separately).
-calculates how many spare tiles are left over once the minimum number of packs have been purchased.
-prints out all these results in a neat table along with the total cost of the order. (A pack of tiles costs $50 for each of the first four packs, and the price reduces to $45 for each subsequent pack.)
Think carefully about how are you going to test your program? The calculations are non-trivial and easy to get wrong. If your program doesn't work then you will waste the company a lot of money and you will probably lose the contract to supply the software.
You should write out a number of test cases (by hand) that cover all the different possibilities that could happen. Don't forget to consider various boundary cases too - these are often where errors are detected.
So far I have done:
import java.util.Scanner;
public class carpet {
public static void main (String args[]){
Scanner scanf = new Scanner (System.in);
float len, wid;
float area;
int roundTiles;
int roundPacks;
float tarea;
float tpack;
float NoOfTiles;
float NoOfPacks;
float tspares1;
float tspares2;
int packCost;
int cost;
tarea= 12* 12;
tpack= 10;
System.out.format("Enter the length of the room, Inches: ");
len = scanf.nextFloat();
System.out.format("Enter the width of the room, Inches: ");
wid = scanf.nextFloat();
area = len * wid;
NoOfTiles = area/ tarea;
NoOfPacks = NoOfTiles/tpack;
roundTiles = (int) Math.ceil(NoOfTiles);
roundPacks = (int) Math.ceil(NoOfPacks);
tspares1 = roundPacks * 10;
tspares2 = tspares1 - roundTiles;
if (roundPacks <= 4)
packCost =50;
else if(roundPacks > 4)
{
packCost = 45;
packCost = packCost + 20; *<<-----******LINE 50-----*********
}
cost =roundPacks * packCost; *<<*******---ERROR-------------*********
System.out.println(cost);
}
}
The error says: "The local variable packCost may not have been initialized"
AND the compiler says: "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable packCost may not have been initialized
at carpet.main(carpet.java:50)
"
You need to initialize the variable packCost by doing replacing the current line:
int packCost;
by
int packCost=0;
You are initializing your packCost variable in a conditional statement and the compiler is not smart enough to detect it.
Using else instead of else-if makes it clear to the compiler that one of the conditions will be executed, so packCost is always set:
if (roundPacks <= 4)
packCost = 50;
else
{
packCost = 45;
packCost = packCost + 20;
}
You need to initialise packetCost:
int packCost = 0;
In Java, local variables need to be initialized. If this was a field variable, it would have an implicit default value of 0 (zero).
Here is a link that explains this: http://www.janeg.ca/scjp/lang/defaults.html (disclaimer: not my page)
From this page:
Field variables (class members) are automatically initialized to default values
Local variables (method or constructor variables) are not automatically initialized