0b01 ^ 0b10 = 0b1 (Google search result) - language-agnostic

If I Google search 0b01 ^ 0b10, the result I get back by Google's result widget is 0b1.
Is this a bug? Or am I missing something here?
I'm expecting an answer of ob11, based on these simple rules:
0 ^ 0 = 0
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1

Thank you #harold:
The google result widget interprets ^ as exponentiation.
Took me a while to realize that.

Related

ValueError; Quantreg with Intercept

I am working with the following dataframe, called test:
y intercept x
0 -1.6168468132687293 1 NA
1 1.5500031232431757 1 NA
2 1.5952617833602785 1 1.5500031232431757
3 1.1390724309357498 1 1.5952617833602785
4 0.9950311340335872 1 1.1390724309357498
5 0.7095780139613861 1 0.9950311340335872
6 0.5962529862944801 1 0.7095780139613861
7 0.6555353674581792 1 0.5962529862944801
8 1.0008751093886736 1 0.6555353674581792
9 1.2648319050758074 1 1.0008751093886736
I am trying to apply the smf.quantreg() function to the dataframe as follows:
import statsmodels.formula.api as smf
Output_pre = smf.quantreg('y ~ intercept + x', test , missing = 'drop')
Output = Output_pre.fit(q=0.25)
The equivalent standard statsmodels.ols() function works just fine. However, applying the smf.quantreg() function yields the following error:
ValueError: operands could not be broadcast together with shapes (3,) (2,)
Why is that so and how do I solve this issue?
I found out myself. By default, the function gives an intercept. Hence the input matrix is not full rank when including a second intercept (two columns are perfectly colinear).

mapping j function (used defined) over a list

I've written my own version of the exponential (^) function which works fine for simple scalars :
3 : '+/ (y&^%!) i.50'
It doesn't work over a list, so I thought of modifying it with "0
3 : '+/ (y"0&^%!) i.50'
This works over a list but gives the wrong answers.
Two questions arise:
1) Given my usage of "0 doesn't work, is there one that does ?
2) If I don't have access to a functional definition like this, what is the best way to apply it to the individual elements of an array ?
You need to apply the rank conjunction "0 over the function you want to map (y&^%!), instead of its argument y:
3 : '+/(y&^%!)"0 i.50'
However, the precision isn't as good as the native ^:
a =: 3 : '+/(y&^%!)"0 i.50' 4 4 $ 10+i.20
b =: ^ 4 4 $ 10+i.20
a = b
1 1 1 1
0 0 0 0
0 0 0 0
0 0 0 0

Database update weirdness

I am developing a game in Django in which the quantity of rebels updates after a turn change. Rebels are represented as integers from 0 to 100 in the (MySQL) database.
When I save, this happens:
print world.rebels
>>> 0
rebstab = 0
world.rebels += rebstab
world.save()
print world.rebels
>>> 0
However, when I use F() expressions (as I gather I should to prevent race conditions), this happens:
print world.rebels
>>> 0
rebstab = 0
world.rebels = F('rebels') + rebstab
world.save()
print world.rebels
>>> 100
What's going on?
I don't have any experience with using F() objects like this so this answer may not be useful, but having a look at the documentation it mentions:
In order to access the new value that has been saved in this way, the object will need to be reloaded:
reporter = Reporters.objects.get(pk=reporter.pk)
so you may have to actually query the DB again to see the updated value:
print world.rebels
>>> 0
rebstab = 0
world.rebels = F('rebels') + rebstab
world.save()
print World.objects.get(pk=world.pk)
>>> 100
Edit: Also look [at the example where you don't need to pull the objects into memory and can do everythign at the DB level:
World.objects.get(pk=...).update(rebels=F('rebels) + 1)
I just figured out the reason had nothing to do with the F() object itself but rather a conflict with a custom save method. See Django F() objects and custom saves weirdness for a more indepth explanation.

Create line chart from csv file without excel

Suppose i have the following data in csv format :
Time Total Allocated Deallocated
0.00004 0 16 0
0.000516 16 31 0
0.046274 47 4100 0
0.047036 4147 0 31
0.047602 4116 35 0
0.214296 4151 4100 0
0.215109 8251 0 35
i am looking for some kind of software that will allow me to make a line chart of it ( where time column will be the X axis) , i used excel for now , but i am looking for something else,that will allow me to see in greater details .
Any ideas ?
Use Datawrapper. It's very easy and you can publish it on the web or export it to a PNG file.
You can also use R. Here is an example of code to generate a time series plot :
library("ggplot2")
df <- data.frame(date = seq(as.Date("2012-01-01"),as.Date("2012-12-01"), by = "month"), x = rnorm(12))
ggplot(df, aes(x=date, y = x)) + geom_line() + theme_bw()
This is an old question but still: https://plot.ly is also a good site for that kind of stuff.

What's the correct way to expand a [0,1] interval to [a,b]?

Many random-number generators return floating numbers between 0 and 1.
What's the best and correct way to get integers between a and b?
Divide the interval [0,1] in B-A+1 bins
Example A=2, B=5
[----+----+----+----]
0 1/4 1/2 3/4 1
Maps to 2 3 4 5
The problem with the formula
Int (Rnd() * (B-A+1)) + A
is that your Rnd() generation interval is closed on both sides, thus the 0 and the 1 are both possible outputs and the formula gives 6 when the Rnd() is exactly 1.
In a real random distribution (not pseudo), the 1 has probability zero. I think it is safe enough to program something like:
r=Rnd()
if r equal 1
MyInt = B
else
MyInt = Int(r * (B-A+1)) + A
endif
Edit
Just a quick test in Mathematica:
Define our function:
f[a_, b_] := If[(r = RandomReal[]) == 1, b, IntegerPart[r (b - a + 1)] + a]
Build a table with 3 10^5 numbers in [1,100]:
table = SortBy[Tally[Table[f[1, 100], {300000}]], First]
Check minimum and maximum:
In[137]:= {Max[First /# table], Min[First /# table]}
Out[137]= {100, 1}
Lets see the distribution:
BarChart[Last /# SortBy[Tally[Table[f[1, 100], {300000}]], First],
ChartStyle -> "DarkRainbow"]
X = (Rand() * (B - A)) + A
Another way to look at it, where r is your random number in the range 0 to 1:
(1-r)a + rb
As for your additional requirement of the result being an integer, maybe (apart from using built in casting) the modulus operator can help you out. Check out this question and the answer:
Expand a random range from 1–5 to 1–7
Well, why not just look at how Python does it itself? Read random.py in your installation's lib directory.
After gutting it to only support the behavior of random.randint() (which is what you want) and removing all error checks for non-integer or out-of-bounds arguments, you get:
import random
def randint(start, stop):
width = stop+1 - start
return start + int(random.random()*width)
Testing:
>>> l = []
>>> for i in range(2000000):
... l.append(randint(3,6))
...
>>> l.count(3)
499593
>>> l.count(4)
499359
>>> l.count(5)
501432
>>> l.count(6)
499616
>>>
Assuming r_a_b is the desired random number between a and b and r_0_1 is a random number between 0 and 1 the following should work just fine:
r_a_b = (r_0_1 * (b-a)) + a