Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TFG_Informatica
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
victarr
TFG_Informatica
Commits
bfa6c714
Commit
bfa6c714
authored
Jun 26, 2022
by
victarr
Browse files
Options
Downloads
Patches
Plain Diff
Replace server.R
parent
6b4ffe23
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
code/shiny-apps/optimizacion/server.R
+79
-60
79 additions, 60 deletions
code/shiny-apps/optimizacion/server.R
with
79 additions
and
60 deletions
code/shiny-apps/optimizacion/server.R
+
79
−
60
View file @
bfa6c714
...
...
@@ -113,6 +113,7 @@ server <- function(input, output, session) {
A
<-
names
(
datos
)
A
<-
gsub
(
"="
,
"."
,
A
)
str_sub
(
A
,
-9
)
<-
""
val_aux
<-
A
A
<-
str_c
(
A
,
collapse
=
" "
)
a
<-
str_c
(
"set A :=\n"
,
A
,
";"
,
sep
=
""
)
b
<-
str_c
(
"param R:\n"
,
A
,
" :=\n"
,
sep
=
""
)
...
...
@@ -227,6 +228,7 @@ server <- function(input, output, session) {
varianza
<-
ampl
$
getVariable
(
"varianza"
)
x
<-
ampl
$
getVariable
(
"x"
)
cartera
<-
x
$
getValues
()
cartera
$
index0
<-
val_aux
cartera
<-
cartera
[
cartera
$
x.val
>
0.001
,]
data_return
<-
data.frame
(
"activ"
=
cartera
$
index0
,
"porc"
=
cartera
$
x.val
)
...
...
@@ -253,6 +255,8 @@ server <- function(input, output, session) {
datos
<-
datos
[
1
:
filas
,]
save
(
datos_evaluacion
,
file
=
"../../RData/data_eval.RData"
)
# Eliminar en la ventana de inicio de la web
rownames
(
datos
)
<-
gsub
(
"\\."
,
"-"
,
unlist
(
gsub
(
"X"
,
""
,
rownames
(
datos
))))
# Valores que utilizamos (retornos).
returns
<-
1
+
Return.calculate
(
datos
)
returns
<-
returns
[
-1
,]
...
...
@@ -261,6 +265,7 @@ server <- function(input, output, session) {
A
<-
names
(
datos
)
A
<-
gsub
(
"="
,
"."
,
A
)
str_sub
(
A
,
-9
)
<-
""
val_aux
<-
A
A
<-
str_c
(
A
,
collapse
=
" "
)
a
<-
str_c
(
"set A :=\n"
,
A
,
";"
,
sep
=
""
)
b
<-
str_c
(
"param R:\n"
,
A
,
" :=\n"
,
sep
=
""
)
...
...
@@ -292,7 +297,6 @@ server <- function(input, output, session) {
p
<-
input
$
discount
h
<-
str_c
(
"param mean {j in A}:= ( sum{i in T}"
,
p
,
"^(card(T) - i) * R[i,j])/( sum {i in T}"
,
p
,
"^(card(T) - i));"
,
collapse
=
""
)
#########
# AMPL
#########
...
...
@@ -378,6 +382,7 @@ server <- function(input, output, session) {
varianza
<-
ampl
$
getVariable
(
"varianza"
)
x
<-
ampl
$
getVariable
(
"x"
)
cartera
<-
x
$
getValues
()
cartera
$
index0
<-
val_aux
cartera
<-
cartera
[
cartera
$
x.val
>
0.001
,]
data_return
<-
data.frame
(
"activ"
=
cartera
$
index0
,
"porc"
=
cartera
$
x.val
)
...
...
@@ -600,6 +605,7 @@ server <- function(input, output, session) {
A
<-
names
(
datos
)
A
<-
gsub
(
"="
,
"."
,
A
)
str_sub
(
A
,
-9
)
<-
""
val_aux
<-
A
A
<-
str_c
(
A
,
collapse
=
" "
)
a
<-
str_c
(
"set A :=\n"
,
A
,
";"
,
sep
=
""
)
b
<-
str_c
(
"param R:\n"
,
A
,
" :=\n"
,
sep
=
""
)
...
...
@@ -737,6 +743,7 @@ server <- function(input, output, session) {
sharpe
<-
c
(
ampl
$
getVariable
(
"sharpe"
))
x
<-
ampl
$
getVariable
(
"x"
)
cartera
<-
x
$
getValues
()
cartera
$
index0
<-
val_aux
cartera
<-
cartera
[
cartera
$
x.val
>
0.001
,]
data_return
<-
data.frame
(
"activ"
=
cartera
$
index0
,
"porc"
=
cartera
$
x.val
)
data_portfolio
<-
list
(
modelo
=
"markowitz2"
,
cartera
=
data_return
,
mu
=
input
$
mu
)
...
...
@@ -801,7 +808,7 @@ server <- function(input, output, session) {
represent
<-
data.frame
(
"Activo"
=
data
$
activ
,
"Porcentaje"
=
round
(
as.numeric
(
data
$
porc
),
2
),
"Inversión"
=
round
(
as.numeric
(
input
$
dinero
*
data
$
porc
),
2
))
}
if
(
input
$
modelos
==
"Modelo de mi
min
ización del riesgo"
){
if
(
input
$
modelos
==
"Modelo de mi
nim
ización del riesgo"
){
data
<-
Data_min
()
represent
<-
data.frame
(
"Activo"
=
data
$
activ
,
"Porcentaje"
=
round
(
as.numeric
(
data
$
porc
),
2
),
"Inversión"
=
round
(
as.numeric
(
input
$
dinero
*
data
$
porc
),
2
))
...
...
@@ -838,13 +845,14 @@ server <- function(input, output, session) {
(
max
(
data
$
medias
)
-
(
input
$
sharpe
-
1
)
*
100
)
/
sqrt
(
max
(
data
$
varianzas
)),
(
data_sharpe
$
medias
-
(
input
$
sharpe
-
1
)
*
100
)
/
sqrt
(
data_sharpe
$
varianzas
)))
}
}
represent
$
Rendimiento
<-
round
(
represent
$
Rendimiento
,
4
)
represent
$
Rendimiento
<-
as.character
(
represent
$
Rendimiento
)
represent
$
Riesgo
<-
round
(
represent
$
Riesgo
,
4
)
represent
$
Riesgo
<-
as.character
(
represent
$
Riesgo
)
represent
$
Ratio.Sharpe
<-
round
(
represent
$
Ratio.Sharpe
,
4
)
represent
$
Ratio.Sharpe
<-
as.character
(
represent
$
Ratio.Sharpe
)
}
represent
}
...
...
@@ -853,6 +861,7 @@ server <- function(input, output, session) {
pdf
(
NULL
)
output
$
scatter
<-
renderPlotly
({
if
(
input
$
modelos
==
"Modelo biobjetivo de Markowitz"
){
if
(
length
(
input
$
empresas
)
>
1
){
data
<-
Data_markowitz
()
$
dataframe
datos
<-
Data_empresas
()
...
...
@@ -909,6 +918,8 @@ server <- function(input, output, session) {
fig
}
}
})
...
...
@@ -1340,6 +1351,10 @@ server <- function(input, output, session) {
data
<-
Data_markowitz2
()
data
$
activ
<-
gsub
(
"\\.X"
,
"=X"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"\\.F"
,
"=F"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"GSPC"
,
"^GSPC"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"NDX"
,
"^NDX"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"IBEX"
,
"^IBEX"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"STOXX50E"
,
"^STOXX50E"
,
data
$
activ
)
valores
<-
NULL
for
(
i
in
1
:
dim
(
data
)[
1
]){
valores
<-
c
(
valores
,
activos
$
activo
[
activos
$
simbolo
==
data
$
activ
[
i
]][
1
])
...
...
@@ -1373,6 +1388,10 @@ server <- function(input, output, session) {
data
<-
Data_markowitz2
()
data
$
activ
<-
gsub
(
"\\.X"
,
"=X"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"\\.F"
,
"=F"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"GSPC"
,
"^GSPC"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"NDX"
,
"^NDX"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"IBEX"
,
"^IBEX"
,
data
$
activ
)
data
$
activ
<-
gsub
(
"STOXX50E"
,
"^STOXX50E"
,
data
$
activ
)
valores
<-
NULL
for
(
i
in
1
:
dim
(
data
)[
1
]){
valores
<-
c
(
valores
,
activos
$
activo
[
activos
$
simbolo
==
data
$
activ
[
i
]][
1
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment