Generating a random variable with specific mean and variance
I am new to Python
and I am wondering If I can simulate a process where a variable follow a normal distribution with specific mean and variance
I am working on the Vasicek model and I found the distribution of the stochastic rate as:
Is it possible to generate this with a graph via matplotlib
?
1 answer

The approach that generalises to any distribution is to
1) draw a uniform random number in the range [0, 1).
2) Apply the quantile function of your distribution to that random number.
The result of the quantile function will then have the distribution that you require. Other methods such as Box Muller work well for normal distributions, and can be faster, if a little more fiddly to set up.
See also questions close to this topic

How to groupby column headers using a regex?
I have a dataframe like this
S1,0 S1,0.1 S1,0.2 S1,1 S1,1.1 S1,1.2 S2,0 S2,0.1 S2,1 S2,1.1 0 4 0 3 3 3 1 3 2 4 0 1 0 4 2 1 0 1 1 0 1 4 2 3 0 3 0 2 3 0 1 3 3
Now I want to
groupby
its column headers wherebyS1,0
should be in one group,S1,1
in another one, and the same forS2
and apply certain operations on those groups.My expected outcome looks like this (in case I calculate the
mean
, calledm
and thestandard deviation
calleds
):S1,0 S1,1 S2,0 S2,1 m 0 2.333333 2.333333 2.500000 2.000000 1 2.000000 0.666667 0.500000 2.500000 2 2.000000 1.666667 0.500000 3.000000 s 0 2.081666 1.154701 0.707107 2.828427 1 2.000000 0.577350 0.707107 2.121320 2 1.732051 1.527525 0.707107 0.000000
I can get this output doing:
import pandas as pd import numpy as np np.random.seed(0) data = np.random.randint(0, 5, 30).reshape(3, 10) df = pd.DataFrame(data, columns=['S1,0', 'S1,0.1', 'S1,0.2', 'S1,1', 'S1,1.1', 'S1,1.2', 'S2,0', 'S2,0.1', 'S2,1', 'S2,1.1']) df = df.T gdf = df.groupby(lambda x: x.split('.', 1)[0])[df.columns].agg({'m': np.mean, 's': np.std}).T.sort_index()
My question is whether there is a way which avoids this
split
operation on the column names but where one can pass an actual regex? So something along the linesimport re reg = re.compile('^S\d,\d') gdf2 = df.groupby(reg)[df.columns].agg({'m': np.mean, 's': np.std}).T.sort_index()
This does not work but is anything comparable possible?

OpenCV Homography matrix calculation
I have the following image I1. I did not capture it. I downloaded it from Google
I want to apply a homography h to I1 to obtain the following image I2.
I have manually established some suitable correspondence points between I1 and I2, and from these correspondences, I calculate the homography h to get the image I2 when I apply h to I1.
However, I am getting some very small values in the homography matrix h. I have tested this with a wide range of correspondences but I am still getting extremely small values in the h matrix.
My code:
import cv2 import numpy as np import random import math from scipy import linalg no_of_images = 0 t1_y = 10 t2_y = 10 for t1_x in range(30,315,1000): #30 for top_size in range(200,355,1000): t2_x = t1_x + top_size b1_x = t1_x b2_x = t2_x for b_y in range(205,385,1000): #205,305 b1_y = b_y b2_y = b_y no_of_images+=1 image_name="road" location1 = "/path/to/image/"+image_name+".jpg" # Read source image. im_src = cv2.imread(location1) # Four corners in source image pts_src = np.array([[float(t2_x), float(t2_y)], [float(b2_x), float(b2_y)], [float(b1_x), float(b1_y)], [float(t1_x), float(t1_y)]]) location2 = "/path/to/dest/image/after homography.jpg" im_dst = cv2.imread(location2) # Four corners in destination image. for x in range(0,500,1): pts_dst = np.array([[500.0, 168.0],[640.0, 358.0],[0.0, 358.0],[500.0x, 168.0]]) #150 # Calculate Homography h, status = cv2.findHomography(pts_src, pts_dst) print "h = ", h # Warp source image to destination based on homography im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
An example of the h matrix I obtain:
[[ 2.46712622e+00 5.07091356e03 7.29742493e+01] [ 3.54718152e16 5.63521116e01 1.60487917e+02] [ 1.34873822e18 1.11718564e03 1.00000000e+00]]
Why am I getting such tiny values in the h matrix? What am I doing wrong? Is it correct to get such small values in the h matrix? I have been told that this may be due to zero perspective distortion. If this is the case, how do I fix it?

Python pandas rolling winsorize
I have a timeseries pandas dataframe and I have calculated a new column
df['std_series']= ( df['series1']df['series1'].rolling(252).mean() )/ df['series1'].rolling(252).std()
however I want to winsorize to the 5% level before I standardize and on a rolling basis. So for any datapoint, look back 252 days if it is outside the 5% quantiles clip it to the 5% quantile and then standardize. I couldn't figure out how to make it work with
rolling.apply
. All the examples I found were winsorize the either dataframe or entire column. 
SPSS: Make dummy variable for ranges of date of birth
I have a dataset that includes per respondent three variables with his full date of birth (day, month, year). I want to create a dummy variable based on a range of date of birth. For example, those born between December 1, 1948 and August 31, 1949 should be labeled as intervention group, while those born between September 1, 1949 and May 31, 1950 should be the comparison group.
I think that a dummy variable would be a good way to denote these groups, where 1 represents the intervention group, and 0 the comparison group.
However, not all respondents in the dataset have these dates of births, some cannot be considered intervention or comparison group participant.
What would be the best way to do this? Later on, I want to use the dummy variable in a regression analysis. There I can only include those participants either in the comparison or intervention group.
Can anyone please help me with this? I can't find the solution on my own.
Thanks already!

