Deefine a Sequence of Random Variables in Maple? - probability

I want to use the Statistics package that enables to do symbolic calculations with random variables to define a sequence of random variables.
This code does not work:
restart;
with(Statistics):
X:=RandomVariable(DiscreteUniform(1,26));
seq(X(i),i=0..1000);
because it displays:
> Mean(X(5));
>
_R(5)
Instead of 27/2.
Does somebody know how to fix this?

Make X a Vector, like this:
X:= Vector(1000, ()-> Statistics:-RandomVariable(DiscreteUniform(1,26))):
Then you can give commands like
Statistics:-Mean(X(5));
and you'll get your expected output.
If you insist on indexing starting at 0, it's a tiny bit more complicated. Change the defining command to
X:= Array(0..1000, ()-> Statistics:-RandomVariable(DiscreteUniform(1,26))):
And you'll need to do the indexing with square brackets: X[0], X[5], etc,

Related

rounding a number in ruby

I would like to round this figure to the nearest whole number. I am generating an xml based on an excel file, and would like to round the figure.
Here is my code:
xml.POS110 “wert”: “#{row[18]}”
I have tried:
xml.POS110 “wert”: “#{row[18]}”.round(0)
Move the round inside the quotes. Before, you were just trying to round a string. Also the default argument for round is 0 so you don't need to specify it (but you can if you really want to).
“#{row[18].round(0)}"

SPSS- assigning mulitple numeric codes to one variable

