I am in the middle of making a CDF for a calculation of many random variables. I've gone with DynamicModule and have read the documentation plus these posts: 3229432294 and 3007930079. I've obviously missed a few things as it is not working as expected.
DynamicModule[ (* Parameters *) {numberOfRuns = 10000, investmentCaptial = 15000000, (* Many more dynamic parameters in this section *) \[Mu]OverheadExpensesμOverheadExpenses = 600000, \[Sigma]OverheadExpensesσOverheadExpenses = 40000, \[Mu]ReturnOnInvestμReturnOnInvest = 0.05, \[Sigma]SpliceLowerσSpliceLower = 0.03, \[Sigma]SpliceUpperσSpliceUpper = 0.02, update = 1}, rtnPeriods = {50, 100, 250, 500, 1000}; variableNames = {(* more names for the omitted dynamic variables above *) "OverheadExpenses", "ReturnOnInvestLogN", "ReturnOnInvestSpliceN"}; (* A few other constant parameters in this section *) Dynamic@Refresh[ (* Many distributions defined in this section to the dynamic parameters omitted above *) overheadExpensesDist = NormalDistribution[\[Mu]OverheadExpensesNormalDistribution[μOverheadExpenses, \[Sigma]OverheadExpenses];σOverheadExpenses]; returnOnInvestLogNDist = LogNormalDistribution[1 + \[Mu]ReturnOnInvestμReturnOnInvest, Sqrt[\[Sigma]SpliceLower^2Sqrt[σSpliceLower^2 + \[Sigma]SpliceUpper^2]];σSpliceUpper^2]]; returnOnInvestSpliceNDist = SplicedDistribution[{\[Sigma]SpliceLowerσSpliceLower/\[Sigma]SpliceUpperσSpliceUpper, 1}, {-\[Infinity]∞, \[Mu]ReturnOnInvestμReturnOnInvest, \[Infinity]∞}, { NormalDistribution[\[Mu]ReturnOnInvestNormalDistribution[μReturnOnInvest, \[Sigma]SpliceLower]σSpliceLower], NormalDistribution[\[Mu]ReturnOnInvestNormalDistribution[μReturnOnInvest, \[Sigma]SpliceUpper]σSpliceUpper]}]; (* Single step simulation *) sampleRuns = Transpose[ RandomVariate[#, numberOfRuns] & /@ {(* More distributions here o the dynamic parameters omitted above *) overheadExpensesDist, returnOnInvestLogNDist, returnOnInvestSpliceNDist}]; runsAssoc = AssociationThread[variableNames, #] & /@ sampleRuns; (* Calculations begin *) {netIncomeInvestLogN, netIncomeInvestSpliceN} = Transpose[(Function[{rtnOnInvest}, netIncome[(* More dynamic parameter names here *)#OverheadExpenses, Switch[rtnOnInvest, "ReturnOnInvestLogN", Log[#[rtnOnInvest]] - 1, "ReturnOnInvestSpliceN", #[rtnOnInvest]], investmentCaptial]] /@ {"ReturnOnInvestLogN", "ReturnOnInvestSpliceN"}) & /@ runsAssoc]; {netIncomeDistPlotLogN, netIncomeDistPlotSpliceN} = {distributionPlot[ netIncomeInvestLogN, PlotLabel -> "Investment Return \[Distributed] " <> ToString@Head[returnOnInvestLogNDist], ImageSize -> 400], distributionPlot[netIncomeInvestSpliceN, PlotLabel -> "Investment Return \[Distributed] " <> ToString@Head[returnOnInvestSpliceNDist], ImageSize -> 400]}; {netIncomeInvestLogNHdt, netIncomeInvestSpliceNHdt} = DistributionFitTest[#, NormalDistribution[x, y], "HypothesisTestData"] & /@ {netIncomeInvestLogN, netIncomeInvestSpliceN}; {netIncomeInvestLogNReturnPeriod, netIncomeInvestSpliceNReturnPeriod} = returnPeriodTables[{netIncomeInvestLogNHdt, netIncomeInvestSpliceNHdt}, "Net Income"]; (* More calcuations, tables, and plots on simulation *) , TrackedSymbols -> {update} ] Column[{ Grid[{ {, "Distribution", "Mean", "Std. Deviation"}, (* More variable rows here *) Evaluate@ variableRow["Overhead Expense", "Normal", Dynamic@\[Mu]OverheadExpensesDynamic@μOverheadExpenses, {500000, 4000000, 25000}, Dynamic@\[Sigma]OverheadExpensesDynamic@σOverheadExpenses, {30000, 100000, 5000}], {Item["Return On Investment", Alignment -> Center], "Log Normal", Item[ Column[{Dynamic@\[Mu]ReturnOnInvestDynamic@μReturnOnInvest, Slider[Dynamic@\[Mu]ReturnOnInvestSlider[Dynamic@μReturnOnInvest, {.01, .08, .005}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Alignment -> Center], Item[ Dynamic@Sqrt[\[Sigma]SpliceLower^2Dynamic@Sqrt[σSpliceLower^2 + \[Sigma]SpliceUpper^2]σSpliceUpper^2], Alignment -> {Center, Bottom}]}, {SpanFromAbove, "Splice Normal", SpanFromAbove, Row[{ Column[{Dynamic@\[Sigma]SpliceLowerDynamic@σSpliceLower, Slider[Dynamic@\[Sigma]SpliceLowerSlider[Dynamic@σSpliceLower, {.001, .1, .001}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Column[{Dynamic@\[Sigma]SpliceUpperDynamic@σSpliceUpper, Slider[Dynamic@\[Sigma]SpliceUpperSlider[Dynamic@σSpliceUpper, {.001, .1, .001}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}] }, Spacer[2]]}, {, "Number of Runs", PopupMenu[Dynamic[numberOfRuns], Range[25000, 100000, 25000], "Select ..."]} }], Button["Run Simulation", update = update + 1, ImageSize -> Large], Dynamic@netIncomeDistPlotLogN, Dynamic@netIncomeInvestLogNReturnPeriod }, Dividers -> {None, {-3 -> True}}] , (* Initialise functions *) Initialization :> ( netIncome[(* parameters removed for example *)overheadExpenses_, returnOnInvestment_, investmentCapital_] := returnOnInvestment*investmentCapital - overheadExpenses; distributionPlot[data_List, opts : OptionsPattern[{Histogram, SmoothHistogram}]] := With[{\[Mu]μ = Mean@data, \[Sigma]σ = StandardDeviation@data}, Show[ Histogram[data, 30, "PDF", Sequence @@ FilterRules[{opts}, Options[Histogram]], Frame -> {{False, False}, {True, False}}], SmoothHistogram[data, Sequence @@ FilterRules[{opts}, Options[SmoothHistogram]]] ]]; returnPeriodTables[hypoTestData_List, valueHeading_String] := TableForm[ Transpose@{Function[{rp}, AccountingForm[InverseCDF[#["FittedDistribution"], 1/rp](*, DigitBlock\[Rule]{3,2}*)]] /@ rtnPeriods, rtnPeriods}, TableHeadings -> {None, {valueHeading, "Return Period"}}] & /@ hypoTestData; variableRow[name_String, distributionName_String, \[Mu]Variable_μVariable_, \[Mu]VariableRange_μVariableRange_, \[Sigma]Variable_σVariable_, \[Sigma]VariableRange_]σVariableRange_] := {name, distributionName, Column[{\[Mu]VariableμVariable, Slider[\[Mu]VariableSlider[μVariable, \[Mu]VariableRangeμVariableRange, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Column[{\[Sigma]VariableσVariable, Slider[\[Sigma]VariableSlider[σVariable, \[Sigma]VariableRangeσVariableRange, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}]}; ), UnsavedVariables :> {update}, SaveDefinitions -> True ] DynamicModule[ (* Parameters *) {numberOfRuns = 10000, investmentCaptial = 15000000, (* Many more dynamic parameters in this section *) \[Mu]OverheadExpenses = 600000, \[Sigma]OverheadExpenses = 40000, \[Mu]ReturnOnInvest = 0.05, \[Sigma]SpliceLower = 0.03, \[Sigma]SpliceUpper = 0.02, update = 1}, rtnPeriods = {50, 100, 250, 500, 1000}; variableNames = {(* more names for the omitted dynamic variables above *) "OverheadExpenses", "ReturnOnInvestLogN", "ReturnOnInvestSpliceN"}; (* A few other constant parameters in this section *) Dynamic@Refresh[ (* Many distributions defined in this section to the dynamic parameters omitted above *) overheadExpensesDist = NormalDistribution[\[Mu]OverheadExpenses, \[Sigma]OverheadExpenses]; returnOnInvestLogNDist = LogNormalDistribution[1 + \[Mu]ReturnOnInvest, Sqrt[\[Sigma]SpliceLower^2 + \[Sigma]SpliceUpper^2]]; returnOnInvestSpliceNDist = SplicedDistribution[{\[Sigma]SpliceLower/\[Sigma]SpliceUpper, 1}, {-\[Infinity], \[Mu]ReturnOnInvest, \[Infinity]}, { NormalDistribution[\[Mu]ReturnOnInvest, \[Sigma]SpliceLower], NormalDistribution[\[Mu]ReturnOnInvest, \[Sigma]SpliceUpper]}]; (* Single step simulation *) sampleRuns = Transpose[ RandomVariate[#, numberOfRuns] & /@ {(* More distributions here o the dynamic parameters omitted above *) overheadExpensesDist, returnOnInvestLogNDist, returnOnInvestSpliceNDist}]; runsAssoc = AssociationThread[variableNames, #] & /@ sampleRuns; (* Calculations begin *) {netIncomeInvestLogN, netIncomeInvestSpliceN} = Transpose[(Function[{rtnOnInvest}, netIncome[(* More dynamic parameter names here *)#OverheadExpenses, Switch[rtnOnInvest, "ReturnOnInvestLogN", Log[#[rtnOnInvest]] - 1, "ReturnOnInvestSpliceN", #[rtnOnInvest]], investmentCaptial]] /@ {"ReturnOnInvestLogN", "ReturnOnInvestSpliceN"}) & /@ runsAssoc]; {netIncomeDistPlotLogN, netIncomeDistPlotSpliceN} = {distributionPlot[ netIncomeInvestLogN, PlotLabel -> "Investment Return \[Distributed] " <> ToString@Head[returnOnInvestLogNDist], ImageSize -> 400], distributionPlot[netIncomeInvestSpliceN, PlotLabel -> "Investment Return \[Distributed] " <> ToString@Head[returnOnInvestSpliceNDist], ImageSize -> 400]}; {netIncomeInvestLogNHdt, netIncomeInvestSpliceNHdt} = DistributionFitTest[#, NormalDistribution[x, y], "HypothesisTestData"] & /@ {netIncomeInvestLogN, netIncomeInvestSpliceN}; {netIncomeInvestLogNReturnPeriod, netIncomeInvestSpliceNReturnPeriod} = returnPeriodTables[{netIncomeInvestLogNHdt, netIncomeInvestSpliceNHdt}, "Net Income"]; (* More calcuations, tables, and plots on simulation *) , TrackedSymbols -> {update} ] Column[{ Grid[{ {, "Distribution", "Mean", "Std. Deviation"}, (* More variable rows here *) Evaluate@ variableRow["Overhead Expense", "Normal", Dynamic@\[Mu]OverheadExpenses, {500000, 4000000, 25000}, Dynamic@\[Sigma]OverheadExpenses, {30000, 100000, 5000}], {Item["Return On Investment", Alignment -> Center], "Log Normal", Item[ Column[{Dynamic@\[Mu]ReturnOnInvest, Slider[Dynamic@\[Mu]ReturnOnInvest, {.01, .08, .005}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Alignment -> Center], Item[ Dynamic@Sqrt[\[Sigma]SpliceLower^2 + \[Sigma]SpliceUpper^2], Alignment -> {Center, Bottom}]}, {SpanFromAbove, "Splice Normal", SpanFromAbove, Row[{ Column[{Dynamic@\[Sigma]SpliceLower, Slider[Dynamic@\[Sigma]SpliceLower, {.001, .1, .001}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Column[{Dynamic@\[Sigma]SpliceUpper, Slider[Dynamic@\[Sigma]SpliceUpper, {.001, .1, .001}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}] }, Spacer[2]]}, {, "Number of Runs", PopupMenu[Dynamic[numberOfRuns], Range[25000, 100000, 25000], "Select ..."]} }], Button["Run Simulation", update = update + 1, ImageSize -> Large], Dynamic@netIncomeDistPlotLogN, Dynamic@netIncomeInvestLogNReturnPeriod }, Dividers -> {None, {-3 -> True}}] , (* Initialise functions *) Initialization :> ( netIncome[(* parameters removed for example *)overheadExpenses_, returnOnInvestment_, investmentCapital_] := returnOnInvestment*investmentCapital - overheadExpenses; distributionPlot[data_List, opts : OptionsPattern[{Histogram, SmoothHistogram}]] := With[{\[Mu] = Mean@data, \[Sigma] = StandardDeviation@data}, Show[ Histogram[data, 30, "PDF", Sequence @@ FilterRules[{opts}, Options[Histogram]], Frame -> {{False, False}, {True, False}}], SmoothHistogram[data, Sequence @@ FilterRules[{opts}, Options[SmoothHistogram]]] ]]; returnPeriodTables[hypoTestData_List, valueHeading_String] := TableForm[ Transpose@{Function[{rp}, AccountingForm[InverseCDF[#["FittedDistribution"], 1/rp](*, DigitBlock\[Rule]{3,2}*)]] /@ rtnPeriods, rtnPeriods}, TableHeadings -> {None, {valueHeading, "Return Period"}}] & /@ hypoTestData; variableRow[name_String, distributionName_String, \[Mu]Variable_, \[Mu]VariableRange_, \[Sigma]Variable_, \[Sigma]VariableRange_] := {name, distributionName, Column[{\[Mu]Variable, Slider[\[Mu]Variable, \[Mu]VariableRange, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Column[{\[Sigma]Variable, Slider[\[Sigma]Variable, \[Sigma]VariableRange, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}]}; ), UnsavedVariables :> {update}, SaveDefinitions -> True ] DynamicModule[ (* Parameters *) {numberOfRuns = 10000, investmentCaptial = 15000000, (* Many more dynamic parameters in this section *) μOverheadExpenses = 600000, σOverheadExpenses = 40000, μReturnOnInvest = 0.05, σSpliceLower = 0.03, σSpliceUpper = 0.02, update = 1}, rtnPeriods = {50, 100, 250, 500, 1000}; variableNames = {(* more names for the omitted dynamic variables above *) "OverheadExpenses", "ReturnOnInvestLogN", "ReturnOnInvestSpliceN"}; (* A few other constant parameters in this section *) Dynamic@Refresh[ (* Many distributions defined in this section to the dynamic parameters omitted above *) overheadExpensesDist = NormalDistribution[μOverheadExpenses, σOverheadExpenses]; returnOnInvestLogNDist = LogNormalDistribution[1 + μReturnOnInvest, Sqrt[σSpliceLower^2 + σSpliceUpper^2]]; returnOnInvestSpliceNDist = SplicedDistribution[{σSpliceLower/σSpliceUpper, 1}, {-∞, μReturnOnInvest, ∞}, { NormalDistribution[μReturnOnInvest, σSpliceLower], NormalDistribution[μReturnOnInvest, σSpliceUpper]}]; (* Single step simulation *) sampleRuns = Transpose[ RandomVariate[#, numberOfRuns] & /@ {(* More distributions here o the dynamic parameters omitted above *) overheadExpensesDist, returnOnInvestLogNDist, returnOnInvestSpliceNDist}]; runsAssoc = AssociationThread[variableNames, #] & /@ sampleRuns; (* Calculations begin *) {netIncomeInvestLogN, netIncomeInvestSpliceN} = Transpose[(Function[{rtnOnInvest}, netIncome[(* More dynamic parameter names here *)#OverheadExpenses, Switch[rtnOnInvest, "ReturnOnInvestLogN", Log[#[rtnOnInvest]] - 1, "ReturnOnInvestSpliceN", #[rtnOnInvest]], investmentCaptial]] /@ {"ReturnOnInvestLogN", "ReturnOnInvestSpliceN"}) & /@ runsAssoc]; {netIncomeDistPlotLogN, netIncomeDistPlotSpliceN} = {distributionPlot[ netIncomeInvestLogN, PlotLabel -> "Investment Return \[Distributed] " <> ToString@Head[returnOnInvestLogNDist], ImageSize -> 400], distributionPlot[netIncomeInvestSpliceN, PlotLabel -> "Investment Return \[Distributed] " <> ToString@Head[returnOnInvestSpliceNDist], ImageSize -> 400]}; {netIncomeInvestLogNHdt, netIncomeInvestSpliceNHdt} = DistributionFitTest[#, NormalDistribution[x, y], "HypothesisTestData"] & /@ {netIncomeInvestLogN, netIncomeInvestSpliceN}; {netIncomeInvestLogNReturnPeriod, netIncomeInvestSpliceNReturnPeriod} = returnPeriodTables[{netIncomeInvestLogNHdt, netIncomeInvestSpliceNHdt}, "Net Income"]; (* More calcuations, tables, and plots on simulation *) , TrackedSymbols -> {update} ] Column[{ Grid[{ {, "Distribution", "Mean", "Std. Deviation"}, (* More variable rows here *) Evaluate@ variableRow["Overhead Expense", "Normal", Dynamic@μOverheadExpenses, {500000, 4000000, 25000}, Dynamic@σOverheadExpenses, {30000, 100000, 5000}], {Item["Return On Investment", Alignment -> Center], "Log Normal", Item[ Column[{Dynamic@μReturnOnInvest, Slider[Dynamic@μReturnOnInvest, {.01, .08, .005}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Alignment -> Center], Item[ Dynamic@Sqrt[σSpliceLower^2 + σSpliceUpper^2], Alignment -> {Center, Bottom}]}, {SpanFromAbove, "Splice Normal", SpanFromAbove, Row[{ Column[{Dynamic@σSpliceLower, Slider[Dynamic@σSpliceLower, {.001, .1, .001}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Column[{Dynamic@σSpliceUpper, Slider[Dynamic@σSpliceUpper, {.001, .1, .001}, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}] }, Spacer[2]]}, {, "Number of Runs", PopupMenu[Dynamic[numberOfRuns], Range[25000, 100000, 25000], "Select ..."]} }], Button["Run Simulation", update = update + 1, ImageSize -> Large], Dynamic@netIncomeDistPlotLogN, Dynamic@netIncomeInvestLogNReturnPeriod }, Dividers -> {None, {-3 -> True}}] , (* Initialise functions *) Initialization :> ( netIncome[(* parameters removed for example *)overheadExpenses_, returnOnInvestment_, investmentCapital_] := returnOnInvestment*investmentCapital - overheadExpenses; distributionPlot[data_List, opts : OptionsPattern[{Histogram, SmoothHistogram}]] := With[{μ = Mean@data, σ = StandardDeviation@data}, Show[ Histogram[data, 30, "PDF", Sequence @@ FilterRules[{opts}, Options[Histogram]], Frame -> {{False, False}, {True, False}}], SmoothHistogram[data, Sequence @@ FilterRules[{opts}, Options[SmoothHistogram]]] ]]; returnPeriodTables[hypoTestData_List, valueHeading_String] := TableForm[ Transpose@{Function[{rp}, AccountingForm[InverseCDF[#["FittedDistribution"], 1/rp](*, DigitBlock\[Rule]{3,2}*)]] /@ rtnPeriods, rtnPeriods}, TableHeadings -> {None, {valueHeading, "Return Period"}}] & /@ hypoTestData; variableRow[name_String, distributionName_String, μVariable_, μVariableRange_, σVariable_, σVariableRange_] := {name, distributionName, Column[{μVariable, Slider[μVariable, μVariableRange, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}], Column[{σVariable, Slider[σVariable, σVariableRange, Appearance -> Tiny, ImageSize -> Tiny]}, Alignment -> {Center, Center}]}; ), UnsavedVariables :> {update}, SaveDefinitions -> True ] The
Dynamic@Refreshkeeps kicking out aNull. If I end it with a;then none of the plots and tables update when the Run Simulation button is pressed. This seems odd that making it not to display output makes it not run the code inside. How do I get rid of thisNull? Is this a bug?Is this a bug?When previewed as CDF Player I get a ton of errors from the
FE'context when I click the Run Simulation button. I haveSaveDefinitions -> Trueon the module so am not certain why this is happening.When the CDF Player is previewed moving the window it is in causes the simulation to repeatedly run. This only happens until the Run Simulation button is pressed. After it is pressed moving the window does not cause the simulation to run. It is an expensive simulation so this behaviour is very much not desired.
This has the same threefour problems described above.
The
Dynamic@Refreshkeeps kicking out aNull. If I end it with a;then none of the plots and tables update when the Run Simulation button is pressed. This seems odd that it not to display output makes it not run the code inside. How do I get rid of thisNull? Is this a bug?When previewed as CDF Player I get a ton of errors from the
FE'context when I click the Run Simulation button. I haveSaveDefinitions -> Trueon the module so am not certain why this is happening.When the CDF Player is previewed moving the window it is in causes the simulation to repeatedly run. This only happens until the Run Simulation button is pressed. After it is pressed moving the window does not cause the simulation to run. It is an expensive simulation so this behaviour is very much not desired.
This has the same three problems described above.
The
Dynamic@Refreshkeeps kicking out aNull. If I end it with a;then none of the plots and tables update when the Run Simulation button is pressed. This seems odd that making it not display output makes it not run the code inside. How do I get rid of thisNull? Is this a bug?When previewed as CDF Player I get a ton of errors from the
FE'context when I click the Run Simulation button. I haveSaveDefinitions -> Trueon the module so am not certain why this is happening.When the CDF Player is previewed moving the window it is in causes the simulation to repeatedly run. This only happens until the Run Simulation button is pressed. After it is pressed moving the window does not cause the simulation to run. It is an expensive simulation so this behaviour is very much not desired.
This has the same four problems described above.