我正在尝试使用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);
}
}