PHP turn table into variables list
I have a table in my database (mySql) that has two columns. I want to turn them into variables at the top of my .php The result would be something like this: $column1 = "column2" (for each lines) . I have absolutely no idea how to do that because each time I create an array the php is read as a string (even between
<?php>
). Any idea how to achieve that? 
Using Excel to find the relationship between 2 variables
I'm trying to create a formula that will give me the following commonality, when the scores 1 and 2 are given.
It needs to be a formula that I can use on a website, otherwise I could use an index, vlookup or =if(and(A1=5,A2=5),100,if(and(A1=5,B1=4),90...
Can I use excel to run a regression, solver, correlation, etc. to return the equation?
Thanks for any advice.

How to Remove time gaps in candlestick plots with matplotlib in python
I have been trying to resolve this for a while now, what has been asked multiple times and are all leading me to this one answered by @gcalmettes
I cannot see the date format used in the input file, financedata.txt there since the provided link to the pastebin location is no longer available. The solution seems to depend on that specific format because it doesn't like the one I have in my input file (YYYYMMDD HH:MM:SS). The supplied code has this comment:
note that I'm using the time you formated into an ordinal float
Either I need to match my data to the original format in financedata.txt, for me to use that code asis or I need to figure out a way to convert my date format into an ordinal float. Can anyone help?

specify matplotlibrc file path in python script
After some research in the matplotlib documentation I could not find the answer to my question so here it goes.
Is it possible to give the (relative or absolute) path to the matplotlibrc file I want to use? I have different configuration for different type of plot and I would like use them without copy/paste to the working directory each time...
Thank you!

Plot datetime.timedelta using matplotlib and python
I am working on a task, where I need to calculate time spent on each day and then represent that time using a bar plot, so for this task I used python and able to get time spent on each day, and stored it in a list "time_list", now I don't understand how to plot this using matplotlib function. The problem is that, this list contains datetime.timedelta class values. Example:
time_list [datetime.timedelta(0, 23820), datetime.timedelta(0, 27480), datetime.timedelta(0, 28500), datetime.timedelta(0, 24180), datetime.timedelta(0, 27540), datetime.timedelta(0, 28920), datetime.timedelta(0, 28800), datetime.timedelta(0, 29100), datetime.timedelta(0, 29100), datetime.timedelta(0, 24480), datetime.timedelta(0, 27000)]
And these values meaning is as follow:
Total Time Spent on 2 is 6:37:00 Total Time Spent on 3 is 7:38:00 Total Time Spent on 4 is 7:55:00 Total Time Spent on 5 is 6:43:00 Total Time Spent on 8 is 7:39:00 Total Time Spent on 9 is 8:02:00 Total Time Spent on 10 is 8:00:00 Total Time Spent on 11 is 8:05:00 Total Time Spent on 12 is 8:05:00 Total Time Spent on 15 is 6:48:00 Total Time Spent on 16 is 7:30:00
Can someone help me in plotting this. Thanks in advance

Why is this php mkdir / uniqid not working?
I have this piece of code that should create a random directory and move uploads there:
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . mkdir( 'assets/post/email_uploads/{uniqid(attachment_)}', 0777 ) . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
The path
assets/post/email_uploads/
already exists so the random folder should go insideemail_uploads
. The issue I'm facing is what to place between theDIRECTORY_SEPARATORs
and have everything work.When I try
mkdir( 'assets/post/email_uploads/{uniqid(attachment_)}', 0777 )
ORmkdir( 'assets/post/email_uploads/'.uniqid(attachment_), 0777 )
 The folder is not created and the upload is placed at the root.When I try
$attchmentPath = 'assets/post/email_uploads/'; $uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $attchmentPath.mkdir( uniqid(attachment_), 0777 ) . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
OR
$attchmentPath = 'assets/post/email_uploads/'; $randomDir = mkdir( uniqid(attachment_), 0777 ); $newPath = $attchmentPath.$randomDir; $uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $newPath . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
The folder is created at the root instead of the desired path and the file is not uploaded at all.

Are 2D Cartesian coordinate points formed by random numbers random in nature?
A series of 2D cartesian coordinates are formed by the combination of random numbers. For example, x and yaxes of every coordinate point are random in nature and follow the Normal distribution. Now, what will be the nature of the formed coordinated points? Will they be random in nature?

Pseudorandom Number Generator with slight bias
I've thought about this for a while to no avail...
How would one go about creating a pseudorandom number generator with a slight (we're talking only apparent after millions, maybe billions, of iterations/tests) bias towards a single number. So, for example, if our generator is producing numbers from 0,1,2,...,98,99 and we want the slight bias for 47.
I feel as though there should be a clever Number Theory kind of solution to this but I couldn't find anything. Curious to see what y'all think!