gpt4 book ai didi

java - 为什么我的虚拟机中的 R 计算不一致?

转载 作者:行者123 更新时间:2023-12-04 18:34:17 25 4
gpt4 key购买 nike

我正在尝试使用 R 构建一个新的虚拟机以及以下作为 R server 运行的包根据我的计算。

    #this is how I install my R-packages
function install_packages(){
folder='dir.create(Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)'
packages='install.packages(c("Rserve","fArma","fGarch","tseries","MASS","lattice","gtools","gmodels","gplots","HiddenMarkov", "xts", "PerformanceAnalytics"), Sys.getenv("R_LIBS_USER"), repos = "http://cran.rstudio.com")'

echo "$folder" >> ./install_packages.R
echo "$packages" >> ./install_packages.R

sudo /usr/bin/R CMD BATCH install_packages.R
rm -f ./install_packages.R
}

如果我从主机向这个新虚拟机调用电话(使用 mvn clean package ),我的计算中会出现一个奇怪的错误:
Running com.company.documentengine.statistics.JensensAlphaTest
Oct 28, 2015 2:17:45 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB
INFO: PostgreSQL JDBC Driver Registered
Oct 28, 2015 2:17:45 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB
INFO: test Database connection confirmed for user postgres
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 17.971 sec <<< FAILURE! - in com.company.documentengine.statistics.JensensAlphaTest
testCalculate(com.company.documentengine.statistics.JensensAlphaTest) Time elapsed: 8.821 sec <<< FAILURE!
java.lang.AssertionError: Calculation wrong. expected:<0.039801296645998546> but was:<NaN>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:553)
at com.company.documentengine.statistics.JensensAlphaTest.testCalculate(JensensAlphaTest.java:40)

现在,如果我进行相同的调用,但从新虚拟机到我的主机(也安装了所有这些软件包),一切正常。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.company.documentengine.statistics.JensensAlphaTest
Oct 28, 2015 1:23:13 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB
INFO: PostgreSQL JDBC Driver Registered
Oct 28, 2015 1:23:13 PM com.company.documentengine.toolbox.util.DatabaseConnection connectToDB
INFO: test Database connection confirmed for user postgres
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 17.465 sec - in com.company.documentengine.statistics.JensensAlphaTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.423s
[INFO] Finished at: Wed Oct 28 13:23:20 UTC 2015
[INFO] Final Memory: 18M/362M
[INFO] ------------------------------------------------------------------------

我真的对此感到困惑,任何人都可以给我一些建议/想法,拜托!

编辑

我试图调试我的测试以查看我在哪里犯了错误,但仍然没有任何线索。现在我至少知道我的问题在于... look my debug comparison请。这是与所有 my packages used in both cases 的比较.

Java 代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class})
@ActiveProfiles(profiles = {"test"})
public class JensensAlphaTest {

@Autowired
private TestSeriesManager testSeriesManager;

@Test
public void testCalculate() throws Exception {
PriceSeries<PriceSeriesDatum> dax = testSeriesManager.getDax();
PriceSeries<PriceSeriesDatum> sDax = testSeriesManager.getSDax();
InterestRateSeries<InterestRateDatum> euribor = testSeriesManager.getEuribor();

LocalDate asOfDate = LocalDate.of(2014, 10, 1);
JensensAlpha jensensAlpha = new JensensAlpha(dax, sDax, euribor, asOfDate);

double eps = 1e-15;
/* here is the inconsistent part */
double actualValue = jensensAlpha.calculate(Period.SINCE_INCEPTION, ReturnsType.DAILY_DISCRETE);
double expectedValue = 0.039801296645998546;
assertEquals("Calculation wrong.", expectedValue, actualValue, eps);
}

}

这是调用的方法:

public double calculate(Period period, ReturnsType returnsType) {
NavigableMap<LocalDate, Double> returnSeries = returnsType.getReturnSeries(series);
NavigableMap<LocalDate, Double> returnBenchmark = returnsType.getReturnSeries(benchmark);
NavigableMap<LocalDate, Double> returnRiskFree = returnsType.getReturnSeries(riskFree);

LocalDate startDate = period.getStartDate(returnSeries);

NavigableMap<LocalDate, Double> cutReturnSeries = StatisticsUtils.getMapSince(startDate, returnSeries);

NavigableMap<LocalDate, Double> cutBenchmarkReturnSeries;
NavigableMap<LocalDate, Double> cutRiskFreeReturnSeries;
try {
cutBenchmarkReturnSeries = StatisticsUtils.getMapSince(startDate, returnBenchmark);
cutRiskFreeReturnSeries = StatisticsUtils.getMapSince(startDate, returnRiskFree);
} catch (IllegalArgumentException e) {
throw new NotEnoughDataException(
"This error can occur when the price series is short (only a few returns), so the benchmark is not"
+ " updated for the taken first date of the series.", e);
}

REXPS4[] inputClasses =
{RexpParser.createREXPS4Class(cutReturnSeries), RexpParser.createREXPS4Class(cutBenchmarkReturnSeries),
RexpParser.createREXPS4Class(cutRiskFreeReturnSeries)};
RScript script = RScript.fromFileName("JensensAlpha.R");
REXPS4 resultClass = script.execute(inputClasses);

try {
return resultClass.getAttribute("value").asDouble();
} catch (REXPMismatchException e) {
throw new RScriptException("Exception while getting results from the R script.", e);
}

}

和执行方法:
@Override
public REXPS4 execute(REXPS4[] inputClasses) {

RConnection c = RConnectionSingleton.INSTANCE.getRConnection();

try {

int inputClassNumber = 1;
for (REXPS4 inputClass : inputClasses) {

c.assign("inputClass" + inputClassNumber, inputClass);
inputClassNumber++;

}

c.eval(code);
/* the resultClass is wrong only when I connect to my vm */
return (REXPS4) c.get("resultClass", null, true);

} catch (REngineException e) {
throw new ScriptExecutionException("Exception while trying to execute the RScript.", e);
}

}

最佳答案

只是为了让您知道我的问题发生了什么。
问题是 时区 .我不知道为什么,但是 R或者我们用于计算的某些包要求时区相同。

我位于德国(CET 时区为 +1 UTC),我将虚拟机设置为使用 UTC,因此出现了问题。哦,伙计,我真的很高兴解决这个问题(连续 3 天解决这个问题!)但现在一切都很好!非常感谢我的同事 @Ralf给小费!

其他相关问题:1 , 2 , 3 .
我希望这可以帮助别人! :)

关于java - 为什么我的虚拟机中的 R 计算不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33393040/

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