繁体   English   中英

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

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

首先首先感谢您的帮助。

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

我正在编写的Java类使用一个CSV文件夹,每个文件夹包含8位信息,关键信息是日期,时间和开盘价。 日期和时间已合并为“距离增量秒”的时间度量,开盘股价保持不变。 输出CSV包含上述两条信息,还包含文件名索引以供以后参考。

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

我的问题听起来很简单,应该很容易克服(我可能是个笨蛋)。 我正在使用的多项式包采用两个双精度数组(Double [] x,Double [] y)。 X与特定股票的“秒后增量”时间值数组有关,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);
    }

根据文档PolynomialFunctionLagrangeForm构造函数采用两个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();
}
...

另请参阅如何将包含整数的ArrayList转换为原始int数组? 用于将ArrayList<T> (其中T是原始类型的包装器)转换为相应的原始数组T[]一些替代方法。

请注意,您的代码中显然也有一个错字:

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

需要是

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

暂无
暂无

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

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