Weather Conditions in Mathematica - wolfram-mathematica

I have yet to find a city or location or date, that gives me an answer other than Missing[NotApplicable] to the following query.
WeatherData["Chicago", "Conditions", {2010, 8}]
That syntax works for "Temperature" and "Pressure" and "WindDirection" But not "Conditions". In particular, if you want data returned for "Conditions" you need to add the "Value" keyword (as the responder below noted.) Why is that? i.e. this command works:
WeatherData["Chicago", "Conditions", {2010, 8}, "Value"]
But now, no DateTime stamps are included, so we are only left to guess when the Conditions were recorded. (On Temperature for instance, readings are not once / hour, although typically they are once per hour.)
This is Mathematica 7.0

Hmmm, I'm getting output:
In[8]:= WeatherData["Chicago","Conditions",{2010,8, 1}, "Value"]
Out[8]= {{},{},{},{},{},{},{Fog},{Fog},{Fog},{Fog},{Fog},{Fog},{},
{},{},{},{},{},{},{},{},{},{},{},{},{}}
Do other WeatherData[] syntaxes return data for you?

Related

TradingChart Example does not work

I am new to Mathematica 10 and I was trying to get one of the examples from Wolfram to work. The example is the following:
TradingChart[{"SP500", {{2009, 1, 1}, {2009, 12, 31}}}]
However, I always get the following error:
TradingChart::notdate: "{{2009,1,1},{2009,12,31}} is not a valid date range for \!\(\"TradingChart\"\)."
I do not understand how I can modify the date range to get it to work. Maybe one of you could give me a hint in the right direction.
You can also find the examples here: http://reference.wolfram.com/language/ref/TradingChart.html
try this..
If[$VersionNumber < 11 , UpTo[3] = 3 ]
TradingChart[{"SP500", {{2009, 1, 1}, {2009, 12, 31}}}]

pymc3 how to code multi-state discrete Bayes net CPT?

I'm trying to build a simple Bayesian network, where rain and sprinkler are the parents of wetgrass, but rain and sprinkler each have three (fuzzy-logic type rather rather than the usual two boolean) states, and wetgrass has two states (true/false). I can't find anywhere in the pymc3 docs what syntax to use to describe the CPTs for this -- I'm trying the following based on 2-state examples but it's not generalizing to three states the way I thought it would. Can anyone show the correct way to do this? (And also for the more general case where wetgrass has three states too.)
rain = mc.Categorical('rain', p = np.array([0.5, 0. ,0.5]))
sprinker = mc.Categorical('sprinkler', p=np.array([0.33,0.33,0.34]))
wetgrass = mc.Categorical('wetgrass',
mc.math.switch(rain,
mc.math.switch(sprinker, 10, 1, -4),
mc.math.switch(sprinker, -20, 1, 3),
mc.math.switch(sprinker, -5, 1, -0.5)))
[gives error at wetgrass definition:
Wrong number of inputs for Switch.make_node (got 4((, , , )), expected 3)
]
As I understand it - switch is a theano function similar to (b?a:b) in a C program; which is only doing a two way comparison. It's maybe possible to set up the CPT using a whole load of binary switches like this, but I really want to just give a 3D matrix CPT as the input as in BNT and other bayes net libraries. Is this currently possible ?
You can code a three-way switch using two individual switches:
tt.switch(sprinker == 0,
10
tt.switch(sprinker == 1, 1, -4))
But in general it is probably better to index into a table:
table = tt.constant(np.array([[...], [...]]))
value = table[rain, sprinker]

Mathematica, convert string to number

