You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am running various tests to be able to upgrade my PortfolioAnalytics simulations to r4.4 with the latest version of PortfolioAnalytics.
I noticed that my current production version that runs on r4.3 with a previous version of PortfolioAnalytics does not show an error.
However on r4.4 with the current GitHub development version of PortfolioAnalytics causes an error when implying a limit on the number of long positions.
I have created a minimal version of the problem using a reprex (shown below) to highlight the problem. The position limit is set a 70. 70 is much larger than 4 so the limit should not have any impact on the selection of random portfolio's.
library(PortfolioAnalytics)
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#> Loading required package: xts
#> Loading required package: foreach
#> Loading required package: PerformanceAnalytics
#>
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#>
#> legend
#> Registered S3 method overwritten by 'PortfolioAnalytics':
#> method from
#> print.constraint ROI
Assets = c("stock1", "stock2", "stock3", "stock4")
Initportfolio <- portfolio.spec(assets=Assets)
# Add the full investment constraint that specifies the weights must sum to 1
Initportfolio <- add.constraint(portfolio=Initportfolio, type = "leverage", min_sum = 0.98, max_sum = 1.02)
if (length(Assets) > 10){
Initportfolio <- add.constraint(portfolio=Initportfolio, type = "box", min = 0, max = 0.2)
} else {
Initportfolio <- add.constraint(portfolio=Initportfolio, type = "box", min = 0, max = 1)
}
NoLimit <- random_portfolios_v2( portfolio = Initportfolio, permutations = 100, rp_method = "sample", eliminate = TRUE)
# Add position limit to portfolio
Initportfolio <- add.constraint(portfolio=Initportfolio, type = "position_limit", max_pos_long = 70)
PositionLimit <- random_portfolios_v2( portfolio = Initportfolio, permutations = 100, rp_method = "sample", eliminate = TRUE)
#> Error in sample.int(length(x), size, replace, prob): cannot take a sample larger than the population when 'replace = FALSE'`
@xinran1228
I made a bugfix. By making sure in rp_transform that max_pos is always <= length(tmp_w) while making sure that the max position constraint is still satisfied.
Correct max_pos if length of weights is less than the maximum nr of positions in the portfolio in rp_transform.
I checked this issue a little deeper and it seems that the position_limit_constraint function has changed dramatically since this commit from 3 months ago: 2817778
If I manually compare the files it can be seen that almost all code is deleted for the position_limit_constraint constraint function and nothing is placed back. This has as consequence that the new code is at the minimum not backwards compatible. It seems however that this is not intentional.
Using notepad++ I manually compared both files and here you can see it clearly:
I am running various tests to be able to upgrade my PortfolioAnalytics simulations to r4.4 with the latest version of PortfolioAnalytics.
I noticed that my current production version that runs on r4.3 with a previous version of PortfolioAnalytics does not show an error.
However on r4.4 with the current GitHub development version of PortfolioAnalytics causes an error when implying a limit on the number of long positions.
I have created a minimal version of the problem using a reprex (shown below) to highlight the problem. The position limit is set a 70. 70 is much larger than 4 so the limit should not have any impact on the selection of random portfolio's.
#Created on 2024-07-18 with reprex v2.1.1
The text was updated successfully, but these errors were encountered: