# Creating a function after working out an example calculation

I have created this setup, it is giving me the correct answer, however I am interested in a function that does all this calculations and creates a output of mean and standard deviation. The function is has to be able to set the limit which i have named y, and run over a list.

In[302]:= list = RandomReal[{-10, 10}, 20]

Out[302]= {4.48825, 3.31096, -5.1268, -2.11263, 2.04758, 7.41939, \
1.37117, 4.73285, -1.38028, 4.55688, 6.2333, 5.67909, 6.41423, \
0.709616, -3.75265, -3.16793, 2.10447, 0.583272, -0.370173, -8.65539}

In[303]:= pp = Range[Length[list]];

In[304]:= y = 2;

In[305]:= vt = If[(-y > list[[#]] || list[[#]] > y), yes, no] & /@ pp

Out[305]= {yes, yes, yes, yes, yes, yes, no, yes, no, yes, yes, yes, \
yes, no, yes, yes, yes, no, no, yes}

In[306]:= groups = SplitBy[vt, # == yes]

Out[306]= {{yes, yes, yes, yes, yes, yes}, {no}, {yes}, {no}, {yes,
yes, yes, yes}, {no}, {yes, yes, yes}, {no, no}, {yes}}

In[307]:= fpt = DeleteCases[groups, no, {2}]

Out[307]= {{yes, yes, yes, yes, yes, yes}, {}, {yes}, {}, {yes, yes,
yes, yes}, {}, {yes, yes, yes}, {}, {yes}}

In[308]:= rp = Range[Length[fpt]];

In[309]:= bob = Length[fpt[[#]]] & /@ rp;

In[310]:= countLength = DeleteCases[bob, 0];

In[311]:= N[StandardDeviation[countLength]]

Out[311]= 2.12132

In[312]:= N[Mean[countLength]]

Out[312]= 3.

-
This is not a free coding service. You should try to build the function yourself. If you run into difficulties, then ask about the specific problem you have run into, – m_goldberg Sep 17 '13 at 12:44
I understand that, I have spent all day trying to come up with a solution I have pages on pages with different ideas, but no one looks even close to be able to solve the problem. I also thought that this was a pretty simple coding for most of the people on this forum, so I guessed it would be easier to let you guys show me how to do it then to play around with my bad coding. – ALEXANDER Sep 17 '13 at 12:53
@m_goldberg, give the man a break. OP obviously tried some. – Hector Sep 17 '13 at 12:55

g[l_List, y_] := N@{Mean@#, StandardDeviation@#} &@(Length /@
Cases[Split[Not[-y < # < y ] & /@ l], {True..}])

-
Hi! This looks amazing, would you be able to explain what is going on in the code? – ALEXANDER Sep 17 '13 at 12:55
@ALEXANDER I'm sure you can figure it out yourself. Just start trying the expressions inside out. Try -y < # < y & /@ l first, then add Not[], then Split[], and so on – Dr. belisarius Sep 17 '13 at 12:58
Okey, thanks, also one more question, how long have you been programming? – ALEXANDER Sep 17 '13 at 13:00
@ALEXANDER For longer than I want to confess – Dr. belisarius Sep 17 '13 at 13:05
@ALEXANDER Your code is not bad, that was the reason because I decided to answer and disregard goldberg's comment above. Perhaps a good book can help you to progress faster mathematica.stackexchange.com/questions/18/… – Dr. belisarius Sep 17 '13 at 13:17

Module[{pp = Range[Length[list]], y = 2, vt, groups, fpt, rp, bob, countLength},
vt = If[(-y > list[[#]] || list[[#]] > y), yes, no] & /@ pp;
groups = Split[vt]; fpt = DeleteCases[groups, no, {2}];
rp = Range[Length[fpt]]; bob = Length[fpt[[#]]] & /@ rp;
countLength = DeleteCases[bob, 0];
{N[StandardDeviation[countLength]], N[Mean[countLength]]}]


and check that it runs. The syntax is basically

Module[{vars}, code;eachLineSeparated;bySemicolons;Result]


Once you check it works, define the function

theFunction[y_][list_] := Module[{pp = Range[Length[list]], vt, groups, fpt, rp, bob,
countLength}, vt = If[(-y > list[[#]] || list[[#]] > y), yes, no] & /@ pp;
groups = Split[vt]; fpt = DeleteCases[groups, no, {2}];
rp = Range[Length[fpt]]; bob = Length[fpt[[#]]] & /@ rp;
countLength = DeleteCases[bob, 0];
{N[StandardDeviation[countLength]], N[Mean[countLength]]}]

-
Thank you Hector, smart way to gather and check the code, what I have done is extremely bad, but I have to start somewhere. I constantly experience new things that I am wondering about is there any easy way to example figure out what [y_][list_] is doing. – ALEXANDER Sep 17 '13 at 13:19
Quick and dirty, get the job done. 15 years later, you can produce the sleek code as shown in other posts. Then, you will begin thinking about optimizing the code, etc. Keep up the interest. As for f[x_][y_], because of the := that follows, it defines a function. Almost like f[x_,y_], except that f[x] is itself a function. You might want check reference.wolfram.com/mathematica/ref/…. – Hector Sep 17 '13 at 13:23
Thank you, I will definitely keep up the interest. Mathematica is an amazing tool. – ALEXANDER Sep 17 '13 at 13:40

I believe this code produces the desired result:

ms[data_,
y_] := {Mean[#], N@StandardDeviation[#]} &@(Length /@
Select[SplitBy[Boole[Abs[#] > y] & /@ data, 1], MemberQ[#, 1] &])


Using:

data = {4.48825, 3.31096, -5.1268, -2.11263, 2.04758, 7.41939,
1.37117, 4.73285, -1.38028, 4.55688, 6.2333, 5.67909, 6.41423,
0.709616, -3.75265, -3.16793, 2.10447, 0.583272, -0.370173, -8.65539}


ms[data,2] yields:

{3, 2.12132}
`
-