gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:34:25 25 4
gpt4 key购买 nike

首先感谢您提前提供的帮助。

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

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

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

我的问题听起来相当简单,应该很容易克服(我可能是个大白痴)。我使用的多项式包接受两个 double 组(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);
}

最佳答案

根据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()]);

关于CSV 多项式的 Java.Lang.Double[] 到 Double[] 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35605001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com