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.

In the following code how can I keep the DynamicModule from computing values if the input values are not all filled? If I don't specify default values it will show the code used to compute the values instead of blank values.

Thanks.

backingAgreementValue[w_, \[Sigma]_, 
  a_] := \[Sigma] /Sqrt[2 \[Pi]] Exp[ -(w - a)^2/(2 \[Sigma]^2)] + 
  w CDF[NormalDistribution[0, 1], (w - a)/\[Sigma]]

DynamicModule[{w = 40, \[Sigma] = 600, a = 0, n = 100, horseCut = 0.5,
   backingValue, expectation, backerValue, horseValue, 
  horseExpectationPercentage, values}, 
 Deploy[Style[
   Panel[Grid[
     Transpose[{{Style["Mean", Blue], 
        Style["Standard Deviation", Blue], Style["Threshold", Blue], 
        Style["N", Blue], Style["Player Cut", Blue], 
        Style["Backing Agreement Value", Orange], 
        Style["Expectation", Orange], Style["Player Value", Orange], 
        Style["Player Expectation Percentage", Orange], 
        Style["Backer Value", Darker[Magenta]]}, {InputField[
         Dynamic[w], Number], InputField[Dynamic[\[Sigma]], Number], 
        InputField[Dynamic[a], Number], 
        InputField[Dynamic[n], Number], 
        InputField[Dynamic[horseCut], Number],
        Dynamic[
         Style[backingValue = 
           backingAgreementValue[expectation, \[Sigma] Sqrt[n], a] // 
            N, If[backingValue >= 0, Darker[Green], Red]]],
        Dynamic[
         Style[expectation = w n // N, 
          If[expectation >= 0, Darker[Green], Red]]],
        Dynamic[
         Style[horseValue = horseCut  backingValue // N, 
          If[horseValue >= 0, Darker[Green], Red]]],
        Dynamic[
         Style[horseExpectationPercentage = 
           horseCut backingValue/expectation // N, 
          If[horseExpectationPercentage >= 0.5, Darker[Green], Red]]],
        Dynamic[
         Style[backerValue = (expectation -  
              backingValue + (1 - horseCut) backingValue) // N, 
          If[backerValue >= 0, Darker[Green], Red]]]}}], 
     Alignment -> Left], ImageMargins -> 10], 
   DefaultOptions -> {InputField -> {ContinuousAction -> True, 
       FieldSize -> {{5, 30}, {1, Infinity}}}}]], 
 SaveDefinitions -> True]
share|improve this question
    
SaveDefinitions is not an option for DynamicModule –  Mike Honeychurch Aug 18 '13 at 11:22
    
Thanks but how does this help with my initial question? –  dabd Aug 18 '13 at 11:26
    
@MikeHoneychurch In Version 9 SaveDefinitions is an option of DynamicModule. SaveDefinitions /. Options[DynamicModule, SaveDefinitions] gives False. Sure, it is not documented, but a lot is not documented ... –  Rolf Mertig Aug 18 '13 at 12:53
    
@RolfMertig ok. I have V8 on this computer and work with 8 most of the time –  Mike Honeychurch Aug 18 '13 at 12:56
    
@MikeHoneychurch I never use M8 anymore. M9 is really better for DynamicModule, CDF's and all that jazz. –  Rolf Mertig Aug 18 '13 at 12:59
show 1 more comment

2 Answers

up vote 1 down vote accepted

Wrap a test around the visible code. If the test passes then display the calculation otherwise display a e.g. spacer:

If[NumberQ[\[Sigma]]&&NumberQ[n]& ...etc,
Dynamic[Style[
  backingValue = 
   backingAgreementValue[expectation, \[Sigma] Sqrt[n], a] // N, 
  If[backingValue >= 0, Darker[Green], Red]]],
Spacer[0]
]
share|improve this answer
add comment

Given that they have the same format, I might write a function that tests whether the input evaluates to a number. This will catch some bad input, too, such as a negative or zero value for n, although if you want to seriously address invalid input, you probably ought to give the user a hint about what's wrong. Or prevent invalid input.

display[x_, threshold_: 0] /; NumberQ[N[x]] := 
  Style[N[x], If[x >= threshold, Darker[Green], Red]];
display[__] = Spacer[0];

Then

...,
Dynamic[display[backingValue = backingAgreementValue[expectation, \[Sigma] Sqrt[n], a]]],
Dynamic[display[expectation = w n]],
Dynamic[display[horseValue = horseCut backingValue]],
Dynamic[display[horseExpectationPercentage = horseCut backingValue/expectation,  0.5]],
Dynamic[display[backerValue = (expectation - backingValue + (1 - horseCut) backingValue)]]
...
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.