Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to write this function in mathematica exactly as shown, and then run the expression on a dataset?

enter image description here

Please note I know that there are several other ways to do this. I am only interested in knowing how to compute it(if its possible) using notation that you would find in a text book.

share|improve this question
    
This question would benefit from some attempt to actually do this in Mathematica and Mr. Wizard's answer raises important issues. Still, I've up voted it, because trying this kind of thing helped my entry into the Mathematica universe. I think it could also help others. Not the most efficient way to use Mathematica, but interesting none the less. –  Jagra Sep 26 '13 at 14:34
add comment

2 Answers

up vote 6 down vote accepted

The meaning of the subscript is not pre-defined in Mathematica. In this instance it is used to grab an element of a dataset, but it could be used as an adjective like Subscript[F, gravity]. To use the subscript notation you could use

<< Notation`
Notation[ParsedBoxWrapper[SubscriptBox["x_", "i_"]] \[DoubleLongLeftRightArrow] ParsedBoxWrapper[RowBox[{"x_", "[[", "i_", "]]"}]]]

Notation package makes subscripts equivalent to part

Then you could write a block of code which implements the procedure with whichever dataset is assigned to x at the moment of evaluation.

x = RandomReal[{0, 1}, 30];

Note N is a built-in function, so we will use [ScriptCapitalN] instead

\[ScriptCapitalN] = Length@x;
\[Mu] = 1/\[ScriptCapitalN] \!\(\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(\[ScriptCapitalN]\)]\*SubscriptBox[\(x\), \(i\)]\);
\[Sigma] = Sqrt[1/\[ScriptCapitalN] \!\(\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(\[ScriptCapitalN]\)]\*SuperscriptBox[\((\*SubscriptBox[\(x\), \(i\)] - \[Mu])\), \(2\)]\)];

immediate assignments

A little less "exactly as written", you could reinforce the idea that each of these quantities depends on the dataset by making them functions.

ClearAll[\[ScriptCapitalN], \[Mu], \[Sigma]];
\[ScriptCapitalN][x_] := Length@x;
\[Mu][x_] := 1/\[ScriptCapitalN][x] \!\(\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(\[ScriptCapitalN][x]\)]\*SubscriptBox[\(x\), \(i\)]\);
\[Sigma][x_] := Sqrt[1/\[ScriptCapitalN][x] \!\(\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(\[ScriptCapitalN][x]\)]\*SuperscriptBox[\((\*SubscriptBox[\(x\), \(i\)] - \[Mu][x])\), \(2\)]\)];
\[Sigma]@x

delayed assignments

share|improve this answer
1  
In a teaching setting, I would use this for small data sets so students see how the computer is just doing tedious hand calculations very quickly. Then I would introduce large data sets so the computer hangs. This is because functions are being called over and over unnecessarily. Ie, our implementation is inefficient. Then I would introduce the built-in functions, as efficient implementations for the same calculations. –  Timothy Wofford Sep 26 '13 at 8:03
    
This is great! Thank you! –  ALEXANDER Sep 27 '13 at 6:27
add comment

It matters if this about entering the expression (function), or displaying the expression. (Please clarify.)

If it is about entering the expression you will find that while you can get close in many cases it will not be exact, and also that there are ambiguities that must be resolved with more definite coding. Generally speaking I would recommend that you do not attempt this and instead enter functions in the most native Mathematica format.

If it is about display, e.g. TraditionalForm, then you can use things like Interpretation, and your own MakeBoxes or Format definitions.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.