Eigen Zero() feature fails to compile with semi-dynamic matrices - eigen3

It sounds like Zero() cannot be called on a semi-dynamic matrix.
May I ask you some explanation about the compilation error below :
Matrix<double, 3, Dynamic> M = Matrix<double, 3, Dynamic>::Zero(3);
In instantiation of ‘static const Eigen::CwiseNullaryOp<CustomNullaryOp, typename Eigen::internal::conditional<Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::MatrixXpr>::value, Eigen::Matrix<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime>, Eigen::Array<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime> >::type> Eigen::DenseBase<Derived>::NullaryExpr(Eigen::Index, const CustomNullaryOp&) [with CustomNullaryOp = Eigen::internal::scalar_constant_op<double>; Derived = Eigen::Matrix<double, 15, -1>; typename Eigen::internal::conditional<Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::MatrixXpr>::value, Eigen::Matrix<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime>, Eigen::Array<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime> >::type = Eigen::Matrix<double, 15, -1>; Eigen::Index = long int]’:
/usr/local/include/Eigen/src/Core/CwiseNullaryOp.h:213:41: required from ‘static const ConstantReturnType Eigen::DenseBase<Derived>::Constant(Eigen::Index, const Scalar&) [with Derived = Eigen::Matrix<double, 15, -1>; Eigen::DenseBase<Derived>::ConstantReturnType = Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, 15, -1> >; typename Eigen::internal::conditional<Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::MatrixXpr>::value, Eigen::Matrix<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime>, Eigen::Array<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime> >::type = Eigen::Matrix<double, 15, -1>; typename Eigen::internal::traits<T>::Scalar = double; Eigen::Index = long int; Eigen::DenseBase<Derived>::Scalar = double]’
/usr/local/include/Eigen/src/Core/CwiseNullaryOp.h:472:18: required from ‘static const ConstantReturnType Eigen::DenseBase<Derived>::Zero(Eigen::Index) [with Derived = Eigen::Matrix<double, 15, -1>; Eigen::DenseBase<Derived>::ConstantReturnType = Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, 15, -1> >; typename Eigen::internal::conditional<Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::MatrixXpr>::value, Eigen::Matrix<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime>, Eigen::Array<typename Eigen::internal::traits<T>::Scalar, Eigen::internal::traits<T>::RowsAtCompileTime, Eigen::internal::traits<T>::ColsAtCompileTime, (AutoAlign | ((Eigen::internal::traits<T>::Flags & Eigen::RowMajorBit) ? RowMajor : ColMajor)), Eigen::internal::traits<T>::MaxRowsAtCompileTime, Eigen::internal::traits<T>::MaxColsAtCompileTime> >::type = Eigen::Matrix<double, 15, -1>; typename Eigen::internal::traits<T>::Scalar = double; Eigen::Index = long int]’
../src/COpdCtrl.cpp:77:49: required from here
/usr/local/include/Eigen/src/Core/CwiseNullaryOp.h:147:3: error: static assertion failed: YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
Thanks for helping
Sylvain

If one or both sizes of an Eigen::Matrix are Dynamic you always need to pass both sizes (unless the fixed size is 1).
Matrix<double, 3, Dynamic> M = Matrix<double, 3, Dynamic>::Zero(3, 100); // 3x100 zeros
If both sizes are fixed, you can pass both sizes or none (or in fact one size, if the other size is 1):
Matrix<double, 3, 2>::Zero();
Matrix<double, 3, 2>::Zero(3,2); // optionally pass both sizes again
Matrix<double, 3, 1>::Zero(3); // possible as well
Matrix<double, 1, 3>::Zero(3); // also possible
N.B., there is a predefined typedef Matrix3Xd for Matrix<double, 3, Dynamic>.

Related

Historical Market Screener: Processing Array Data

