[英]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.