How to solve a function subject to a condition R
up vote
0
down vote
favorite
I'm working on a paper for school.
I have to look for the tangency portfolio using this formula, which I translated in R as follows (basic tangency portfolio formula with matrician algebra.)
nom <- solve(Mat) %*% (ER - RF)
denom <- Ones %*% nom
w <- nom %*% solve(denom)
This formula gives me also negative weights (short selling), and I would like to add a constraint that only allows weights from 0 to 1, with sum 1.
Who can help me with this?
Example:
If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)
), with sum == 1
. (In this case, short selling would be enabled). This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. In the example, I would have the weights something like c(0.18, 0.05, 0.72)
. It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one
r optimization portfolio
add a comment |
up vote
0
down vote
favorite
I'm working on a paper for school.
I have to look for the tangency portfolio using this formula, which I translated in R as follows (basic tangency portfolio formula with matrician algebra.)
nom <- solve(Mat) %*% (ER - RF)
denom <- Ones %*% nom
w <- nom %*% solve(denom)
This formula gives me also negative weights (short selling), and I would like to add a constraint that only allows weights from 0 to 1, with sum 1.
Who can help me with this?
Example:
If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)
), with sum == 1
. (In this case, short selling would be enabled). This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. In the example, I would have the weights something like c(0.18, 0.05, 0.72)
. It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one
r optimization portfolio
Adding a small example and desired output will help you get answers faster.
– Shree
Nov 19 at 23:29
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:44
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on a paper for school.
I have to look for the tangency portfolio using this formula, which I translated in R as follows (basic tangency portfolio formula with matrician algebra.)
nom <- solve(Mat) %*% (ER - RF)
denom <- Ones %*% nom
w <- nom %*% solve(denom)
This formula gives me also negative weights (short selling), and I would like to add a constraint that only allows weights from 0 to 1, with sum 1.
Who can help me with this?
Example:
If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)
), with sum == 1
. (In this case, short selling would be enabled). This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. In the example, I would have the weights something like c(0.18, 0.05, 0.72)
. It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one
r optimization portfolio
I'm working on a paper for school.
I have to look for the tangency portfolio using this formula, which I translated in R as follows (basic tangency portfolio formula with matrician algebra.)
nom <- solve(Mat) %*% (ER - RF)
denom <- Ones %*% nom
w <- nom %*% solve(denom)
This formula gives me also negative weights (short selling), and I would like to add a constraint that only allows weights from 0 to 1, with sum 1.
Who can help me with this?
Example:
If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)
), with sum == 1
. (In this case, short selling would be enabled). This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. In the example, I would have the weights something like c(0.18, 0.05, 0.72)
. It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one
r optimization portfolio
r optimization portfolio
edited Nov 20 at 14:21
Shree
3,1361323
3,1361323
asked Nov 19 at 23:23
Paolo Montemurro
1
1
Adding a small example and desired output will help you get answers faster.
– Shree
Nov 19 at 23:29
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:44
add a comment |
Adding a small example and desired output will help you get answers faster.
– Shree
Nov 19 at 23:29
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:44
Adding a small example and desired output will help you get answers faster.
– Shree
Nov 19 at 23:29
Adding a small example and desired output will help you get answers faster.
– Shree
Nov 19 at 23:29
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:44
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:44
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
as mentioned by @Shree, an example would help understanding more precisely what you're after.
From what I understood, you want w
to be bounded between [0,1]? From the top of my head you could either shift and scale w
## Shifting
shift_w <- (w-min(w))
## Scaling
shift_w/max(shift_w)
Or, more brutally, replace the values <0 or >1 by a given value or function
## What to replace the value with when negative
replace_negative <- 0
## What to replace the value with when superior to 1
replace_one <- 1
## Making sure w is bounded between 0 and 1
ifelse(ifelse(w < 0, replace_negative, w) > 1, replace_one w)
Note that replace_negative
and replace_one
can also be a function f(w)
if you want something more complex.
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
as mentioned by @Shree, an example would help understanding more precisely what you're after.
From what I understood, you want w
to be bounded between [0,1]? From the top of my head you could either shift and scale w
## Shifting
shift_w <- (w-min(w))
## Scaling
shift_w/max(shift_w)
Or, more brutally, replace the values <0 or >1 by a given value or function
## What to replace the value with when negative
replace_negative <- 0
## What to replace the value with when superior to 1
replace_one <- 1
## Making sure w is bounded between 0 and 1
ifelse(ifelse(w < 0, replace_negative, w) > 1, replace_one w)
Note that replace_negative
and replace_one
can also be a function f(w)
if you want something more complex.
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
add a comment |
up vote
0
down vote
as mentioned by @Shree, an example would help understanding more precisely what you're after.
From what I understood, you want w
to be bounded between [0,1]? From the top of my head you could either shift and scale w
## Shifting
shift_w <- (w-min(w))
## Scaling
shift_w/max(shift_w)
Or, more brutally, replace the values <0 or >1 by a given value or function
## What to replace the value with when negative
replace_negative <- 0
## What to replace the value with when superior to 1
replace_one <- 1
## Making sure w is bounded between 0 and 1
ifelse(ifelse(w < 0, replace_negative, w) > 1, replace_one w)
Note that replace_negative
and replace_one
can also be a function f(w)
if you want something more complex.
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
add a comment |
up vote
0
down vote
up vote
0
down vote
as mentioned by @Shree, an example would help understanding more precisely what you're after.
From what I understood, you want w
to be bounded between [0,1]? From the top of my head you could either shift and scale w
## Shifting
shift_w <- (w-min(w))
## Scaling
shift_w/max(shift_w)
Or, more brutally, replace the values <0 or >1 by a given value or function
## What to replace the value with when negative
replace_negative <- 0
## What to replace the value with when superior to 1
replace_one <- 1
## Making sure w is bounded between 0 and 1
ifelse(ifelse(w < 0, replace_negative, w) > 1, replace_one w)
Note that replace_negative
and replace_one
can also be a function f(w)
if you want something more complex.
as mentioned by @Shree, an example would help understanding more precisely what you're after.
From what I understood, you want w
to be bounded between [0,1]? From the top of my head you could either shift and scale w
## Shifting
shift_w <- (w-min(w))
## Scaling
shift_w/max(shift_w)
Or, more brutally, replace the values <0 or >1 by a given value or function
## What to replace the value with when negative
replace_negative <- 0
## What to replace the value with when superior to 1
replace_one <- 1
## Making sure w is bounded between 0 and 1
ifelse(ifelse(w < 0, replace_negative, w) > 1, replace_one w)
Note that replace_negative
and replace_one
can also be a function f(w)
if you want something more complex.
answered Nov 20 at 3:44
Thomas Guillerme
6731616
6731616
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
add a comment |
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384080%2fhow-to-solve-a-function-subject-to-a-condition-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Adding a small example and desired output will help you get answers faster.
– Shree
Nov 19 at 23:29
Example: If I run the code as now, let's say with 3 assets, I will get also negative weights for some assets (ex c(0.20, -0.40, 0.80)), with sum ==1. (In this case, short selling would be enabled).This is the tangency portfolio that maximizes the sharpe ratio, given the expected return and variance. What I'd love to have is the tangency portfolio without short selling allowed. in the example, I would have the weights something like c(0.18, 0.05, 0.72). It will be incorrect to replace the negative numbers with 0 and also the >1 with 1, as the sum of all the weights should be one.
– Paolo Montemurro
Nov 20 at 9:44