I am trying to assign multiple codes to existing variables. I am using the syntax below, but it will only assign the first code entered for that hosp.id.number.
Syntax example:
Do if (hosp.id.number=9037) or (hosp.id.number=1058) or (hosp.id.number=11256).
Compute role_EM_communication=10.
Else if (hosp.id.number=9037.
Compute role_EM_communication=11.
End if.
Execute.
hosp.id.number needs to be coded 10 and 11, but it will only code it at 10. Anyway to rephrase so that SPSS will accept 2 or more codes for a variable such as hosp.id.number?
Your role_EM_communication variable is a single variable, but from what you are saying, I think you need it to be a set (for the same record, it could take on more than just one code). So you need to create n variables named role_EM_communication_1 to role_EM_communication_n, where n is the maximum number of codes you estimate will be possible for one record.
For your example, it would translate like this:
create the 2 variables:
vector role_EM_communication_ (2, f2.0).
do the first recode:
if any(hosp.id.number,9037,1058,11256) role_EM_communication_1=10.
very important - execute the recode
exe.
check if the first variable has data, and populate the second variable if true:
if miss(role_EM_communication_1) and any(hosp.id.number,9037) role_EM_communication_1=11.
if ~miss(role_EM_communication_1) and any(hosp.id.number,9037) role_EM_communication_2=11.
exe.

Most efficient way to parse a file in Lua

I'm trying to figure out what is the most efficient way to parse data from a file using Lua. For example lets say I have a file (example.txt) with something like this in it:
0, Data
74, Instance
4294967295, User
255, Time
If I only want the numbers before the "," I could think of a few ways to get the information. I'd start out by getting the data with f = io.open(example.txt) and then use a for loop to parse each line of f. This leads to the heart of my question. What is the most efficient way to do this?
In the for loop I could use any of these methods to get the # before the comma:
line.find(regex)
line:gmatch(regex)
line:match(regex)
or Lua's split function
Has anyone run test for speed for these/other methods which they could point out as the fast way to parse? Bonus points if you can speak to speeds for parsing small vs. large files.
You probably want to use line:match("%d+").
line:find would work as well but returns more than you want.
line:gmatch is not what you need because it is meant to match several items in a string, not just one, and is meant to be used in a loop.
As for speed, you'll have to make your own measurements. Start with the simple code below:
for line in io.lines("example.txt") do
local x=line:match("%d+")
if x~=nil then print(x) end
end

Erlang on Windows List Comprehension multiply gives "\f"

Hi i have a weird problem with Erlang on Windows i am running 16B and WinXP.
I have the following code
-module(test).
-export([cost/1,total/1]).
cost(orange) ->
5;
cost(apple) ->
6.
total(L) ->
[cost(I) * Q || {I,Q} <- L].
I run it with
test:total([{orange,2}]).
and it gives me "\f"
changing cost(I) * Q to use -,+ or divide gives me a number.
I have no idea why multiply dosen't work in list comprehension. Running
[test:cost(I) * Q || {I,Q} <- [{orange,2}]]
in an erlang console and emacs mode also dosen't work but
test:cost(orange) * 2
does give me a number.
Any ideas why?
Note your cost/1 function returns a number. But total/1 returns a list (of numbers).
The results on that list are ok, this is just how erlang happens to display lists of small integers. See http://www.erlang.org/faq/problems.html 9.3
to see what I mean, try with larger numbers
test:total([{orange,2000}]).
Again, this is just a display issue, the value in the lists are what you expect. Try it:
[Value] = test:total([{orange,2}]).
Value.
A string is a list of integers. The value you're returning is a list of integers.
Erlang uses a simple heuristic for when to show something as a string, or as a list of integers: is it a flat list containing only numbers in the range {55,250}. (I made those numbers up, but it's something like that. If there are control characters or low characters, it bails.)
Since Erlang doesn't do this to tuples, tuples make it easy to see.
1> {72,101,108,108,111,44,32,83,116,101,112,104,101,110,46}.
{72,101,108,108,111,44,32,83,116,101,112,104,101,110,46}
2> [72,101,108,108,111,44,32,83,116,101,112,104,101,110,46].
"Hello, Stephen."
Erlang is just guessing wrongly what's inside the list.
HTH.

Format statement with unknown columns

I am attempting to use fortran to write out a comma-delimited file for import into another commercial package. The issue is that I have an unknown number of data columns. My output needs to look like this:
a_string,a_float,a_different_float,float_array_elem1,float_array_elem2,...,float_array_elemn
which would result in something that might look like this:
L1080,546876.23,4325678.21,300.2,150.125,...,0.125
L1090,563245.1,2356345.21,27.1245,...,0.00983
I have three issues. One, I would prefer the elements to be tightly grouped (variable column width), two, I do not know how to define a variable number of array elements in the format statement, and three, the array elements can span a large range--maybe 12 orders of magnitude. The following code conceptually does what I want, but the variable 'n' and the lack of column-width definition throws an error (of course):
WRITE(50,900) linenames(ii),loc(ii,1:2),recon(ii,1:n)
900 FORMAT(A,',',F,',',F,n(',',F))
(I should note that n is fixed at run-time.) The write statement does what I want it to when I do WRITE(50,*), except that it's width-delimited.
I think this thread almost answered my question, but I got quite confused: SO. Right now I have a shell script with awk fixing the issue, but that solution is...inelegant. I could do some manipulation to make the output a string, and then just write it, but I would rather like to avoid that option if at all possible.
I'm doing this in Fortran 90 but I like to try to keep my code as backwards-compatible as possible.
the format close to what you want is f0.3, this will give no spaces and a fixed number of decimal places. I think if you want to also lop off trailing zeros you'll need to do a good bit of work.
The 'n' in your write statement can be larger than the number of data values, so one (old school) approach is to put a big number there, eg 100000. Modern fortran does have some syntax to specify indefinite repeat, i'm sure someone will offer that up.
----edit
the unlimited repeat is as you might guess an asterisk..and is evideltly "brand new" in f2008
In order to make sure that no space occurs between the entries in your line, you can write them separately in character variables and then print them out using theadjustl() function in fortran:
program csv
implicit none
integer, parameter :: dp = kind(1.0d0)
integer, parameter :: nn = 3
real(dp), parameter :: floatarray(nn) = [ -1.0_dp, -2.0_dp, -3.0_dp ]
integer :: ii
character(30) :: buffer(nn+2), myformat
! Create format string with appropriate number of fields.
write(myformat, "(A,I0,A)") "(A,", nn + 2, "(',',A))"
! You should execute the following lines in a loop for every line you want to output
write(buffer(1), "(F20.2)") 1.0_dp ! a_float
write(buffer(2), "(F20.2)") 2.0_dp ! a_different_float
do ii = 1, nn
write(buffer(2+ii), "(F20.3)") floatarray(ii)
end do
write(*, myformat) "a_string", (trim(adjustl(buffer(ii))), ii = 1, nn + 2)
end program csv
The demonstration above is only for one output line, but you can easily write a loop around the appropriate block to execute it for all your output lines. Also, you can choose different numerical format for the different entries, if you wish.

Resources