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










share|improve this question
























  • 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

















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










share|improve this question
























  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'