I imported a table in mathematica using Import["..file","table"] now I cant use the numbers in the table as they are in string formta, can someone please explain how can i convert them from string to number again?
Another approach is to use Read. This is useful if you have numbers in "E" form scientific notation:
Read[StringToStream[#], Number] &/#{ "1" ,"1.5" , "1E-20" , "2.E10" }
{1, 1.5, 1.5*10^-20, 2.*10^10}
Note ToExpression gets these wrong:
ToExpression /# {"1", "1.5", "1.5E-20", "2.E10"}
{1, 1.5, -15.9226, 2. E10}
"1.5E-20" is evaluated as 1.5 * 2.71828-20
in the last case the "E10" is taken as a new symbol..
ToExpression is however faster if you can use it..
The intrinsic function ToExpression will convert its argument to an expression; if the argument is the string representation of a number the function will return the number.
These days (early 2023) there are other ways to convert a number string to a number. For example
Interpreter["Number"]["4.1234"]
ought to return the number 4.1234, and ought to fail when the string can't be parsed as a number. This approach makes it more difficult for arbitrary code to execute.

Best approach for deleting when using Array.combination()?

I want to compare every object in lectures with each other and if some_condition is true, the second object has to be deleted:
toDelete=[]
lectures.combination(2).each do |first, second|
if (some_condition)
toDelete << second
end
end
toDelete.uniq!
lectures=lectures-toDelete
I got some weird errors while trying to delete inside the .each loop, so I came up with this approach.
Is there a more efficient way to do this?
EDIT after first comments:
I wanted to keep the source code free of unnecessary things, but now that you ask:
The elements of the lectures array are hashes containing data of different university lectures, like the name, room,the calendar weeks in which they are taught and begin and end time.
I parse the timetables of all student groups to get this data, but because some lectures are held in more than one student group and these sometimes differ in the weeks they are taught, I compare them with each other. If the compared ones only differ in certain values, I add the values from the second object to the first object and delete the second object. That's why.
The errors when deleting while in .each-loop: When using the Rails Hash.diff method, I got something like "Cannot convert Symbol to Integer". Turns out there was suddenly an Integer value of 16 in the array, although I tested before the loop that there are only hashes in the array...
Debugging is really hard if you have 9000 hashes.
EDIT:
Sample Data:
lectures = [ {:day=>0, :weeks=>[11, 12, 13, 14], :begin=>"07:30", :end=>"09:30", :rooms=>["Li201", "G221"], :name=>"TestSubject1", :kind=>"Vw", :lecturers=>["WALDM"], :tut_groups=>["11INM"]},
{:day=>0, :weeks=>[11, 12, 13, 14], :begin=>"07:30", :end=>"09:30", :rooms=>["Li201", "G221"], :name=>"TestSubject1", :kind=>"Vw", :lecturers=>["WALDM"], :tut_groups=>["11INM"]} ]
You mean something like this?
cleaned_lectures = lectures.combination(2).reject{|first, second| some_condition}

Problems passing options in specialized plot functions for Mathematica

This should be quick to an expert, but I'm relatively new at defining functions with options. Here is a schematic of what I've tried, I'll explain after showing the code:
MyPlotFunction[params_, optionalparameter_List:{1,2,3}, opts:OptionsPattern[]]:=
Plot [ stuff, {x,0,1}, Evaluate#FilterRules[{opts},Options#Plot]];
Options[MyPlotFunction] = { PlotRange->{-5,5}, Frame->True, ... other plot options};
There are four slight subtleties:
I have an optional parameter in my function that needs to be a list of integers.
I want the ability to call the function with any option of Plot, especially using values other than the default values specified in the third line.
I want to have default values for some of the options.
I potentially want to put other options in the function, so it is not guaranteed that all of the options should be passed through to plot.
But what I have above doesn't work. The default options I set are ignored, yet they appear in the ??MyPlotFunction information for my function. I'll give examples if you guys can't spot the error yet.
Edit:
Examples that doesn't work:
SimplePlot[t_,opts:OptionsPattern[{PlotRange->{-4,4},Frame->True}]]:=
Plot[2x+t,{x,0,1},opts];
Fails, the default option is ignored.
SimplePlot[t_,opts:OptionPattern[]]:=
Plot[2x+t],{x,0,1},opts];
Options[SimplePlot] = {PlotRange->{-4,4},Frame->True};
Fails, the default option is ignored.
SimplePlot[t_,opts__:{PlotRange->{-4,4},Frame->True}]:=
Plot[2x+t,{x,0,1},opts];
Default options work with a bare call, but if one of these options or any other plot option is overridden the remaining defaults are lost.
OptionsPattern[] only catches the options that are passed in, so you need to explicitly include any non-default option settings, say by using something like:
FilterRules[{opts, Options[MyPlotFunction]}, Options#Plot]
Here's a simple example:
Options[MyPlotFunction] = {PlotRange -> {-5, 5}, Frame -> True};
MyPlotFunction[params_, optionalparameter_List: {1, 2, 3},
opts : OptionsPattern[MyPlotFunction]] :=
Plot[optionalparameter, {x, 0, 1},
Evaluate#FilterRules[{opts, Options[MyPlotFunction]}, Options#Plot]]
As noted in the comments to Brett's answer, since options given first supersede options given later, and since options to Plot may be given as a list, you can write something like this:
Options[SimplePlot] = {PlotRange -> {-4, 4}, Frame -> True};
SimplePlot[t_, opts : OptionsPattern[]] :=
Plot[2 x + t, {x, 0, 1}, opts, #] & # Options[SimplePlot];

Resources