简体   繁体   中英

Java.Lang.Double[] to Double[] issue for Polynomial from CSV

First of all thanks for your help in advance.

I'm writing an investment algorithm and am currently pre-processing CSV historical data. The end goal for this part of the process is to create a symmetrical co-variance matrix of 2k x 2k / 2 (2 million) entries.

The Java class I'm writing takes a folder of CSVs each with 8 bits of information, key ones being Date, Time & Opening stock price. Date & time have been combined into one 'seconds from delta' time measure and opening stock prices remain unchanged. The output CSV contains the above two pieces of information also with a filename index for later referencing.

In order to create the co-variance matrix each stock on the NYSE must have a price value for every time, if values are missing the matrix cannot be properly completed. Due to discrepancies between time entries in the historical training CSV, I have to use a polynomial function to estimate missed values, which then can be fed into the next process in the chain.

My problem sounds fairly simple and should be easy to overcome (I'm probably being a massive idiot). The polynomial package I'm using takes in two arrays of doubles (Double[] x, Double[] y). X pertaining to an array of the 'seconds past delta' time values of a particular stock and Y the corresponding price. When I try to feed these in I'm getting a type error as what I'm actually trying to input are 'java.lang.Double' objects. Can anyone help me with converting an array of the latter to an array of the former?

I realise there is a load of ridiculousness after the main print statement, these are just me tinkering trying to miraculously change the type.

Again thanks for your time, I look forward to your replies!

Please find the relevant method below:

public void main(String filePath) throws IOException {

    String index = filePath;
    index = index.replace("/Users/louislimon/Desktop/Invest Algorithm/Data/Samples US Stock Data/data-1/5 min/us/nyse stocks/1/", "");
    index = index.replace(".us.txt", "");

    File fout = new File("/Users/louislimon/Desktop/Invest Algorithm/Data.csv");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));


    Reader in = new FileReader(filePath);

    Iterable<CSVRecord> records;

    try {

        records = CSVFormat.EXCEL.withSkipHeaderRecord(true).parse(in);

    } catch ( IOException ex ) {
        System.out.println ( "[ERROR] " + ex );
        return;
    }

    ZoneId zoneId = ZoneId.of("America/New_York");

    boolean tmp = true;

    Instant firstInstant = null; // Track the baseline against which we calculate the increasing time


    ArrayList<Double> timeVals = new ArrayList<Double>();
    ArrayList<Double> priceVals = new ArrayList<Double>();

    for ( CSVRecord record : records ) {

        if(tmp){
            tmp = false;
        }
        else {
            //System.out.println(record.toString());

            String dateInput = record.get(0);
            String timeInput = record.get(1);
            Double price = Double.parseDouble(record.get(2));

            LocalDate date = LocalDate.parse(dateInput);
            LocalTime time = LocalTime.parse(timeInput);
            //Double price = Double.parseDouble(priceInput);

            LocalDateTime ldt = LocalDateTime.of(date, time);

            ZonedDateTime zdt = ldt.atZone(zoneId);

            Instant instant = zdt.toInstant();  // Use Instant (moment on the timeline in UTC) for data storage, exchange, serialization, database, etc.
            if (null == firstInstant) {
                firstInstant = instant;  // Capture the first instant.
            }

            Duration duration = Duration.between(firstInstant, instant);
            Long deltaInSeconds = duration.getSeconds();

            double doubleDeltaInSeconds = deltaInSeconds.doubleValue();

            timeVals.add(doubleDeltaInSeconds);
            priceVals.add(price);

            //System.out.println("deltaInSeconds: " + deltaInSeconds + " | price: " + price + " | index: " + index);
        }


        Double [] timeValsArray = timeVals.toArray(new Double[timeVals.size()]);
        Double [] priceValsArray = timeVals.toArray(new Double[priceVals.size()]);


        Double[] timeFeed = new Double[timeVals.size()];
        Double[] priceFeed = new Double[priceVals.size()];

        for(int x = 0;x<timeVals.size(); x++) {
            timeFeed[x] = new Double (timeValsArray[x].doubleValue());
            priceFeed[x] = new Double (priceValsArray[x]);
        }

       PolynomialFunctionLagrangeForm pflf =  new PolynomialFunctionLagrangeForm(timeFeed,priceFeed);
    }

According to the documentation , the PolynomialFunctionLagrangeForm constructor takes two double[] arrays, not Double[] .

Hence you need to create a raw array and pass that:

...
double[] timeFeed = new double[timeVals.size()];
double[] priceFeed = new double[priceVals.size()];

for(int x = 0; x < timeVals.size(); x++) {
    timeFeed[x] = timeValsArray[x].doubleValue();
    priceFeed[x] = priceValsArray[x].doubleValue();
}
...

See also How to convert an ArrayList containing Integers to primitive int array? for some alternative ways to convert an ArrayList<T> (where T is a wrapper for a primitive type) to the corresponding raw array T[] .

Note that there is also obviously a typo in your code:

 Double [] priceValsArray = timeVals.toArray(new Double[priceVals.size()]);

needs to be

 Double [] priceValsArray = priceVals.toArray(new Double[priceVals.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM