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

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

首先感谢您的提前帮助。

我正在编写投资算法,目前正在预处理 CSV 历史数据。这部分过程的最终目标是创建一个 2k x 2k / 2(200 万)个条目的对称协方差矩阵。

我正在写的 Java class 包含一个 CSV 文件夹,每个文件夹包含 8 位信息,关键信息是日期、时间和开盘价。日期和时间已合并为一个 'seconds from delta' 时间度量,开盘价保持不变。输出的 CSV 包含以上两条信息,还有一个文件名索引供以后参考。

为了创建协方差矩阵,纽约证券交易所的每只股票每次都必须有一个价格值,如果缺少值,则无法正确完成矩阵。由于历史训练 CSV 中时间条目之间的差异,我必须使用多项式函数来估计遗漏值,然后可以将其输入链中的下一个过程。

我的问题听起来很简单,应该很容易解决(我可能是个大白痴)。我正在使用的多项式包包含两个双精度数组(Double[] x,Double[] y)。 X 与特定股票的 'seconds past delta' 时间值数组有关,Y 是相应的价格。当我尝试输入这些时,出现类型错误,因为我实际尝试输入的是 'java.lang.Double' 个对象。谁能帮我把后者的数组转换成前者的数组?

我意识到在主要打印语句之后有很多荒谬的东西,这些只是我在修补试图奇迹般地改变类型。

再次感谢您的宝贵时间,期待您的回复!

请在下面找到相关方法:

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);
    }

根据 the documentationPolynomialFunctionLagrangeForm 构造函数采用两个 double[] 数组,而不是 Double[]

因此您需要创建一个原始数组并传递它:

...
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();
}
...

另请参阅 How to convert an ArrayList containing Integers to primitive int array?,了解将 ArrayList<T>(其中 T 是基本类型的包装器)转换为相应原始数组 T[] 的一些替代方法。

请注意,您的代码中也有明显的拼写错误:

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

需要

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