I have a question with respect to arrays & tables on PineScript, please. To give an overview of what I am intending to do, I want to create a screener that will go through a set of symbols and output the annual performance at a historical date.
The idea behind this concept is that I would be able to identify top performing symbols from a historical perspective. I have researched online, and I was not able to find such a tool.
The reason why I would need a tool like this, is so that I can back-test my strategy on historical top-performers, considering that I would be rebalancing my portfolio at a fixed interval e.g. once per quarter.
So my questions with regards to the code below are basically 2 –
How can I filter the arrays to output only values above 10% yearly performance?
How do I sort the table so that the top performers come to the top of the list?
I have shortened the code for this post. However, this screener can go through a max of 40 symbols as you all know. My plan is to run this on basically all the major and minor FX pairs and for stocks, I will pick a balanced portfolio with stocks from each of the 11 S&P sectors. As liquidity rotates through each of sectors, this should keep me running my strategy on stocks with positive alpha potential.
//#version=5
indicator('Annual Performance Historical Screener', overlay=true)
////////////
// INPUTS //
//Year Input
input_year = input(2022, "Year")
/////////////
// SYMBOLS //
u01 = input.bool(true, title = "", group = 'Symbols', inline = 's01')
u02 = input.bool(true, title = "", group = 'Symbols', inline = 's02')
u03 = input.bool(true, title = "", group = 'Symbols', inline = 's03')
u04 = input.bool(true, title = "", group = 'Symbols', inline = 's04')
u05 = input.bool(true, title = "", group = 'Symbols', inline = 's05')
s01 = input.symbol('XRPUSDT', group = 'Symbols', inline = 's01')
s02 = input.symbol('BTCUSDT', group = 'Symbols', inline = 's02')
s03 = input.symbol('DOGEUSDT', group = 'Symbols', inline = 's03')
s04 = input.symbol('BNBUSDT', group = 'Symbols', inline = 's04')
s05 = input.symbol('ETHUSDT', group = 'Symbols', inline = 's05')
//////////////////
// CALCULATIONS //
// Get only symbol
only_symbol(s) =>
array.get(str.split(s, ":"), 1)
//price at the input
price_input_year = timestamp(input_year,10,12)
period = time >= price_input_year and time[1] < price_input_year
periodPrice = ta.valuewhen(period, close, 0)
//price at 1 year preceeding input
price_prev_year = timestamp(input_year-1,10,12)
period_prev = time >= price_prev_year and time[1] < price_prev_year
periodPrice_prev = ta.valuewhen(period_prev, close, 0)
screener_func() =>
//Yearly performance from input date
annual_perf=(periodPrice-periodPrice_prev)*100/periodPrice
[math.round_to_mintick(close), annual_perf]
// Security call
[cl01, ap01] = request.security(s01, timeframe.period, screener_func())
[cl02, ap02] = request.security(s02, timeframe.period, screener_func())
[cl03, ap03] = request.security(s03, timeframe.period, screener_func())
[cl04, ap04] = request.security(s04, timeframe.period, screener_func())
[cl05, ap05] = request.security(s05, timeframe.period, screener_func())
////////////
// ARRAYS //
s_arr = array.new_string(0)
u_arr = array.new_bool(0)
cl_arr = array.new_float(0)
ap_arr = array.new_float(0)
// Add Symbols
array.push(s_arr, only_symbol(s01))
array.push(s_arr, only_symbol(s02))
array.push(s_arr, only_symbol(s03))
array.push(s_arr, only_symbol(s04))
array.push(s_arr, only_symbol(s05))
///////////
// FLAGS //
array.push(u_arr, u01)
array.push(u_arr, u02)
array.push(u_arr, u03)
array.push(u_arr, u04)
array.push(u_arr, u05)
///////////
// CLOSE //
array.push(cl_arr, cl01)
array.push(cl_arr, cl02)
array.push(cl_arr, cl03)
array.push(cl_arr, cl04)
array.push(cl_arr, cl05)
/////////
// Annual performance //
array.push(ap_arr, ap01)
array.push(ap_arr, ap02)
array.push(ap_arr, ap03)
array.push(ap_arr, ap04)
array.push(ap_arr, ap05)
///////////
// PLOTS //
var tbl = table.new(position.top_right, 6, 41, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.white, 100))
if barstate.islast
table.cell(tbl, 0, 0, 'Symbol', text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.small)
table.cell(tbl, 1, 0, 'Price', text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.small)
table.cell(tbl, 2, 0, 'Annual Performance', text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.small)
for i = 0 to 4
if array.get(u_arr, i)
ap_col = array.get(ap_arr, i) >= 10 ? color.green : #aaaaaa
table.cell(tbl, 0, i + 1, array.get(s_arr, i), text_halign = text.align_left, bgcolor = color.gray, text_color = color.white, text_size = size.small)
table.cell(tbl, 1, i + 1, str.tostring(array.get(cl_arr, i)), text_halign = text.align_center, bgcolor = #aaaaaa, text_color = color.white, text_size = size.small)
table.cell(tbl, 2, i + 1, str.tostring(array.get(ap_arr, i), "#.##"), text_halign = text.align_center, bgcolor = ap_col, text_color = color.white, text_size = size.small)

Import many files .txt in SAS

I built a code to import multiple data simultaneously into SAS, but I want to improve it does anyone have any suggestions?
filename indata pipe 'dir E:\Desafio_SAS\Dados /B';
data file_list;
length arquivos$20.;
infile indata truncover ;
input arquivos $20.;
call symput('num_files',_n_);
arquivos=compress(arquivos,',.txt');
run;
CRIANDO UMA MACRO POR PROC SQL PARA GUARDAR O NOME DOS ARQUIVOS
proc sql;
select arquivos into :lista separated by ' ' from file_list;
quit;
%let &lista;
%macro importar(arquivo=);
filename data "E:\Desafio_SAS\Dados\&arquivo..txt";
data &arquivo;
infile data dlm=" " missover dsd firstobs=2;
input v0 (v1 - v8) ($);
format v0 F16.;
run;
%mend importar;
%macro fileout;
%do i=1 %to &num_files;
%importar(arquivo=df&i);
data df&i;
set var_names df&i;
run;
%end;
%mend fileout;
%fileout;
%macro excluiv0;
%do i=1 %to &num_files;
data _null_;
data df&i(drop = v0);
set df&i;
run;
%end;
run;
%mend excluiv0;
%excluiv0;
It's just part of the code.
You can use a wildcard in the infile file-specification. As long as all the files meeting the wildcard are the same layout, you can use a single input to read all the files.
Example
* create three text files having same fields;
data _null_;
file '%temp%\1.txt';
put '1 2 3 abc';
put '3 4 5 def';
file '%temp%\2.txt';
put '6 7 8 ghi';
put '9 10 11 jkl';
file '%temp%\3.txt';
put '12 13 14 xyz';
put '15 16 17 tuv';
run;
* read all three using wildcard in infile. Save name of file whence
* data cometh frometh;
data want;
length _filename_ $250;
infile '%temp%\?.txt' filename=_filename_;
length source $250;
length a b c 8 s $20;
source = _filename_;
input a b c s;
run;
Wildcards are
?, 0 or 1 of any character
*, any number of any character
t1 <- ttheme_default(core=list(
fg_params=list(fontface=c("bold.italic")),
bg_params = list(fill=c("green", "grey90","blue","red"))))
grid.arrange(g1,
tableGrob(iris[1:5, 1:4], theme = t1,, rows=NULL),
g1, g1, nrow = 2)
---
title: "Column Orientation"
output: flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(ggplot2);library(knitr);library(kableExtra)
library(flexdashboard);library(gridExtra);library(grid)
```
<style>
.colored {
background-color: #002080;}
</style>
Column{data-width=200}
-------------------------------------
### Chart 1{.colored}
```{r}
gauge(10, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(50, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(20, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(15, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(5 , min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
```
Column
-------------------------------------
### Chart 2
```{r, include=FALSE}
tt1 <- ttheme_default()
tt2 <- ttheme_minimal()
tt3 <- ttheme_minimal(
core=list(bg_params = list(fill = blues9[1:4], col=NA),
fg_params=list(fontface=3)),
colhead=list(fg_params=list(col="navyblue", fontface=4L)),
rowhead=list(fg_params=list(col="orange", fontface=3L)))
tab <- grid.arrange(tableGrob(iris[c(1:4,1:2), c(1:3,1:2)], theme=tt3), nrow=1)
graf <- ggplot(data=mtcars, aes(x=drat, y=disp, group=vs)) +
geom_line() + ylab("") +
geom_point()
gg.gauge <- function(pos,breaks=c(0,10,25,100)) {
get.poly <- function(a,b,r1=0.5,r2=1.0) {
th.start <- pi*(1-a/100)
th.end <- pi*(1-b/100)
th <- seq(th.start,th.end,length=1000)
x <- c(r1*cos(th),rev(r2*cos(th)))
y <- c(r1*sin(th),rev(r2*sin(th)))
return(data.frame(x,y))
}
ggplot()+
geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill="forestgreen", colour = "white", size = 1.2, alpha = 0.7) +
geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill="gold", colour = "white", size = 1.2, alpha = 0.7) +
geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill="red", colour = "white", size = 1.2, alpha = 0.7) +
geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y), colour = "white")+
annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
coord_fixed()+
theme_bw()+
theme(axis.text=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank())
}
gg1 <- gg.gauge(2,breaks=c(0,10,25,100))
gg2 <- gg.gauge(5,breaks=c(0,10,25,100))
gg3 <- gg.gauge(7,breaks=c(0,10,25,100))
```
```{r, fig.width=9.5, fig.height=7}
for (i in 1:5){
title1=textGrob("Test title TESTE", gp=gpar(fontface="bold", fontsize = 15))
lay <- rbind(c(3,3,4,4,5,5),
c(1,1,1,1,1,1),
c(1,1,1,1,1,1),
c(2,2,2,2,2,2),
c(2,2,2,2,2,2))
grid.arrange(graf, tab, gg1, gg2, gg3, top=title1,
layout_matrix= lay)
grid.rect(width = 1, height = 1, gp = gpar(lwd = 2, col = "black", fill = NA))
cat("\n")
}
```
---
title: "BRADESCO"
output:
flexdashboard::flex_dashboard:
orientation: rows
---
```{r setup, include=FALSE}
library(ggplot2);library(knitr);library(kableExtra)
library(flexdashboard);
library(gridExtra);library(grid)
```
Geral {data-icon="fa-signal"}
=====================================
### Chat 1
```{r}
p1 <- qplot(mpg, wt, data = mtcars, colour = cyl)
p2 <- qplot(mpg, data = mtcars)
p3 <- qplot(mpg, data = mtcars, geom = "dotplot")
lay <- rbind(c(1,1,1,2,2,2),
c(3,3,3,3,3,3))
grid.arrange(p2, p3, p1, nrow = 2, layout_matrix= lay)
```
### Table 1
```{r}
kable(mtcars[1:10, c(1:6,1:4)], caption = "Group Rows") %>%
kable_styling("striped", full_width = F) %>%
group_rows("Group 1", 4, 7) %>%
group_rows("Group 2", 8, 10)
```
Por segmento {data-icon="fa-signal"}
=====================================
<style>
.colored {
background-color: #002080;}
</style>
Row{data-height=200}
-------------------------------------
### Chart 1{.colored}
```{r, fig.width=55}
dat = data.frame(count=rep(c(10, 60, 30),10), category=rep(c("A", "B", "C"),10), fator=c(1,2,3,4,5))
# Add addition columns, needed for drawing with geom_rect.
dat$fraction = dat$count / sum(dat$count)
dat = dat[order(dat$fraction), ]
dat$ymax = cumsum(dat$fraction)
dat$ymin = c(0, head(dat$ymax, n=-1))
p <- ggplot(dat, aes(x=2, y=fraction, fill=category))+
geom_bar(stat="identity", colour = "white", size = 2) +
xlim(0, 2.5) +
scale_fill_manual(values=c("#002080", "#002080", "white")) +
coord_polar(theta = "y")+
labs(x=NULL, y=NULL)+ guides(fill=FALSE) +
ylab("fsfagafs") + facet_wrap(~ fator,nrow = 1) +
annotate("text", x = 0, y = 0, label = "WW", size = 20, colour = "white") +
theme(
plot.margin = margin(-1.1, 3.6, -1.1, 3.6, "cm"),
panel.spacing = unit(30, "lines"),
axis.ticks=element_blank(),
axis.text=element_blank(),
axis.title=element_blank(),
panel.grid=element_blank(),
plot.background = element_rect(fill = "#002080", colour="#002080"),
panel.background = element_rect(fill = "#002080", colour="#002080"),
strip.background = element_blank(),
strip.text.x = element_blank())
p
```
Row
-------------------------------------
### Chart 2 {data-wight=900}
```{r, include=FALSE}
tt1 <- ttheme_default()
tt2 <- ttheme_minimal()
tt3 <- ttheme_minimal(
core=list(bg_params = list(fill = blues9[1:4], col=NA),
fg_params=list(fontface=3)),
colhead=list(fg_params=list(col="navyblue", fontface=4L)),
rowhead=list(fg_params=list(col="orange", fontface=3L)))
tab <- grid.arrange(tableGrob(iris[c(1:4,1:2), c(1:3,1:2)], theme=tt3), nrow=1)
graf <- ggplot(data=mtcars, aes(x=drat, y=disp, group=vs)) +
geom_line() + ylab("") +
geom_point()
gg.gauge <- function(pos,breaks=c(0,10,25,100)) {
get.poly <- function(a,b,r1=0.5,r2=1.0) {
th.start <- pi*(1-a/100)
th.end <- pi*(1-b/100)
th <- seq(th.start,th.end,length=1000)
x <- c(r1*cos(th),rev(r2*cos(th)))
y <- c(r1*sin(th),rev(r2*sin(th)))
return(data.frame(x,y))
}
ggplot()+
geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill="forestgreen", colour = "white", size = 1.2, alpha = 0.7) +
geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill="gold", colour = "white", size = 1.2, alpha = 0.7) +
geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill="red", colour = "white", size = 1.2, alpha = 0.7) +
geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y), colour = "white")+
annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
coord_fixed()+
theme_bw()+
theme(axis.text=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank())
}
gg1 <- gg.gauge(2,breaks=c(0,10,25,100))
gg2 <- gg.gauge(5,breaks=c(0,10,25,100))
gg3 <- gg.gauge(7,breaks=c(0,10,25,100))
```
```{r, fig.width=7.2, fig.height=7}
for (i in 1:5){
title1=textGrob("Test title TESTE", gp=gpar(fontface="bold", fontsize = 15))
lay <- rbind(c(3,3,4,4,5,5),
c(1,1,1,1,1,1),
c(1,1,1,1,1,1),
c(2,2,2,2,2,2),
c(2,2,2,2,2,2))
grid.arrange(graf, tab, gg1, gg2, gg3, top=title1,
layout_matrix= lay)
grid.rect(width = 1, height = 1, gp = gpar(lwd = 2, col = "black", fill = NA))
cat("\n")
}
```
### Chart 2
```{r}
mydata = data.frame(x1 = c(1,2,3),
x2 = c(9,8,7),
label = c("description a",
"description b",
"description c"))
ht = 5
wd1 = 5
wd2 = 12
gap = 0.1
nc = ncol(mydata)
nr = nrow(mydata)
x = rep(c(seq(0,(nc-2)*(wd1+gap), wd1+gap), (nc-2)*(wd1+gap) + gap + 0.5*(wd2+wd1)), nr)
y = rep(seq(0,(nr-1)*(ht+gap), ht+gap), nc) %>% sort()
h = rep(ht, nr * nc)
w = rep(c(rep(wd1, nc-1), wd2), nr)
info = as.vector(t(as.matrix(mydata[nr:1,])))
df = data.frame(x = x, y = y, h = h, w = w, info = info)
ggplot(df, aes(x, y, height = h, width = w, label = info)) +
geom_tile() +
geom_text(color = "white", fontface = "bold") +
coord_fixed() +
scale_fill_brewer(type = "qual",palette = "Dark2") +
theme_void() +
guides(fill = F)
```
teste

create my nribin code for logistf ,does it really work?

i need to creat my own nribin code , it can be used for logistf package outcome, i maybe work,please give me some advise!!!
i change z.std = mdl.std$x[,-1] to z.std = mdl.std$x
and cancle: link = mdl.std$family[[2]] and family=binomial(link),
the whole code is:
nribin_LTY <-
function (event=NULL, mdl.std=NULL, mdl.new=NULL, z.std=NULL, z.new=NULL, p.std=NULL, p.new=NULL,
updown='category', cut=NULL, link='logit', niter=1000, alpha=0.05, msg=TRUE) {
##
## type of calculation
flag.mdl = !is.null(mdl.std) && !is.null(mdl.new)
flag.prd = !is.null(z.std) && !is.null(z.new)
flag.rsk = !is.null(p.std) && !is.null(p.new)
##
## check standard & new model
if (flag.mdl) {
if (is.null(event)) event = as.numeric(mdl.std$y)
if (is.null(mdl.std$x) || is.null(mdl.new$x))
stop("\n\nmodel object does not contain predictors. pls set x=TRUE for model calculation.\n\n")
z.std = mdl.std$x
z.new = mdl.new$x
mdl.std = glm(event ~ ., data=as.data.frame(cbind(event, z.std)))
mdl.new = glm(event ~ ., data=as.data.frame(cbind(event, z.new)))
} else if (flag.prd) {
mdl.std = glm(event ~ ., data=as.data.frame(cbind(event, z.std)))
mdl.new = glm(event ~ ., data=as.data.frame(cbind(event, z.new)))***
message("\nSTANDARD prediction model:")
print(summary(mdl.std)$coef)
message("\nNEW prediction model:")
print(summary(mdl.new)$coef)
} else if (!flag.mdl && !flag.prd && !flag.rsk) {
stop("\n\neither one of 'event, z.std, z.new', 'event, p.std, p.new', and 'mdl.std, mdl.new' should be specified.\n\n")
}
if (is.null(cut))
stop("\n\n'cut' is empty")
objs = list(mdl.std, mdl.new, z.std, z.new, p.std, p.new)
##
## DH & DL
wk = get.uppdwn.bin(event, objs, flag.mdl, flag.prd, flag.rsk, updown, cut, link, msg=msg)
upp = wk[[1]]
dwn = wk[[2]]
ret = list(mdl.std=mdl.std, mdl.new=mdl.new, p.std=wk[[3]], p.new=wk[[4]], up=upp, down=dwn, rtab=wk[[5]], rtab.case=wk[[6]], rtab.ctrl=wk[[7]])
##
## point estimation
message("\nNRI estimation:")
est = nribin.count.main(event, upp, dwn)
message("Point estimates:")
result = data.frame(est)
names(result) = 'Estimate'
row.names(result) = c('NRI','NRI+','NRI-','Pr(Up|Case)','Pr(Down|Case)','Pr(Down|Ctrl)','Pr(Up|Ctrl)')
print(result)
##
## interval estimation
if (niter > 0) {
message("\nNow in bootstrap..")
ci = rep(NA, 14)
N = length(event)
samp = matrix(NA, niter, 7)
colnames(samp) = c('NRI','NRI+','NRI-','Pr(Up|Case)','Pr(Down|Case)','Pr(Down|Ctrl)','Pr(Up|Ctrl)')
for (b in 1:niter) {
f = as.integer(runif(N, 0, N)) + 1
objs = list(mdl.std, mdl.new, z.std[f,], z.new[f,], p.std[f], p.new[f])
wk = get.uppdwn.bin(event[f], objs, flag.mdl, flag.prd, flag.rsk, updown, cut, link, msg=FALSE)
upp = wk[[1]]
dwn = wk[[2]]
samp[b,] = nribin.count.main(event[f], upp, dwn)
}
ret = c(ret, list(bootstrapsample=samp))
ci = as.numeric(apply(samp, 2, quantile, c(alpha/2, 1-alpha/2), na.rm=TRUE, type=2))
se = as.numeric(apply(samp, 2, sd))
message("\nPoint & Interval estimates:")
result = as.data.frame(cbind(est, se, matrix(ci, ncol=2, byrow=TRUE)))
names(result) = c('Estimate', 'Std.Error', 'Lower', 'Upper')
row.names(result) = c('NRI','NRI+','NRI-','Pr(Up|Case)','Pr(Down|Case)','Pr(Down|Ctrl)','Pr(Up|Ctrl)')
print(result)
}
invisible(c(list(nri=result), ret))
}
It is need to add variable matrix to logistf outcome,such as:
x.std=as.matrix(thromb2[,c(8,9,10,14,21,22,24,25,26)])
mstd = logistf(formula=fml.std, firth = FALSE,
data = thromb2, x=TRUE)
mstd =backward(mstd)
mstd$x <- x.std

How to reduce array's numeric values, skipping strings

Using Google Sheets' Script Editor, I need to calculate the average of each column of a table such as this one:
+---------+---------+---------+---------+
| | User 1 | User 2 | User 3 |
+---------+---------+---------+---------+
| | 20/1/17 | 21/1/17 | 22/1/17 |
+---------+---------+---------+---------+
| Movie 1 | 6 | 2 | 0 |
+---------+---------+---------+---------+
| Movie 2 | 2 | 1 | 5 |
+---------+---------+---------+---------+
| Movie 3 | 3 | 1 | 2 |
+---------+---------+---------+---------+
I want the result to be:
+---------+---------+---------+
| User 1 | User 2 | User 3 |
+---------+---------+---------+
| 3.6 | 1.3 | 2.3 |
+---------+---------+---------+
I have this code, which works if I use it on a table composed by numbers only. The problem is my table has strings in it, so the .reduce returns a #NUM! error (Result was not a number).
var sum = array.reduce(function(r, e, i) {
e.forEach(function(a, j) {
r[j] = (r[j] || 0) + a
})
if (i == array.length - 1) r = r.map(function(el) {
return el / array.length
});
return r;
}, []);
return sum;
How can I perform the average with the numeric values only? I tried using .filter, but it didn't seem to work on Google Apps Script Editor, or maybe I was just using it wrong (very likely).
This is the condition I was using to filter:
(typeof el !== 'number')
How about following sample? You can use this on Google Apps Script.
var array = [
[,, 'User 1', 'User 2', 'User 3'],
['Movie 1', '20/1/17', 6.0, 2.0, 5.0],
['Movie 2', '21/1/17', 2.0, 0.0, 2.0],
['Movie 3', '22/1/17', 3.0, 1.0, 3.0]
];
var sum = array.reduce(function(r, e, i) {
e.forEach(function(a, j) {
r[j] = (parseInt(r[j]) || 0) + a;
})
if (i == array.length - 1) r = r.map(function(el) {
return isFinite(el) ? el : "";
});
return r;
}, []);
return [array[0].splice(2, 3), [x for each (x in sum)].splice(2, 3)];
>>> [[User 1, User 2, User 3], [11.0, 3.0, 10.0]]

Optimize matrices operations

I'm optimizing via Genetic Algorithm a machine simulator (of a MultiheadWeigher machine) in order to solve the well known "Setup problem". I based all the code on matricies but with the multiproduct case I think there is still some inefficiency...
Code
function [f] = MHW(position_matrix, HA, HB, HC)
global W_best_tot
global Function
global n_b_global
nComb = size(position_matrix,1);
dim = size(position_matrix,2);
WL = 241;
%SIMULATIONS VARIABLES
alpha = 0.123;
nSim = 3000;
CellUncertainty = 0.5 * 10^(-6);
%// Define Product Hopper allocation
WT_A = 130; WL_A = 129;
WT_B = 30; WL_B = 29;
%HC = 2;
WT_C = 90; WL_C = 89;
%// COMBINATION MATRIX
CombinantionMatrix_A = combn([0 1], HA);
CombinantionMatrix_B = combn([0 1], HB);
CombinantionMatrix_C = combn([0 1], HC);
if HA == 1
CombinantionMatrix_A = CombinantionMatrix_A.';
end
if HB == 1
CombinantionMatrix_B = CombinantionMatrix_B.';
end
if HC == 1
CombinantionMatrix_C = CombinantionMatrix_C.';
end
CombinantionMatrix_A_Transp = CombinantionMatrix_A.';
CombinantionMatrix_B_Transp = CombinantionMatrix_B.';
CombinantionMatrix_C_Transp = CombinantionMatrix_C.';
% OBJECTIVE FUNCTION COST COEFFICIENTS
Cu_A = 0.03; c_p = 0.6; c_f = 734; c_l = 3.2;
Cu_B = 0.09;
Cu_C = 0.04;
[HopperWeight UncertainWeight] = Weight(nComb, dim, alpha, ...
position_matrix, CellUncertainty);
HopperWeight_A = HopperWeight(:,1:HA);
HopperWeight_B = HopperWeight(:,HA+1:HA+HB);
HopperWeight_C = HopperWeight(:,HA+HB+1:HA+HB+HC);
UncertainWeight_A = UncertainWeight(:,1:HA);
UncertainWeight_B = UncertainWeight(:,HA+1:HA+HB);
UncertainWeight_C = UncertainWeight(:,HA+HB+1:HA+HB+HC);
W_best_tot = zeros(nComb, nSim);
W_best_ABC = zeros(nComb, 3, nSim);
for ii=1:nSim
W_A = HopperWeight_A * CombinantionMatrix_A_Transp;
W_B = HopperWeight_B * CombinantionMatrix_B_Transp;
W_C = HopperWeight_C * CombinantionMatrix_C_Transp;
W_Unc_A = UncertainWeight_A * CombinantionMatrix_A_Transp;
W_Unc_B = UncertainWeight_B * CombinantionMatrix_B_Transp;
W_Unc_C = UncertainWeight_C * CombinantionMatrix_C_Transp;
[~,comb_A] = min(abs(W_Unc_A - WT_A),[],2);
[~,comb_B] = min(abs(W_Unc_B - WT_B),[],2);
[~,comb_C] = min(abs(W_Unc_C - WT_C),[],2);
W_best_A = W_A(sub2ind_mod(size(W_A),1:size(W_A,1),comb_A.')).';
W_best_B = W_B(sub2ind_mod(size(W_B),1:size(W_B,1),comb_B.')).';
W_best_C = W_C(sub2ind_mod(size(W_C),1:size(W_C,1),comb_C.')).';
W_best_ABC(:,:,ii) = [W_best_A W_best_B W_best_C];
W_best_tot(:,ii) = sum(W_best_ABC(:,:,ii),2);
[HopperWeight_2_A UncertainWeight_2_A] = Weight(nComb, HA, alpha, ...
position_matrix(:,1:HA), CellUncertainty);
[HopperWeight_2_B UncertainWeight_2_B] = Weight(nComb, HB, alpha, ...
position_matrix(:,HA+1:HA+HB), CellUncertainty);
[HopperWeight_2_C UncertainWeight_2_C] = Weight(nComb, HC, alpha, ...
position_matrix(:,HA+HB+1:HA+HB+HC), CellUncertainty);
idx = CombinantionMatrix_A(comb_A,:)~=0;
HopperWeight_A(idx) = HopperWeight_2_A(idx);
UncertainWeight_A(idx) = UncertainWeight_2_A(idx);
idx = CombinantionMatrix_B(comb_B,:)~=0;
HopperWeight_B(idx) = HopperWeight_2_B(idx);
UncertainWeight_B(idx) = UncertainWeight_2_B(idx);
idx = CombinantionMatrix_C(comb_C,:)~=0;
HopperWeight_C(idx) = HopperWeight_2_C(idx);
UncertainWeight_C(idx) = UncertainWeight_2_C(idx);
clear HopperWeight_2_A HopperWeight_2_B HopperWeight_2_C ...
UncertainWeight_2_A UncertainWeight_2_B UncertainWeight_2_C;
end
n_b = logical(W_best_tot >= WL);
n_bA = logical(reshape(W_best_ABC(:,1,:), nComb, nSim) >= WL_A);
n_bB = logical(reshape(W_best_ABC(:,2,:), nComb, nSim) >= WL_B);
n_bC = logical(reshape(W_best_ABC(:,3,:), nComb, nSim) >= WL_C);
n_b_global = sum(n_b .* n_bA .* n_bB .* n_bC, 2);
GAvg_A = sum(reshape(W_best_ABC(:,1,:), nComb, nSim).*(reshape(...
W_best_ABC(:,1,:), nComb, nSim) > WT_A),2)./sum(reshape(...
W_best_ABC(:,1,:), nComb, nSim) > WT_A,2);
GAvg_B = sum(reshape(W_best_ABC(:,2,:), nComb, nSim).*(reshape(...
W_best_ABC(:,2,:), nComb, nSim) > WT_B),2)./sum(reshape(...
W_best_ABC(:,2,:), nComb, nSim) > WT_B,2);
GAvg_C = sum(reshape(W_best_ABC(:,3,:), nComb, nSim).*(reshape(...
W_best_ABC(:,3,:), nComb, nSim) > WT_C),2)./sum(reshape(...
W_best_ABC(:,3,:), nComb, nSim) > WT_C,2);
f = Cu_A .* GAvg_A + Cu_B .* GAvg_B + Cu_C .* GAvg_C + (nSim./...
n_b_global) .* c_p + (c_f./n_b_global) + ((nSim - n_b_global)./...
n_b_global) .* c_l;
Function = f;
Profiler
This is the Main Profiler and these are: the MHW function and the Weight function. The main problems are in the Weight since it's called 3000times for each GA generation...Above the Weight function code:
Weight Function Code
function [ HopperWeight UncertainWeight ] = Weight( nComb, H, alpha, AllComb, CellUncertainty )
HopperWeight = randn(nComb,H) * alpha;
HopperWeight = (1+HopperWeight) .* AllComb;
idx = HopperWeight<0;
random = randn(sum(idx(:)), 1);
HopperWeight(idx) = random .* ((1+alpha) .* AllComb(idx));
%ADD ERROR
RandomizeUncertainty = randn(nComb,H) * CellUncertainty;
UncertainWeight = abs((1+RandomizeUncertainty) .* HopperWeight);
end
Is there something that I am missing in order to optimize better the Weight function and in general the time consuming part in the MHW function? (If you need the GA launcher in order to try the MHW function use THIS code)
TIA

Resources