coerce real to int? or use real for vector index?

487 views
Skip to first unread message

Kyle Foreman

unread,
Sep 13, 2013, 1:11:45 AM9/13/13
to stan-...@googlegroups.com
Hello all,

Is there any way to coerce a real value into an integer? I need to use a real-valued parameter as the index for a vector.

Simple type coercion isn't allowed:
int a;
real b
;

b
<- 1.3;
a
<- floor(b); // type mismatch error

And using a real for an index of course also fails:
real a;
real b
;
vector
[2] c;
real d
;

b
<- 1.3;
a
<- floor(b); // works
d
<- c[b]; // error "
expression denoting integer required; found type=real"

Any suggestions on how to take a rounded real number and retrieve a vector element with it?

Thanks!

Sergio Polini

unread,
Sep 13, 2013, 4:39:57 AM9/13/13
to stan-...@googlegroups.com
real b;
vector[N] c;
real d;
int ndx;

If N == 2, as in your example,

d <- ifelse(b < 2.0, c[1], c[2]);

If N > 2,

ndx <- 1;
for (i in 2:N) {
if (i <= floor(b)
ndx <- i;
}
d <- c[ndx];

;-)

Sergio

Il 13/09/2013 07:11, Kyle Foreman ha scritto:
> Hello all,
>
> Is there any way to coerce a real value into an integer? I need to use a
> real-valued parameter as the index for a vector.
>
> Simple type coercion isn't allowed:
> |
> inta;
> real b;
>
> b <-1.3;
> a <-floor(b);// type mismatch error
> |
>
> And using a real for an index of course also fails:
> |
> real a;
> real b;
> vector[2]c;
> real d;
>
> b <-1.3;
> a <-floor(b);// works
> d <-c[b];// error "expression denoting integer required; found type=real"
> |
>
> Any suggestions on how to take a rounded real number and retrieve a
> vector element with it?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Kyle Foreman

unread,
Sep 13, 2013, 10:23:39 AM9/13/13
to stan-...@googlegroups.com
Ah, very clever, Sergio! I hadn't though of how to use a loop to generalize to N >> 2. Works great - I just subbed in a while loop instead for efficiency's sake. Thanks!



For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to a topic in the Google Groups "stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/VjFyXEIVarM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

Bob Carpenter

unread,
Sep 13, 2013, 11:02:59 AM9/13/13
to stan-...@googlegroups.com
There's a small problem with truncating real values to
integers --- you lose any autodiff information. So if
the truncation's part of the model, it may not work as you
intend.

Also, as an efficiency note, ifelse() evaluates both of
its arguments, which isn't a big deal in this case because
c is a plain vector. On the other hand, an if { } else { }
conditional statement only evaluates the block for the
condition that goes through.

- Bob
> an email to stan-users+unsubscribe@__googlegroups.com <mailto:stan-users%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/__groups/opt_out <https://groups.google.com/groups/opt_out>.
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "stan users mailing list" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/__topic/stan-users/VjFyXEIVarM/__unsubscribe
> <https://groups.google.com/d/topic/stan-users/VjFyXEIVarM/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@__googlegroups.com
> <mailto:stan-users%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/__groups/opt_out <https://groups.google.com/groups/opt_out>.
>
>
> --
> You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Kyle Foreman

unread,
Sep 13, 2013, 11:37:04 AM9/13/13
to stan-...@googlegroups.com
Thanks, Bob! The integer itself doesn't factor into the likelihood, it's just used in a linear interpolation. e.g. 

y <- x[floor(i)] + (x[ceil(i)] - x[floor(i)]) * (i - floor(i));
Reply all
Reply to author
Forward
0 new messages