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

Why R calculations are inconsistent in my virtual machine?

我正在尝试使用 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] ------------------------------------------------------------------------

我真的很困惑,谁能给我一些suggestion/idea,拜托!

编辑

我试图调试我的测试以查看我在哪里犯了错误,但仍然没有任何线索。现在我至少知道我的问题是... look my debug comparison please. And this is the comparison to all 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);
        }

    }

只是想让你知道我的问题是怎么回事。 问题是 TIMEZONE。我不知道为什么,但是 R 或我们用于计算的某些软件包要求时区相同。

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

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