简体   繁体   English

CSV中多项式的Java.Lang.Double []到Double []问题

[英]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. 我正在编写一种投资算法,目前正在预处理CSV历史数据。 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. 该过程这一部分的最终目标是创建一个2k x 2k / 2(200万)项的对称协方差矩阵。

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. 我正在编写的Java类使用一个CSV文件夹,每个文件夹包含8位信息,关键信息是日期,时间和开盘价。 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. 输出CSV包含上述两条信息,还包含文件名索引以供以后参考。

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. 由于历史训练CSV中的时间条目之间存在差异,因此我必须使用多项式函数来估计缺失值,然后可以将其输入到链中的下一个过程中。

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). 我正在使用的多项式包采用两个双精度数组(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. X与特定股票的“秒后增量”时间值数组有关,Y与相应的价格有关。 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. 当我尝试提供这些信息时,我遇到了类型错误,因为我实际上想输入的是'java.lang.Double'对象。 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[] . 根据文档PolynomialFunctionLagrangeForm构造函数采用两个double[]数组,而不是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? 另请参阅如何将包含整数的ArrayList转换为原始int数组? for some alternative ways to convert an ArrayList<T> (where T is a wrapper for a primitive type) to the corresponding raw array T[] . 用于将ArrayList<T> (其中T是原始类型的包装器)转换为相应的原始数组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()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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